SQL Server clustered indexes are converted in functions and even fail (CASE ),

Source: Internet
Author: User

SQL Server clustered indexes are converted in functions and even fail (CASE ),

During the latest phase I optimization, the first N most time-consuming statements in the database are queried!

SELECT TOP 10 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 most time-consuming query is the statistical query.



There are two rows with similar statistics. The data on the day of query is the same table, for example:

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 data volume and the InsertTime field is the clustered index. However, the execution plan does not go through the index. Both subqueries perform a parallel clustered index scan!

Query takes nearly 20 seconds !~

This is because the index key column is converted. The date judgment is changed to the following:

And datediff (dd, InsertTime, GETDATE () = 0 -- changed to: AND InsertTime> = CONVERT (VARCHAR (10), GETDATE (), 120) AND InsertTime <DATEADD (DD, 1, CONVERT (DATE, CONVERT (VARCHAR (10), GETDATE (), 120 )))


Clustered index used successfully, less than 1 second !~



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.