Improving Query Performance in SQL Server
Dean Smith
Founder, Atamai Analytics
SELECT PlayerName,
Team
FROM PlayerStats;
PlayerName | Team |
---|---|
... | ... |
Emmanuel Mudiay | DEN |
Emmanuel Mudiay | NYK |
Enes Kanter | NYK |
Eric Bledsoe | PHO |
Eric Bledsoe | MIL |
... | ... |
SELECT DISTINCT(PlayerName)
FROM PlayerStats;
PlayerName |
---|
... |
Emmanuel Mudiay |
Enes Kanter |
Eric Bledsoe |
Eric Gordon |
Eric Moreland |
... |
SELECT DISTINCT(PlayerName)
FROM PlayerStats;
SELECT PlayerName
FROM PlayerStats
GROUP BY PlayerName;
SELECT DISTINCT(PlayerName)
FROM PlayerStats;
SELECT PlayerName
FROM PlayerStats
GROUP BY PlayerName;
SELECT PlayerName,
COUNT(Team) AS TeamsPlayedFor
FROM PlayerStats
GROUP BY PlayerName;
PlayerName | TeamsPlayedFor |
---|---|
... | ... |
Emmanuel Mudiay | 2 |
Enes Kanter | 1 |
Eric Bledsoe | 2 |
Eric Gordon | 1 |
Eric Moreland | 1 |
... | ... |
SELECT PlayerName
FROM Players;
PlayerName |
---|
... |
Emmanuel Mudiay |
Enes Kanter |
Eric Bledsoe |
Eric Gordon |
Eric Moreland |
... |
Fruits1
FruitName | FruitType |
---|---|
Grapefruit | Citrus |
Orange | Citrus |
Peach | Stone |
Strawberry | Berry |
Fruits2
FruitName | FruitType |
---|---|
Marionberry | Berry |
Orange | Citrus |
Plum | Stone |
Strawberry | Berry |
Orange
and Strawberry
are duplicatedFruits1 appended to Fruits2
FruitName | FruitType |
---|---|
... | ... |
Orange | Citrus |
Orange | Citrus |
Plum | Stone |
Strawberry | Berry |
Strawberry | Berry |
SELECT FruitName, FruitType
FROM Fruits1
UNION
SELECT FruitName, FruitType
FROM Fruits2
FruitName | FruitType |
---|---|
Grapefruit | Citrus |
Orange | Citrus |
Peach | Stone |
Strawberry | Berry |
Marionberry | Berry |
Plum | Stone |
Before using DISTINCT()
we should ask:
GROUP BY
Before using UNION
we should ask:
UNION ALL
if duplicate rows are OK or if no duplicate rows will be createdImproving Query Performance in SQL Server