Optimize Your DiscuzNT and run it.

Source: Internet
Author: User
Tags rtrim

Last year, I used DiscuzNT3.0 for secondary development and performance optimization, but I have never had the chance to write down the time relationship. Let's take a long 5.1 vacation and write an article about disruptive things.

I have read the blog of Dai zhenjun from yuanli, and I know that the old generation is a member of the DiscuzNT team. I learned a lot from his blog, the blog I wrote here provides my own views and solutions for some problems. It does not mean to be specific to anyone and adheres to the principles of technical exchanges.

DiscuzNT gave me the impression that

1) powerful functions. All the things you can think of are available;

2) performance needs to be optimized. A large amount of data may cause performance bottlenecks, which is the purpose of this Article ). Due to lack of experience, there are not enough arguments in the previous blog posts. Today we will provide more illustrated arguments.

Now, let's go and start our optimization journey today.

The system environment is as follows:

Software environment:DiscuzNT3.0, sqlserver2000, windwos server 2003

Data Environment:There are about 2.2 million records in the primary table dnt_topics, 3 in the reply table, about 4 million in dnt_posts1, about 5 million in dnt_posts2, about 5 million in dnt_posts3, about 1.7 million in the attachment table, and about 0.2 million in the User table dnt_users, forum table dnt_forums: About 5000 forums

Symptom:When reading the post, if the post contains attachments, it will be very difficult;

Purpose:Speed up viewing, especially when attachments exist.

Hands-on:Find showtopic. aspx. cs. The Code is as follows:

 
 
  1. postlist = Posts.GetPostList(postpramsInfo, out attachmentlist, ismoder == 1);  

Let's look at the code of the Posts. GetPostList) method:

 
 
  1. /// <Summary>
  2. /// Obtain the specified post DataSet
  3. /// </Summary>
  4. /// <Param name = "_ postpramsinfo"> parameter list </param>
  5. /// <Returns> specify the post DataSet condition </returns>
  6. Public static List <ShowtopicPagePostInfo> GetPostList (PostpramsInfo postpramsInfo, out List <ShowtopicPageAttachmentInfo> attachList, bool isModer)
  7. {
  8. List <ShowtopicPagePostInfo> postList = Data. Posts. GetPostList (postpramsInfo );
  9. Int adCount = Advertisements. GetInPostAdCount ("", postpramsInfo. Fid );
  10.  
  11. Foreach (ShowtopicPagePostInfo postInfo in postList)
  12. {
  13. LoadExtraPostInfo (postInfo, adCount );
  14. }
  15. AttachList = new List <ShowtopicPageAttachmentInfo> ();
  16. If (postList. Count = 0)
  17. Return postList;
  18.  
  19. String pidList = GetPidListWithAttach (postList );
  20. AttachList = Attachments. GetAttachmentList (postpramsInfo, pidList );
  21. ParsePostListExtraInfo (postpramsInfo, attachList, isModer, postList );
  22. Return postList; 23}

It can be seen from this that DiscuzNT is a good idea to assemble all the post IDS into the form of "id1, id2, id3, id4" and then import them into the database to avoid multiple database calls, now let's take a look at it and see that it calls the database script. It calls this process dnt_getattachmentlistbypid and uses profiler to track this process to see its performance.

As shown in the figure above, exec dnt_getattachmentlistbypid @ pidlist = '000000' indicates that the cpu Of the script is 5163797, reads = 4531, and duration = 152641. It is very impressive, if there are 10 people calling this process at the same time, it is estimated that the database is under heavy pressure. If there are 100 people, it is hard to imagine. So how can we optimize this process? First, let's take a look at how it is written and whether indexes are used.

 
 
  1. ALTER   PROCEDURE [dnt_getattachmentlistbypid]  
  2. @pidlist varchar(500)  
  3. AS 
  4. SELECT   
  5. [aid],  
  6. [uid],  
  7. [tid],  
  8. [pid],  
  9. [postdatetime],  
  10. [readperm],  
  11. [filename],  
  12. [description],  
  13. [filetype],  
  14. [filesize],  
  15. [attachment],  
  16. [downloads],  
  17. [attachprice],  
  18. [width],  
  19. [height]   
  20. FROM [dnt_attachments]   
  21. WHERE CHARINDEX(','+RTRIM([dnt_attachments].[pid])+',', ','+@pidlist+',')>0GO 

The primary search condition here is pid. If an index is created on the pid column and the index can be used in the process, the results will be better. The optimization work is divided into the following steps:

1) whether the pid column has an index;

2) whether the index is used in the process;

3) Optimize SQL scripts;

4) tracking the optimized results;

Let's go down one step by one:

1) sp_helpindex dnt_attachments check whether there is an index. For example, you can see that the pid column has an index. If there is no index, create an index.

2) Check whether indexes are used. CTRL + L check the execution plan of the following statement. The index he uses is PK_dnt_attachments, and the expected pid is useless.

3) if the expected index is not used, we will optimize it. In the above dnt_getattachmentlistbypid process, where charindex (',' + RTRIM ([dnt_attachments]. [pid]) + ',' + @ pidlist + ',')> 0 performs column operations on the pid, which is the culprit, we can try to remove this column operation. The process is changed to the following:

 
 
  1. ALTER    PROCEDURE [dnt_getattachmentlistbypid]  
  2. @pidlist varchar(500)  
  3. AS 
  4.  
  5. declare @sql nvarchar(2000)  
  6.  
  7. set @sql = '  
  8. SELECT   
  9. [aid],  
  10. [uid],  
  11. [tid],  
  12. [pid],  
  13. [postdatetime],  
  14. [readperm],  
  15. [filename],  
  16. [description],  
  17. [filetype],  
  18. [filesize],  
  19. [attachment],  
  20. [downloads],  
  21. [attachprice],  
  22. [width],  
  23. [height]   
  24. FROM [dnt_attachments]   
  25. WHERE pid in (' + @pidlist + ')'  
  26.  
  27. exec(@sql)  
  28. GO 

4) after the optimization, We will track the performance after optimization. Let's take a look at tracking the same process. For the same parameter, 2nd are before optimization, and 4th are after optimization, the optimization effect is quite unsatisfactory)

So far, our optimization has come to an end.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.