Improving Query Performance in SQL Server
Dean Smith
Founder, Atamai Analytics
1. FROM
4. WHERE
6. HAVING
7. SELECT
SELECT Team,
SUM(TotalPoints) AS TotalSGTeamPoints
FROM PlayerStats
WHERE Position = 'SG'
GROUP BY Team
Team | TotalSGTeamPoints |
---|---|
ATL | 2034 |
BOS | 1606 |
BRK | 2126 |
CHI | 2905 |
CHO | 2661 |
CLE | 2489 |
... | ... |
SELECT Team,
SUM(TotalPoints) AS TotalSGTeamPoints
FROM PlayerStats
WHERE Position = 'SG'
GROUP BY Team
SELECT Team,
SUM(TotalPoints) AS TotalSGTeamPoints
FROM PlayerStats
--WHERE Position = 'SG'
GROUP BY Team, Position
HAVING Position = 'SG'
Don't use HAVING to filter individual or ungrouped rows
Team | TotalSGTeamPoints |
---|---|
ATL | 2034 |
BOS | 1606 |
BRK | 2126 |
CHI | 2905 |
CHO | 2661 |
CLE | 2489 |
... | ... |
SELECT
Team,
SUM(DRebound+ORebound) AS TotRebounds,
SUM(DRebound) AS TotDef,
SUM(ORebound) AS TotOff
FROM PlayerStats
GROUP BY Team;
Team | TotRebounds | TotDef | TotOff |
---|---|---|---|
ATL | 3436 | 2693 | 743 |
BOS | 3645 | 2878 | 767 |
BRK | 3644 | 2852 | 792 |
CHI | 3663 | 2873 | 790 |
CHO | 3728 | 2901 | 827 |
CLE | 3455 | 2761 | 694 |
... | ... | ... | ... |
SELECT
Team,
SUM(DRebound+ORebound) AS TotRebounds,
SUM(DRebound) AS TotDef,
SUM(ORebound) AS TotOff
FROM PlayerStats
WHERE ORebound >= 1000
GROUP BY Team;
Team | TotRebounds | TotDef | TotOff |
---|---|---|---|
SELECT
Team,
SUM(DRebound+ORebound) AS TotRebounds,
SUM(DRebound) AS TotDef,
SUM(ORebound) AS TotOff
FROM PlayerStats
GROUP BY Team
HAVING ORebound >= 1000;
------------------------------------------------
-- ERROR
Column 'PlayerStats.ORebound' is invalid in the
HAVING clause because it is not contained in
either an aggregate function or the GROUP BY
clause.
SELECT
Team,
SUM(DRebound+ORebound) AS TotRebounds,
SUM(DRebound) AS TotDef,
SUM(ORebound) AS TotalOff
FROM PlayerStats
GROUP BY Team
-- aggregate function SUM()
HAVING SUM(ORebound) >= 1000;
Team | TotRebounds | TotDef | TotOff |
---|---|---|---|
OKC | 3695 | 2671 | 1024 |
Improving Query Performance in SQL Server