SQL Server Performance Tuning

Methods for Performance Tuning on Azure SQL Database

Updated
9 min read
Written by
Mark Varnas

Since Azure SQL Database is database-scoped, there are some significant differences in performance tuning.

Microsoft provides some ways to leverage and find out details about how your database is performing in Azure.

Let’s see a few of these.

1. Extended Events

Extended Events (also called XEvent) provide a powerful tool to troubleshoot.

An XEvent session in Azure SQL Database is not the same as a typical SQL Server.

Since you don’t have access to the server’s file system, you must configure a storage account (to persist the Extended Events data).

There are some great articles on troubleshooting a specific issue with Extended Events.

Microsoft’s page Extended Events in Azure SQL Database provides samples and more details about using Extended Events to improve performance issues.

In short, you need to create:

  • A storage account (or use an existing one)
  • A database-scoped credential so you can use the storage account
  • And finally, the Extended Event session.

2. Query Store

Enabled by default on Azure SQL Server, the Query Store automatically captures a history of queries, plans, and runtime statistics.

It acts as a “flight data recorder” for the database, continuously collecting critical information about the queries.

Using it, you can review a list of the highest resource-consuming queries and drill down further by selecting individual queries to view more details.

In short, the Query Store is going to improve your productivity significantly:

  • Finding and fixing application performance regressions. With Query Store, you can force an optimal plan for future query execution.
  • Identifying the most expensive queries considering different consumption metrics (CPU time, elapsed time, used memory, read operations, etc.);
  • Keeping performance stability with compatibility level 130 in Azure SQL Database (more details in this blog article);
  • Comparing workload performance before and after the application change you plan to introduce (A/B testing);
  • Identifying and improving ad-hoc workloads (more details here);

3. Query performance insight

Query performance gets insight into resource consumption.

You can access the query performance insight using the web portal or REST API (programmatic access).

It helps to understand easily where your database transaction units (DTU) are being spent.

Understanding DTU measures is essential to determine the right service tier/performance level needs (scale up/down).

When the server DTU hits the limit, latencies for the requests increase. In some cases, new requests can get rejected with an error when the concurrent request limit for DB is reached.

For more details, see this post about how to choose the right service database.

4. DMVs or Dynamic Management Views

Dynamic Management Views (DMVs) are virtual views that expose SQL Server deployment’s internal states.

They are essential to monitor and troubleshoot any SQL Server deployments from on-premise to cloud-enabled versions.

Unfortunately, real-time troubleshooting of certain conditions is not possible. You quickly find lots of online resources from the SQL community for troubleshooting.

Azure SQL Database partially supports three categories of dynamic management views:

  • Session-related dynamic management views:
    • sys.dm_exec_connections: Active connections to the database.
    • sys.dm_exec_sessions: Active sessions to the database.
    • sys.dm_exec_requests Active requests to the database.
  • Execution-related dynamic management views:
    • sys.dm_db_missing_index_details: Missing indexes that would increase the query performance.
    • sys.dm_db_missing_index_columns: Missing table columns for a given index.
    • sys.dm_db_missing_index_groups: Missing indexes are contained in a specific missing index group, excluding spatial indexes.
    • sys.dm_db_missing_index_group_stats: Groups of missing indexes, excluding spatial indexes.
    • sys.dm_db_index_usage_stats: Information about the usage of an index.
    • sys.dm_db_index_physical_stats: Information about the physical layout for a given index (space consumption etc).
    • sys.dm_db_index_operational_stats: Information about the performance for a given index.
    • sys.dm_exec_procedure_stats: Usage of stored procedures in the database.
    • sys.dm_exec_trigger_stats: Usage of triggers in the database.
    • sys.dm_exec_query_memory_grants: Queries waiting for memory before they can be executed.
    • sys.dm_exec_cached_plans: Execution plans that are currently in the case.
    • sys.dm_db_partition_stats: Information about the storage of tables/indexes.
    • sys.dm_db_wait_stats: Waits in the database (new DMV specific to SQL Server Database).
    • sys.dm_tran_locks: Active locks in the database.
  • Transaction-related dynamic management views:
    • sys.dm_tran_active_transactions: Active user transactions to the database.
    • sys.dm_tran_database_transactions: Active transactions in the database.
    • sys.dm_tran_session_transactions: Active transactions in the session.

After reviewing the appropriate DMVs, it is then possible to troubleshoot any performance issues you encounter.

For more details, see Dynamic Management Views and Functions in SQL Server Books Online.

5. Database advisor (automatic index management)

Azure SQL Database continuously monitors the queries and identifies the indexes that could improve performance.

It creates indexes by estimating the performance gain the index would bring through time. The index management also removes unused or duplicate indexes.

Any index recommendations are captured in a DMV to be reviewed. You can see which indexes are auto-created by looking at the sys.indexes view.

Database advisor continuously analyzes the performance of an existing index. So, if an index is not used, the Azure SQL Database recommends dropping it.

We recommend only dropping an index in two cases:

  • Duplicated index (same indexed and included column, partition schema, and filters).
  • Index unused for a prolonged period (93 days).

6. Adaptive Query Processing

Starting with SQL Server 2017 (14.x)) and Azure SQL Database, SQL Server offers a feature family called Adaptive Query Processing (AQP).

AQP improves the cardinality estimator (CE) process and automatically adapts optimization strategies to runtime conditions of your SQL Server workload.

AQP offers three techniques for adapting to application workload characteristics:

Batch mode memory grant feedback

Insufficient memory grants cause expensive spills to disk, bringing performance issues. This feature enables the SQL Server Query Processing Engine to learn that memory grants are not sufficient.

The batch mode memory grant feedback feature is useful for recalculating the actual memory that a query requires and updating the grant value appropriately.

Batch mode adaptive joins

The SQL Server typically chooses among three types of physical join operators: nested loop joins, merge joins, and hash joins.

Each type of join has strengths and weaknesses.

The batch mode adaptive defines a threshold used to decide when to switch between nested loop joins or hash joins. The threshold is calculated for individual statements depending on input data.

Consequently, a plan can dynamically switch to a better join strategy during execution.

Interleaved execution

Multi-statement table-valued functions (MSTVFs) are popular among developers, although their cardinality issues can cause performance slowdowns.

Interleaved execution helps to fight cardinality issues.

Suppose the estimates are way off than the actual ones. In that case, SQL Server Query Optimizer adjusts the execution plan by executing a part of the query execution plan first and re-designing the query execution plan on the actual number of rows.

The compiler pauses on facing a candidate for interleaved execution during a query optimization phase. Next, it executes a query subtree for the MSTVF.

In the end, the compiler captures the correct cardinality estimation and resumes previously paused operations.

Article by
Mark Varnas
Founder | CEO | SQL Veteran
Hey, I'm Mark, one of the guys behind Red9. I make a living performance tuning SQL Servers and making them more stable.

Managed SQL Server services, consulting, and emergency support from expert DBAs to improve performance, predictability, and cost.

Get started with Red9 today.

Contact us

Discover More

SQL Server Health Check SQL Server Migrations & Upgrades SQL Server Performance Tuning SQL Server Security SQL Server Tips

Discover what clients are saying about Red9

Red9 has incredible expertise both in SQL migration and performance tuning.

The biggest benefit has been performance gains and tuning associated with migrating to AWS and a newer version of SQL Server with Always On clustering. Red9 was integral to this process. The deep knowledge of MSSQL and combined experience of Red9 have been a huge asset during a difficult migration. Red9 found inefficient indexes and performance bottlenecks that improved latency by over 400%.

Rich Staats 5 stars
Rich Staats
Cloud Engineer
MetalToad

Always willing to go an extra mile

Working with Red9 DBAs has been a pleasure. They are great team players and have an expert knowledge of SQL Server database administration. And are always willing to go the extra mile to get the project done.
5 stars
Evelyn A.
Sr. Database Administrator

Boosts server health and efficiency for enhanced customer satisfaction

Since adding Red9 to the reporting and DataWarehousing team, Red9 has done a good job coming up to speed on our environments and helping ensure we continue to meet our customer's needs. Red9 has taken ownership of our servers ensuring they remain healthy by monitoring and tuning inefficient queries.
5 stars
Andrew F.
Datawarehousing Manager
See more testimonials