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

by Aug 25, 2021

Categories

Tags

Administration agent-based monitoring Agentless Monitoring alert responses alert thresholds alerting Alerts Amazon Aurora Amazon EC2 Amazon RDS Amazon RDS / Aurora Amazon RDS for SQL Server Amazon Redshift Amazon S3 Amazon Web Services (AWS) Analytics application monitoring Aqua Data Studio automation availability Azure Azure SQL Database azure sql managed instance Azure VM backup Backup and recovery backup and restore backup compression backup status Backup Strategy backups big data Blocking bug fixes business architecture business data objects business intelligence business process modeling business process models capacity planning change management cloud cloud database cloud database monitoring cloud infrastructure cloud migration cloud providers Cloud Readiness Cloud Services cloud storage cloud virtual machine cloud VM clusters code completion collaboration compliance compliance audit compliance audits compliance manager compliance reporting conference configuration connect to database cpu Cross Platform custom counters Custom Views customer survey customer testimonials Dark Theme dashboards data analysis Data Analytics data architect data architecture data breaches Data Collector data governance data lakes data lineage data management data model data modeler data modeling data models data privacy data protection data security data security measures data sources data visualization data warehouse database database administration database administrator database automation database backup database backups database capacity database changes database community database connection database design database developer database developers database development database diversity Database Engine Tuning Advisor database fragmentation database GUI database IDE database indexes database inventory management database locks database management database migration database monitoring database navigation database optimization database performance Database Permissions database platforms database profiling database queries database recovery database replication database restore database schema database security database support database synchronization database tools database transactions database tuning database-as-a-service databases DB Change Manager DB Optimizer DB PowerStudio DB2 DBA DBaaS DBArtisan dBase DBMS DDL Debugging defragmentation Demo diagnostic manager diagnostics dimensional modeling disaster recovery Download drills embedded database Encryption End-user Experience entity-relationship model ER/Studio ER/Studio Data Architect ER/Studio Enterprise Team Edition events execution plans free tools galera cluster GDPR Getting Started Git GitHub Google Cloud Hadoop Healthcare high availability HIPAA Hive hybrid clouds Hyper-V IDERA IDERA ACE Index Analyzer index optimization infrastructure as a service (IaaS) infrastructure monitoring installation Integrated Development Environment interbase Inventory Manager IT infrastructure Java JD Edwards JSON licensing load test load testing logical data model macOS macros managed cloud database managed cloud databases MariaDB memory memorystorage memoryusage metadata metric baselines metric thresholds Microsoft Azure Microsoft Azure SQL Database Microsoft PowerShell Microsoft SQL Server Microsoft Windows MongoDB monitoring Monitoring Tools Monyog multiple platforms MySQL news newsletter NoSQL Notifications odbc optimization Oracle PeopleSoft performance Performance Dashboards performance metrics performance monitoring performance schema performance tuning personally identifiable information physical data model Platform platform as a service (PaaS) PostgreSQL Precise Precise for Databases Precise for Oracle Precise for SQL Server Precise Management Database (PMDB) product updates Project Migration public clouds Query Analyzer query builder query monitor query optimization query performance Query Store query tool query tuning query-level waits Rapid SQL rdbms real time monitoring Real User Monitoring recovery regulations relational databases Releases Reporting Reports repository Restore reverse engineering Roadmap sample SAP Scalability Security Policy Security Practices server monitoring Server performance server-level waits Service Level Agreement SkySQL slow query SNMP snowflake source control SQL SQL Admin Toolset SQL CM SQL code SQL coding SQL Compliance Manager SQL Defrag Manager sql development SQL Diagnostic Manager SQL Diagnostic Manager for MySQL SQL Diagnostic Manager for SQL Server SQL Diagnostic Manager Pro SQL DM SQL Doctor SQL Enterprise Job Manager SQl IM SQL Inventory Manager SQL Management Suite SQL Monitoring SQL Performance SQL Quality SQL query SQL Query Tuner SQL Safe Backup SQL script SQL Secure SQL Security Suite SQL Server sql server alert SQL Server Migration SQL Server Performance SQL Server Recommendations SQL Server Security SQL statement history SQL tuning SQL Virtual Database sqlmemory sqlserver SQLyog Storage Storage Performance structured data Subversion Support tempdb tempdb data temporal data Tips and Tricks troubleshooting universal data models universal mapping unstructured data Uptime Infrastructure Monitor user experience user permissions Virtual Machine (VM) web services webinar What-if analysis WindowsPowerShell

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!