Gestire i duplicati

Migliorare le prestazioni delle query in SQL Server

Dean Smith

Founder, Atamai Analytics

Query che restituisce duplicati

SELECT PlayerName,
       Team
FROM PlayerStats; 
PlayerName Team
... ...
Emmanuel Mudiay DEN
Emmanuel Mudiay NYK
Enes Kanter NYK
Eric Bledsoe PHO
Eric Bledsoe MIL
... ...
Migliorare le prestazioni delle query in SQL Server

Rimuovere duplicati con DISTINCT()

SELECT DISTINCT(PlayerName)
FROM PlayerStats;
PlayerName
...
Emmanuel Mudiay
Enes Kanter
Eric Bledsoe
Eric Gordon
Eric Moreland
...
Migliorare le prestazioni delle query in SQL Server

GROUP BY invece di DISTINCT()

SELECT DISTINCT(PlayerName)
FROM PlayerStats;
SELECT PlayerName
FROM PlayerStats 
GROUP BY PlayerName;
Migliorare le prestazioni delle query in SQL Server

GROUP BY invece di DISTINCT()

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
... ...
Migliorare le prestazioni delle query in SQL Server

C'è un altro modo?

SELECT PlayerName
FROM Players;
PlayerName
...
Emmanuel Mudiay
Enes Kanter
Eric Bledsoe
Eric Gordon
Eric Moreland
...
Migliorare le prestazioni delle query in SQL Server

Tabelle Fruits

Fruits1

FruitName FruitType
Grapefruit Citrus
Orange Citrus
Peach Stone
Strawberry Berry

Fruits2

FruitName FruitType
Marionberry Berry
Orange Citrus
Plum Stone
Strawberry Berry
Migliorare le prestazioni delle query in SQL Server

Frutti duplicati

 

 

 

  • Orange e Strawberry sono duplicati

Fruits1 aggiunta a Fruits2

FruitName FruitType
... ...
Orange Citrus
Orange Citrus
Plum Stone
Strawberry Berry
Strawberry Berry
Migliorare le prestazioni delle query in SQL Server

Rimuovere duplicati con UNION

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
Migliorare le prestazioni delle query in SQL Server

E UNION ALL?

 

  • UNION ALL
    • accoda righe da una o più tabelle e non rimuove i duplicati

 

  • UNION
    • accoda righe da una o più tabelle e rimuove i duplicati
Migliorare le prestazioni delle query in SQL Server

DISTINCT() e UNION

  • Usalo con cautela
  • Può usare un ordinamento interno per controllare i duplicati
  • Può aumentare il tempo di esecuzione della query
Migliorare le prestazioni delle query in SQL Server

Usare DISTINCT()

Prima di usare DISTINCT() chiediti:

  • C'è un metodo alternativo?
    • usa invece una tabella con chiave univoca
  • La query usa una funzione di aggregazione?
    • raggruppa con GROUP BY
Migliorare le prestazioni delle query in SQL Server

Usare UNION

Prima di usare UNION chiediti:

  • I duplicati vanno bene?
  • Unire le query produrrà duplicati?
  • Valuta UNION ALL se i duplicati vanno bene o se non se ne creano
Migliorare le prestazioni delle query in SQL Server

Passons à la pratique !

Migliorare le prestazioni delle query in SQL Server

Preparing Video For Download...