Time statistics

Improving Query Performance in SQL Server

Dean Smith

Founder, Atamai Analytics

SQL Server Management Studio

 

SQL Server Management Studio splash screen

Improving Query Performance in SQL Server

STATISTICS TIME in SSMS

SELECT UNStatisticalRegion, 
        CountryName,
        Capital
FROM Nations
WHERE Capital IN 
        (SELECT CityName 
        FROM Cities)
            AND Capital IN 
                (SELECT CityName 
                    FROM Cities 
                    WHERE Pop2017 > 1000000);

STATISTICS TIME - command reports number of milliseconds, required to parse, compile, and execute a query.

SQL Server parse and compile time: 
   CPU time = 16 ms, elapsed time = 21 ms.

(88 rows affected)

 SQL Server Execution Times:
   CPU time = 390 ms,  elapsed time = 382 ms.
Improving Query Performance in SQL Server

SQL Server Execution Times

 SQL Server Execution Times:
   CPU time = 390 ms,  elapsed time = 382 ms.
  • CPU time: time taken by server processors to process the query
  • Elapsed time: total duration of the query
Improving Query Performance in SQL Server

Example: query 1

SELECT UNStatisticalRegion, 
       CountryName,
       Capital
FROM Nations
WHERE Capital IN 
        (SELECT CityName -- 1st sub-query
        FROM Cities) 
            AND Capital IN 
                (SELECT CityName -- 2nd sub-query
                 FROM Cities 
                 WHERE Pop2017 > 1000000); 
Improving Query Performance in SQL Server

Example: query 1

SET STATISTICS TIME ON
SELECT UNStatisticalRegion, 
       CountryName,
       Capital
FROM Nations
WHERE Capital IN 
        (SELECT CityName -- 1st sub-query
        FROM Cities) 
            AND Capital IN 
                (SELECT CityName -- 2nd sub-query
                 FROM Cities 
                 WHERE Pop2017 > 1000000); 
SQL Server Execution Times:
   CPU time = 391 ms,  elapsed time = 381 ms.
Improving Query Performance in SQL Server

Example: query 2

SELECT UNStatisticalRegion, 
       CountryName,
       Capital 
FROM Nations n
WHERE EXISTS 
    (SELECT 1 
     FROM  Cities c 
     WHERE n.Capital = c.CityName
        AND Pop2017 > 1000000);


SQL Server Execution Times:
   CPU time = 0 ms,  elapsed time = 2 ms.
Improving Query Performance in SQL Server

Example: query 2

SELECT UNStatisticalRegion, 
       CountryName,
       Capital 
FROM Nations n
WHERE EXISTS 
    (SELECT 1 
     FROM  Cities c 
     WHERE n.Capital = c.CityName
        AND Pop2017 > 1000000);
SET STATISTICS TIME OFF
SQL Server Execution Times:
   CPU time = 0 ms,  elapsed time = 2 ms.
Improving Query Performance in SQL Server

Comparing queries

  • First query containing two sub-queries
SELECT UNStatisticalRegion, 
       CountryName,
       Capital
FROM Nations
WHERE Capital IN 
        (SELECT CityName 
        FROM Cities)
            AND Capital IN 
                (SELECT CityName 
                 FROM Cities 
                 WHERE Pop2017 > 1000000);
SQL Server Execution Times:
   CPU time = 391 ms,  elapsed time = 381 ms.
  • Second query using EXISTS
SELECT UNStatisticalRegion, 
       CountryName,
       Capital 
FROM Nations n
WHERE EXISTS 
    (SELECT 1 
     FROM  Cities c 
     WHERE n.Capital = c.CityName
        AND Pop2017 > 1000000);
SQL Server Execution Times:
   CPU time = 0 ms,  elapsed time = 2 ms.
Improving Query Performance in SQL Server

Elapsed time vs. CPU time

Elapsed time

  • May be variable when analyzing query time statistics
  • Time best time statistics measure for the fastest running query

CPU time

  • Should vary little when analyzing query time statistics
  • May not be a useful measure if server processors are running in parallel
Improving Query Performance in SQL Server

Taking an average

  • Don't rely on one measurement, take an average.
elapsed time = 2032 ms.

elapsed time = 2060 ms.

elapsed time = 1915 ms.

elapsed time = 4009 ms.

elapsed time = 3511 ms.

Average elapsed time = 2705 ms.
Improving Query Performance in SQL Server

Let's practice!

Improving Query Performance in SQL Server

Preparing Video For Download...