简体   繁体   中英

Improving poor intermittent SQL query performance

I have a moderately complex nested T-SQL query, which for the most part has good performance, except at the start of each of my 2-week pay periods (where there are fewer records to process!) the query bogs down orders of magnitude slower. Then at some point in the bi-weekly period, SQL Server (2008) determines the execution plan should change and everything is back to normal speeds. I have the execution plans for the "good" and "bad" scenarios, but I am a n00b in this area and I do not know how to interpret them. What is the next step for me here? Post the query? The execution plans? Read some articles/book?

Try updating statistics on the tables in the query at the start of the 2 week period:

DBCC UPDATE STATISTICS table-name

there are some options, like WITH FULLSCAN you could experiment with as well. Take a look in the SQL Server docs.

I got some help over at answers.sqlperformance.com, and while UPDATE STATISTICS <table> does help, it appears to be masking the real problem. I was using the constructs (SELECT COUNT(*) ...) = 0 and (SELECT * ...) IS NULL when I should have been using NOT EXISTS (SELECT * ...) . Changing these resulted in almost another order of magnitude improvement in all cases. It looks like the SQL Plan Optimizer is able to work much better in this case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM