Filtrare con WHERE

Migliorare le prestazioni delle query in SQL Server

Dean Smith

Founder, Atamai Analytics

Come funziona WHERE

SELECT *
FROM PlayerStats
WHERE Position = 'SG'

Tabella PlayerStats

Migliorare le prestazioni delle query in SQL Server

Come funziona WHERE

SELECT *
FROM PlayerStats
WHERE Position = 'SG'

Tabella PlayerStats filtrata

Migliorare le prestazioni delle query in SQL Server

Ordine di esecuzione di WHERE

SELECT PlayerName, 
      Team, 
      (DRebound+ORebound) AS TotalRebounds
FROM PlayerStats
WHERE TotalRebounds >= 1000
ORDER BY TotalRebounds DESC;
  • WHERE viene eseguito prima di SELECT
-- ERROR
Invalid column name 'TotalRebounds'.
Migliorare le prestazioni delle query in SQL Server

Uso di una sottoquery

SELECT PlayerName, 
       Team, 
       TotalRebounds
FROM
     -- Inizio sottoquery tr
    (SELECT PlayerName, Team, 
             (DRebound+ORebound) AS TotalRebounds
     FROM PlayerStats) tr
WHERE TotalRebounds >= 1000 -- creata nella sottoquery
ORDER BY TotalRebounds DESC;
Migliorare le prestazioni delle query in SQL Server

Uso di una sottoquery

SELECT PlayerName, 
       Team, 
       TotalRebounds
FROM
     -- Inizio sottoquery tr
    (SELECT PlayerName, Team, 
             (DRebound+ORebound) AS TotalRebounds
     FROM PlayerStats) tr
WHERE TotalRebounds >= 1000 -- creata nella sottoquery
ORDER BY TotalRebounds DESC;
PlayerName Team TotalRebounds
Andre Drummond DET 1247
DeAndre Jordan LAC 1171
Karl-Anthony Towns MIN 1012
Dwight Howard CHO 1012
Migliorare le prestazioni delle query in SQL Server

Calcoli sulle colonne

SELECT PlayerName, 
       Team, 
       (DRebound+ORebound) AS TotalRebounds
FROM PlayerStats
WHERE (DRebound+ORebound) >= 1000
ORDER BY TotalRebounds DESC;
  • Le operazioni sulle colonne nella condizione di filtro WHERE possono aumentare i tempi di esecuzione
PlayerName Team TotalRebounds
Andre Drummond DET 1247
DeAndre Jordan LAC 1171
Karl-Anthony Towns MIN 1012
Dwight Howard CHO 1012
Migliorare le prestazioni delle query in SQL Server

Funzioni sulle colonne

SELECT PlayerName, College, DraftYear 
FROM Players
WHERE UPPER(LEFT(College,7)) = 'GEORGIA'; 
-- uso non necessario di funzioni 
-- su una colonna di filtro
  • Applicare funzioni alle colonne nella condizione di filtro WHERE può aumentare i tempi di esecuzione
PlayerName College DraftYear
Damien Wilkins Georgia
Derrick Favors Georgia Tech 2010
Iman Shumpert Georgia Tech 2011
R.J. Hunter Georgia State 2015
... ... ...
Migliorare le prestazioni delle query in SQL Server

WHERE semplificato

SELECT PlayerName, College, DraftYear 
FROM Players 
        -- Nessun calcolo o funzione
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
... ... ...
Migliorare le prestazioni delle query in SQL Server

Riepilogo

  • WHERE viene eseguito prima di SELECT
  • Le operazioni sulle colonne nella condizione di filtro WHERE possono aumentare i tempi di esecuzione
  • Applicare funzioni alle colonne nella condizione di filtro WHERE può aumentare i tempi di esecuzione
Migliorare le prestazioni delle query in SQL Server

Passiamo alla pratica !

Migliorare le prestazioni delle query in SQL Server

Preparing Video For Download...