SQL 2019 – Custom Capture of Query Store stats

By | November 13, 2019

Query store is a feature of SQL server that tracks and captures information about queries. Database administrators will find query store useful in trouble shooting performance issues. Most database scenarios query store is perfectly fine under default configuration. However every once in a while a DBA might want to specifically crack metrics where the default values either are too restrictive or 2 broad. In such cases the database administrator can now configure query store using a certain set of custom database scoped configurations.

The below query shows how you can configure query store under the custom capture mode.

      MAX_STORAGE_SIZE_MB = 1000, 
      MAX_PLANS_PER_QUERY = 200,
        EXECUTION_COUNT = 30,

The most important setting to be configured in the above query is stale_capture_policy_threshold. It defines the time period during which query store evaluates the remaining parameters and decides if it wants to capture details of the query. With a default value of one day it assumes that you want to track queries that have executed for the last one day.

Once we tell the database to start tracking queries for the time period of one day the next power meter that needs to be configured is the execution count. The execution count basically enforces a minimum number of executions before a query can be entered into the query store. This helps ignore AdHoc queries that do not have any significant impact in your analysis. The next setting that the DBA will need to configure relates to CPU compile time and as its name suggests it tracks the compiled time of queries. This is an especially useful feature to identify queries that have significant compilation effort as a result of changing plans, parameter sniffing etc.Last but not least we have total execution CPU time in milliseconds. As its name suggests it tracks queries that have total CPU execution times greater than 100 milliseconds. Which typically includes queries that have parallelism or are CPU intensive as a result of the operations being performed on them. Most of the settings are self-explanatory and therefore should be easy to configure and fine tune by the DBA as needed.

It is however important to understand that by setting custom as the query store mode you are going to miss out on one off issues that may occur. Which is why I personally feel the defaults are good enough for a typical database administrative effort and DBAs should use custom only when they are looking for something specific or when they have identified scenarios where query store itself seems to have significant overhead on the server.

Category: Databases

About opsadmin

This post was written by Jayanth Kurup. A Microsoft SQL Server Consultant and Trainer based out of Bangalore, India. Jayanth has been working on MS SQL Server for over 15 years. He is a performance tuning and Business Intelligence expert. Having worked with companies like Microsoft, DELL, Wells Fargo, Thomson Reuters and many other fortune 100 companies. Some other technologies Jayanth works on include Microsoft Azure, PowerBI, Python and AWS. When he isn’t consulting or training, Jayanth like to travel, paint and read. He is also very active in social causes and the founder of Enabled Business Solutions. Visit his company by clicking the link in the menu or email him directly.