Query Store architecture

Query Store is integrated with the query processor in the database engine. A simplified Query Store architecture in SQL Server 2017 is shown in the following figure:

Query Store architecture

Query Store actually has three stores:

  • Query and Plan Store: This stores information about executed queries and execution plans used for their execution
  • Runtime Statistics store: This store holds aggregated execution parameters (execution time, logical reads, and so on) for executed queries within a specified time
  • Wait Stats Store: This store persists wait statistics information

All three stores have instances in memory and persisted representation through disk tables. Due to performance reasons, captured info is not immediately written to disk; rather it is written asynchronously. Query Store physically stores this info into the database primary file group.

When a query is submitted to the database engine and Query Store is enabled for the database, during query compilation, Query Store captures information about the query and execution plan. This is then sent into the Query and Plan Store, if the information is not already there. Information about a single query is stored only once in Query Store; the same for execution plans. For every execution plan we have only one row (up to 200 different execution plans are supported by default).

When the query is executed, Query Store captures the most relevant execution parameters (duration, logical reads, CPU time, used memory, and so on), uses them in aggregated calculations, and sends them into the Runtime Statistics store only at configured time intervals. Therefore, Query Store does not store info about every single execution. If the time interval is 10 minutes, for instance, it stores runtime stats for a single execution plan every 10 minutes.

Captured data is persistently stored in internal database tables. It is not directly available for reading and manipulation. Instead of that, Query Store functionalities are exposed through Query Store catalog views and stored procedures. There are eight Query Store catalog views:

  • sys.database_query_store_options
  • sys.query_context_settings
  • sys.query_store_plan
  • sys.query_store_query
  • sys.query_store_query_text
  • sys.query_store_runtime_stats
  • sys.query_store_runtime_stats_interval
  • sys.query_store_wait_stats

The sys.database_query_store_options system catalog view holds information about Query Store configuration such as actual state, current storage size, maximal storage size, maximum number of plans per query, and so on. You can use this view to check the status and available space and create appropriate alerts when Query Store has switched to Read-Only mode or there isn't enough free space.

The sys.database_query_store_query view contains information about queries and associated overall aggregated runtime execution statistics. There is exactly one row for each query in the system. Queries within a stored procedure, function, or trigger have the same object_id property. There are 30 columns in this view: they cover details about parameterization, the aforementioned object_id, statistics about duration, CPU and memory usage during compilation, binding, and optimization. There are also two columns that are used for connection to other catalog views: query_text_id and context_settings_id. This view does not contain the text of the query. To get the query text, you need to join it with the sys.query_store_query_text catalog view.

The sys.database_query_store_query_text view contains query text (including whitespaces and comments) and SQL handle and a few flags about encryption or restricted words.

The sys.query_context_settings view contains information about the semantics affecting context settings associated with a query. It contains information about date settings, language, and SET options (ARITHABORT, ANSI_NULLS, QUOTED_IDENTIFIER).

 The sys.query_store_plan view stores information about the execution plans for captured queries. It contains an XML representation of the execution plan, flags related to it, and information about the compilation and execution time.

To get execution statistics for execution plans captured by Query Store, you need to query the sys.query_store_runtime_stats catalog view. It has exactly one row for a combination of the plan_id, execution_type and runtime_stats_interval_id attributes. Query Store collects, calculates, and aggregates execution parameters for each execution plan and store them for all successfully executed queries in a single row at the  specified interval. If there were aborted executions or executions that ended with an exception, the collected information for them is stored in separated rows. It stores dozens of aggregated statistical values (min, max, avg, stdev, count) for execution parameters such as duration, CPU time, logical and physical IO, memory usage, degree of parallelism, and different time stamp information. This is a very detailed catalog view; it has 68 columns!

 The sys.query_store_runtime_stats_interval view contains information about the start and end time of each interval over which runtime execution statistics information for a query has been collected.

The sys.query_store_wait_stats catalog view was added in SQL Server 2017 and stores wait information about queries captured in Query Store. Like the query_store_runtime_stats view, it contains exactly one row per plan_id, runtime_stats_interval_id, execution_type, and wait_category attribute combination. To simplify troubleshooting and reduce stored information, wait types are grouped in 24 wait categories. 

Detailed info about Query Store Catalog Views can be found in SQL Server Books Online at https://msdn.microsoft.com/en-us/library/dn818149.aspx.

You can also interact, and perform some actions, with Query Store by using six Query Store stored procedures:

  • sp_query_store_flush_db
  • sp_query_store_force_plan
  • sp_query_store_remove_plan
  • sp_query_store_remove_query
  • sp_query_store_reset_exec_stats
  • sp_query_store_unforce_plan

Of course, SQL Server Books Online describes Query Store stored procedures in detail at https://msdn.microsoft.com/en-us/library/dn818153.aspx.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.124.21