Filtering with WHERE

Improving Query Performance in SQL Server

Dean Smith

Founder, Atamai Analytics

How WHERE works

SELECT *
FROM PlayerStats
WHERE Position = 'SG'

PlayerStats Table

Improving Query Performance in SQL Server

How WHERE works

SELECT *
FROM PlayerStats
WHERE Position = 'SG'

Filtered PlayerStats Table

Improving Query Performance in SQL Server

WHERE processing order

SELECT PlayerName, 
      Team, 
      (DRebound+ORebound) AS TotalRebounds
FROM PlayerStats
WHERE TotalRebounds >= 1000
ORDER BY TotalRebounds DESC;
  • WHERE is processed before SELECT
-- ERROR
Invalid column name 'TotalRebounds'.
Improving Query Performance in SQL Server

Using a sub-query

SELECT PlayerName, 
       Team, 
       TotalRebounds
FROM
     -- Start of sub-query tr
    (SELECT PlayerName, Team, 
             (DRebound+ORebound) AS TotalRebounds
     FROM PlayerStats) tr
WHERE TotalRebounds >= 1000 -- created in the sub-query
ORDER BY TotalRebounds DESC;
Improving Query Performance in SQL Server

Using a sub-query

SELECT PlayerName, 
       Team, 
       TotalRebounds
FROM
     -- Start of sub-query tr
    (SELECT PlayerName, Team, 
             (DRebound+ORebound) AS TotalRebounds
     FROM PlayerStats) tr
WHERE TotalRebounds >= 1000 -- created in the sub-query
ORDER BY TotalRebounds DESC;
PlayerName Team TotalRebounds
Andre Drummond DET 1247
DeAndre Jordan LAC 1171
Karl-Anthony Towns MIN 1012
Dwight Howard CHO 1012
Improving Query Performance in SQL Server

Calculations on columns

SELECT PlayerName, 
       Team, 
       (DRebound+ORebound) AS TotalRebounds
FROM PlayerStats
WHERE (DRebound+ORebound) >= 1000
ORDER BY TotalRebounds DESC;
  • Calculations on columns in the WHERE filter condition could increase query times
PlayerName Team TotalRebounds
Andre Drummond DET 1247
DeAndre Jordan LAC 1171
Karl-Anthony Towns MIN 1012
Dwight Howard CHO 1012
Improving Query Performance in SQL Server

Functions on columns

SELECT PlayerName, College, DraftYear 
FROM Players
WHERE UPPER(LEFT(College,7)) = 'GEORGIA'; 
-- unnecessary use of functions 
-- on a filtering column
  • Applying functions to columns in the WHERE filter condition could increase query times
PlayerName College DraftYear
Damien Wilkins Georgia
Derrick Favors Georgia Tech 2010
Iman Shumpert Georgia Tech 2011
R.J. Hunter Georgia State 2015
... ... ...
Improving Query Performance in SQL Server

WHERE simplified

SELECT PlayerName, College, DraftYear 
FROM Players 
        -- No calculation or function
WHERE College like 'Georgia%'; 
PlayerName College DraftYear
Damien Wilkins Georgia
Derrick Favors Georgia Tech 2010
Iman Shumpert Georgia Tech 2011
R.J. Hunter Georgia State 2015
... ... ...
Improving Query Performance in SQL Server

Summary

  • WHERE is processed before SELECT
  • Calculations on columns in the WHERE filter condition could increase query times
  • Applying functions to columns in the WHERE filter condition could increase query times
Improving Query Performance in SQL Server

Let's practice!

Improving Query Performance in SQL Server

Preparing Video For Download...