Identifying unfinished queries

In the Query Store in action section, you saw that Query Store does not capture runtime statistics only for successfully executed queries. When query execution is aborted by the caller or ends with an exception, this info is not stored in the server cache, but Query Store collects that info too. This can help you easily identify queries with an incomplete execution process.

To see an example, you first need to clean-up info that was captured in the previous sections of this chapter:

ALTER DATABASE WideWorldImporters SET QUERY_STORE CLEAR ALL;
ALTER DATABASE WideWorldImporters SET QUERY_STORE = OFF;
ALTER DATABASE WideWorldImporters
SET QUERY_STORE = ON
(
OPERATION_MODE = READ_WRITE
, DATA_FLUSH_INTERVAL_SECONDS = 2000
, INTERVAL_LENGTH_MINUTES = 1
);

You should also ensure that the latest compatibility mode is applied:

ALTER DATABASE WideWorldImporters SET COMPATIBILITY_LEVEL = 140; 

Now, you can execute the same query as you did in the Query Store in action section:

SELECT *
FROM Sales.Orders o
INNER JOIN Sales.OrderLines ol ON o.OrderID = ol.OrderID;
GO 10

You should execute the query, then click the Cancel Executing Query button in the SSMS, then click again to execute it in order to simulate the execution and query abortion.

In addition to this you should execute the following query, too:

SELECT TOP (1) OrderID/ (SELECT COUNT(*)
FROM Sales.Orders o
INNER JOIN Sales.OrderLines ol ON o.OrderID = ol.OrderID
WHERE o.SalespersonPersonID IN (0,897))
FROM Sales.Orders;

This query will not be successfully executed; it will raise a Divide by zero exception. Now, you can check what Query Store has captured. To do this run the following code:

SELECT * FROM sys.query_store_runtime_stats;

In the following screenshot, you can see two entries for your initial query (regular executed and aborted) and also an entry for the query with the Divide by zero exception:

Identifying unfinished queries by using Query Store

As you can see, by using Query Store you can easy identify started but not executed queries in your database.

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

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