Top 5 Reasons for Slow SQL Server Queries and How to Fix Them

by Aug 25, 2021

Slow SQL Server Queries are a common issue among database administrators (DBAs). You would be hard-pressed to find a SQL Server DBA who was not interested in improving the performance of their databases and applications. Slow response time can impact database performance and result in major repercussions to the business, as disgruntled customers search for faster alternative solutions. 

Optimizing the performance of a SQL Server instance can be a daunting task for even the most experienced DBA. In the data-driven business era, such optimization is crucial as so much is reliant on immediately available information from an organization’s databases and systems. As a DBA, you don’t have time to waste on a wild goose chase for the magic performance enhancer. Fortunately, you don’t have to.

Queries are at the heart of all relational databases, satisfying requests for information in a given amount of time. A viable course of action for improving database performance is to focus on the queries a system is executing.

Slow SQL Queries are a common reason for poor database performance. So understanding the most common reasons for slow SQL Queries can help diagnose and resolve database performance issues more quickly. 

A methodical approach will enable similar techniques to be used on multiple queries, leading to the sought-after performance gains.

Top 5 Reasons for Slow SQL Server Queries

While there are always unique situations that need to be resolved, the majority of slow queries are afflicted by one of these issues:

  • Missing indexes;
  • Stale or missing statistics;
  • Blocking;
  • Inefficient query plans;
  • Insufficient system resources.

When ill-performing queries are discovered, these items furnish an excellent starting point for tuning activities. 

Addressing Performance Issues One Query at a Time

Identifying prime candidates for tuning needs to balance the duration and frequency with which a given query runs. Concentrating on a long-running query that runs very infrequently will not provide the intended overall performance gains.

The same is true for queries that run frequently and can only be improved slightly. There is likely to be a subset of queries on most databases that will result in faster system response when tuned. 

Most problem queries can be fixed using a few tried, tested, and repeatable solutions. Following are some tips for productive query tuning.

  • Have sufficient and reliable test data and work on it in a non-production system.
  • Work on a single statement at a time. This includes taking apart a stored procedure to address individual queries. 
  • Test queries after making modifications to ensure that the query still returns the correct result despite any timing changes.
  • Save versions of your work so you can easily roll back if necessary.
  • Test with a variety of inputs that attempt to cover as many real-life situations as possible.

An Effective Query Tuning Strategy

The following steps outline an effective approach to tuning SQL queries. Performing them iteratively on your problem queries should result in improved performance.

  • Run the query and document its reads and duration. This is your baseline against which you can measure any improvements.
  • If the query has an obvious problem, a simple rewrite may be the answer.
  • Look at the query plan for warnings regarding missing indexes or other problems.
  • Identify the most expensive operator and verify that the estimated and actual numbers are close. If they are not, update the statistics and rerun the query. 
  • Look at the most expensive operator to determine if it can benefit from an index.
  • If there are many lookups, consider a covering index.
  • See if recompiling for different test values makes a difference in query performance. It could be that the problems are more impactful when specific values are used.
  • If the number of reads is reasonable but query duration is high, blocking may be an issue that can be addressed using nolock. 

Recently, IDERA hosted a live walkthrough of SQL Server optimization techniques. The walkthrough includes hands-on demonstrations, useful if you prefer to learn by example. IDERA’s experts demonstrate how DBAs can make real optimization progress by finding and tuning individual queries. 

Using the Right Tool to Optimize Your Queries

DB Optimizer offers the functionality necessary to tune SQL queries for better performance. The tool supports SQL Server, Oracle, Db2, and Sybase from a single user interface that minimizes the learning curve and aids team productivity. The application’s innovative Visual SQL Tuning (VST) diagrams turn text-based SQL code into graphical SQL diagrams, making it easy to see the effects of a query on a database. 

Profiling of wait-time analysis helps DBAs locate the queries that need to be tuned.  Graphical visualization of wait-time analysis helps identify bottlenecks causing poor database performance. DB Optimizer lets teams simulate production environments to tune performance without risking impacting business-critical systems.

DB Optimizer is available as a stand-alone application or as part of the DB PowerStudio suite of database tools. Free trials are available for both products so you can see how they help your database team optimize their systems.

Try DB Optimizer for free!

Try DB PowerStudio for free!