Capturing the Query info

To simulate a database workload, you will execute one simple query. Ensure that the database is in compatibility mode 110, and that Query Store is empty, in order to track your queries easily:

ALTER DATABASE WideWorldImporters SET COMPATIBILITY_LEVEL = 110;
ALTER DATABASE WideWorldImporters SET QUERY_STORE CLEAR;

Run this code to execute one statement 100 times, as shown in the following command:

SET NOCOUNT ON;
SELECT * FROM Sales.Orders o
INNER JOIN Sales.OrderLines ol ON o.OrderID = ol.OrderID
WHERE SalespersonPersonID IN (0, 897);
GO 100

The previous query returns no rows; there are no orders in the Sales.Orders table handled by sales persons with given IDs. After executing this single query 100 times, you can check what has been captured by Query Store by using the following query:

SELECT * FROM sys.query_store_query; 

The query repository contains a single row for each compiled query. You can see in the following screenshot one row representing the query you have executed in the previous step.

Checking captured queries in Query Store

You have cleared Query Store before executing the previous query, and since you have executed only a single query, Query Store captured only one query. However, you could still get more rows from this catalog view if you run this code when you try code examples from this chapter, because some system queries such as updated statistics could also run at this time. Use the following query to return query_text, besides the query_id for all captured queries in Query Store, so that you can identify your query:

SELECT q.query_id, qt.query_sql_text FROM sys.query_store_query q 
INNER JOIN sys.query_store_query_text AS qt ON q.query_text_id = qt.query_text_id; 

The preceding query produces the following output:

You can find the text of your initial query and the query_id associated to it (1 in our case).

As you can see, the query_id of your initial query has a value of 1, and you will use it for further queries. If you get some other value when you run these examples, use it later instead of 1. In this section, the initial query will be tracked by using query_id =1.

The sys.query_store_query catalog view contains information about query hashes, query compilation details, binding and optimizing, and also parameterization. A full list and descriptions of all attributes of the catalog view can be found in Books Online at https://msdn.microsoft.com/en-us/library/dn818156.aspx.

The sys.query_store_query_text catalog view contains query text and statement SQL handles for all queries captured in Query Store. It has only five attributes and is linked to the sys.query_store_query catalog view via the attribute query_id. A full list and descriptions of all attributes of the catalog view can be found in Books Online at https://msdn.microsoft.com/en-us/library/dn818159.aspx.

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

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