Improving Query Performance in SQL Server
Dean Smith
Founder, Atamai Analytics
SELECT *
FROM PlayerStats
WHERE Position = 'SG'

SELECT *
FROM PlayerStats
WHERE Position = 'SG'

SELECT PlayerName,
Team,
(DRebound+ORebound) AS TotalRebounds
FROM PlayerStats
WHERE TotalRebounds >= 1000
ORDER BY TotalRebounds DESC;
-- ERROR
Invalid column name 'TotalRebounds'.
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;
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 |
SELECT PlayerName,
Team,
(DRebound+ORebound) AS TotalRebounds
FROM PlayerStats
WHERE (DRebound+ORebound) >= 1000
ORDER BY TotalRebounds DESC;
| PlayerName | Team | TotalRebounds |
|---|---|---|
| Andre Drummond | DET | 1247 |
| DeAndre Jordan | LAC | 1171 |
| Karl-Anthony Towns | MIN | 1012 |
| Dwight Howard | CHO | 1012 |
SELECT PlayerName, College, DraftYear
FROM Players
WHERE UPPER(LEFT(College,7)) = 'GEORGIA';
-- unnecessary use of functions
-- on a filtering column
| PlayerName | College | DraftYear |
|---|---|---|
| Damien Wilkins | Georgia | |
| Derrick Favors | Georgia Tech | 2010 |
| Iman Shumpert | Georgia Tech | 2011 |
| R.J. Hunter | Georgia State | 2015 |
| ... | ... | ... |
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