SQL Server clustered indexes are converted and invalidated in functions (case)

Source: Internet
Author: User
Tags getdate

Recently in the first phase of optimization, the database of the most time-consuming first n statements to query out!

SELECT TOP qp.query_plan,qt.text,total_worker_time from   sys.dm_exec_query_stats cross  APPLY sys.dm_exec_ Sql_text (sql_handle) QT cross  APPLY sys.dm_exec_query_plan (plan_handle) QP  where Qp.query_plan.exist (' Declare namespace   qplan= "Http://schemas.microsoft.com/sqlserver/2004/07/showplan";              qplan:relop[@LogicalOp = "Index Scan"              or @LogicalOp = "Clustered index Scan"              or @LogicalOp = "Table Scan"]) =1  ORDER BY Total_worker_time DESC

The first one is the most time-consuming, the statistical query



There are 2 rows of similar statistics, the query day data, are the same table, such as:

SELECT ISNULL (SUM (value), 0) as VALUE from Mytab WHERE status=1 DATEDIFF (Dd,inserttime,getdate ()) =0


This table is the table with the largest amount of data, and the field inserttime as a clustered index, but to see that the execution plan is not indexed, two subqueries have a parallel clustered index scan!

It takes nearly 20 seconds to query! ~

This is because the index key column is converted, and finally the date is changed to the following:

and DATEDIFF (Dd,inserttime,getdate ()) =0--changed to: and Inserttime>=convert (VARCHAR), GETDATE (), and Inserttime <dateadd (Dd,1,convert (Date,convert (VARCHAR), GETDATE (), 120))


Use clustered index successfully, less than 1 seconds! ~



SQL Server clustered indexes are converted and invalidated in functions (case)

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.