Capturing plan info

For each query, you can have at least one execution plan. Therefore, in the plan repository, at least one entry exists for each query from the query repository. The following query returns rows from the plan repository:

SELECT * FROM sys.query_store_plan; 

You can see four plans for four executed queries in the WideWorldImporters database. The first query is your initial query; the rest are queries against catalog views as shown in the following screenshot. As mentioned earlier in this section, you can see more entries if you execute the code examples from this chapter.

Check captured query plans in Query Store

The sys.query_store_plan  catalog view contains information about query plan generation, including the plan in XML format. 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/dn818155.aspx.

Retuning query text beside IDs is always a good idea. Run the following code to show query text info, too:

SELECT qs.query_id, q.query_sql_text, CAST(p.query_plan AS XML) AS qplan
FROM sys.query_store_query AS qs
INNER JOIN sys.query_store_plan AS p ON p.query_id = qs.query_id
INNER JOIN sys.query_store_query_text AS q ON qs.query_text_id = q.query_text_id
ORDER BY qs.query_id;

The output is more user-friendly as the following screenshot clearly shows:

Checking captured queries and query plans in Query Store

As you can see, queries against catalog views are also there, but you are interested in user queries only. For the initial query, you can see that all its executions were done with the same execution plan.

Here, you can already see the first great thing about Query Store. You can identify all queries that are executed with more than one execution plan. Use the following query to identify query_id for queries that have at least two different plans:

SELECT query_id, COUNT(*) AS cnt  
FROM sys.query_store_plan p 
GROUP BY query_id  
HAVING COUNT(*) > 1 ORDER BY cnt DESC; 

The query returns no rows in this case. You have simply executed only one query, but this is very useful information: you can instantly identify unstable queries in your system.

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

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