Skip to main content
Our Tech Ideas

Why Your SQL Query Was Fast Yesterday but Very Slow Today

Why Your SQL Query Was Fast Yesterday

It’s a common frustration in database management: your SQL query was blazing fast yesterday, but today it’s crawling. This kind of performance inconsistency can be baffling, but there are several potential reasons behind it. Let’s explore some of the most common causes and how you can address them.

1. Varying Workloads

Backup Operations

If a backup operation is running, it can significantly impact query performance. Backups consume I/O and CPU resources, leading to slower query execution.

Solution: Schedule backups during off-peak hours or use a dedicated backup server.

2. Different Query Plans Due to Parameter Sniffing

Parameter sniffing can cause SQL Server to use a suboptimal query plan for your query based on the parameters provided in a previous execution.

Solution: Use query hints or optimize for specific values to ensure a consistent execution plan.

3. Query Changes

Schema Modifications

Deployments that alter the query, such as adding fields to the SELECT list, can change the query plan and affect performance.

Solution: Review recent changes and test query performance post-deployment to identify issues early.

4. Statistics Change

Statistics help the SQL Server query optimizer create the best execution plan. When these statistics change, the optimizer might choose a different, less efficient plan.

Solution: Regularly update statistics or use the AUTO_UPDATE_STATISTICS option.

5. Hardware Resource Variations

Noisy Neighbors

In a virtualized environment, other virtual machines (VMs) may be competing for the same physical resources, causing slower performance.

Solution: Monitor resource usage and consider isolating critical workloads to dedicated hardware.

6. Index Changes

Adding or removing indexes can drastically affect query performance.

Solution: Ensure indexes are properly maintained and optimized for your queries. Use tools like the Database Engine Tuning Advisor.

7. Server-Level Config Changes

Trace Flags

Changes in server-level configuration settings or trace flags can alter query execution behavior.

Solution: Document and review all server-level changes to understand their impact on query performance.

8. Application Changes

Result Processing

Changes in the application layer, such as how results are processed, can slow down the perceived performance of a query.

Solution: Profile the application to identify bottlenecks and optimize result handling.

9. Unexpected Effects of Patches

Patches can introduce changes that negatively affect performance.

Solution: Test patches in a staging environment before applying them to production. Monitor performance closely post-patch.

10. Memory Grant Variations

Adaptive Memory Grants

With features like adaptive memory grants in SQL Server 2017+, memory allocation for queries can change, affecting performance.

Solution: Monitor memory usage and adjust query or server settings to optimize memory grants.

11. Lock Escalation

Increased Row Modifications

If more rows are being modified at once, you might encounter lock escalation, which can slow down queries.

Solution: Optimize transactions to modify fewer rows or manage lock escalation thresholds.

12. Resource Governor Throttling

If Resource Governor is configured to limit resources for certain workloads, your query might be throttled.

Solution: Review Resource Governor settings and adjust resource pools as needed.

13. Misconception of Initial Performance

Sometimes, what seemed fast initially might not have been truly optimal.

Solution: Establish baseline performance metrics and continuously monitor and tune queries.

14. Adaptive Memory Grants (SQL Server 2017+)

Adaptive memory grants can lead to different memory allocations for similar queries, affecting performance.

Solution: Monitor and tune memory settings, and consider fixing memory grants for critical queries.

15. Adaptive Join Types (SQL Server 2019+)

Changes in the adaptive join threshold can alter join types used in execution plans, impacting performance.

Solution: Analyze execution plans and adjust query or server settings to maintain optimal join strategies.

16. Configuration Changes

Maxdop, Isolation, Compatibility Level

Changes to MAXDOP (maximum degree of parallelism), isolation levels, or compatibility levels can alter query execution.

Solution: Regularly review and document these settings to understand their impact on performance.

Conclusion

Understanding the myriad reasons behind query performance variations is key to maintaining optimal SQL Server operations. Regular monitoring, proper configuration management, and thorough testing of changes can help mitigate these issues. By addressing the potential causes outlined above, you can ensure more consistent and reliable query performance.