Optimize based on SQL execution duration

Source: Internet
Author: User

1) Clear the cache plan first, pay attention to the production environment use this statement with caution!

DBCC FREEPROCCACHE


2) re-execute the query, system use, etc.

3) Find the longest statement time, you may need to perform multiple times to determine the average time, the code is as follows:

SELECT creation_time N ' statement compilation time '


, Last_execution_time N ' last Execution time '


, total_physical_reads N ' total number of physical reads '


, Total_logical_reads/execution_count N ' Each logical read count '


, Total_logical_reads N ' logical read total number of times '


, Total_logical_writes N ' logical write total number of times '


, Execution_count N ' execution times '


, total_worker_time/1000 N ' Total CPU time MS '


, total_elapsed_time/1000 N ' total time spent MS '


, (total_elapsed_time/execution_count)/1000 N ' Average Time MS '


, SUBSTRING (St.text, (QS.STATEMENT_START_OFFSET/2) + 1,


(Case Statement_end_offset


WHEN-1 then datalength (st.text)


ELSE Qs.statement_end_offset End


-Qs.statement_start_offset)/2) + 1) N ' execute statement '


From Sys.dm_exec_query_stats as Qs


Cross APPLY Sys.dm_exec_sql_text (qs.sql_handle) St


where SUBSTRING (St.text, (QS.STATEMENT_START_OFFSET/2) + 1,


(Case Statement_end_offset


WHEN-1 then datalength (st.text)


ELSE Qs.statement_end_offset End


-Qs.statement_start_offset)/2) + 1) Not like '%fetch% '


ORDER by Total_elapsed_time/execution_count DESC;



4) According to the 3rd step result, copy the maximum time statement, then execute, and call up the execution plan (CTRL+M)

View properties, output list, based on the longest percentage taken in the execution plan


5) Optimization According to the column of the output table, for example to build an index



This article is from the "Imagine the Sky" blog, please be sure to keep this source http://kinwar.blog.51cto.com/3723399/1548219

Optimize based on SQL execution duration

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.