For most developers, performance regressions can usually be traced back to a specific event such a major influx of users or a code change. For the database developer, it isn’t always so easy. As indexes are rebuilt and statistics updated, SQL Server may decide to “rewrite” your code by regenerating the execution plan. Without the right backup and production-grade hardware, seeing what changed in the execution plan can be neigh impossible.
At least it was. With SQL Server 2016, Microsoft will start storing historic execution plans using a feature called Query Store. When enabled, the Query Store will log information about each query including:
- Number of executions
- Execution time
- Memory Consumption
- Logical Reads
- Logical Writes
- Physical Reads
- Number of execution plan changes
To reduce the load on the server, this information is aggregated into fixed windows. If you need more precise data, you should look to Extended Events instead.
The easiest way to use this information is to open the Regressed Queries view.
This tool will allow you to see regressions based on any of the recorded metrics. If you see a regression, you have the option to force SQL Server to use an older execution plan.
Query Store Tuning
Because tracking metrics like this can be expensive, SQL Server allows you to tune the Query Store. Factors you can adjust include the size of the aggregate window in minutes, the max size of the query store in MB, the maximum number of execution plans to store. You can also tell the Query Store to only record queries that meet certain criteria.
Programmatic Access
As with most SQL Server features, all the information you see in the Regressed Queries tool can be accessed directly using a collection of management views.
- sys.database_query_store_options (Transact-SQL)
- sys.query_context_settings (Transact-SQL)
- sys.query_store_plan (Transact-SQL)
- sys.query_store_query (Transact-SQL)
- sys.query_store_query_text (Transact-SQL)
- sys.query_store_runtime_stats (Transact-SQL)
- sys.query_store_runtime_stats_interval (Transact-SQL)