Filtrování pomocí WHERE

Improving Query Performance in SQL Server

Dean Smith

Founder, Atamai Analytics

Jak funguje WHERE

SELECT *
FROM PlayerStats
WHERE Position = 'SG'

Tabulka PlayerStats

Improving Query Performance in SQL Server

Jak funguje WHERE

SELECT *
FROM PlayerStats
WHERE Position = 'SG'

Filtrovaná tabulka PlayerStats

Improving Query Performance in SQL Server

Pořadí zpracování WHERE

SELECT PlayerName, 
      Team, 
      (DRebound+ORebound) AS TotalRebounds
FROM PlayerStats
WHERE TotalRebounds >= 1000
ORDER BY TotalRebounds DESC;
  • WHERE se zpracovává před SELECT
-- ERROR
Invalid column name 'TotalRebounds'.
Improving Query Performance in SQL Server

Použití poddotazu

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;
Improving Query Performance in SQL Server

Použití poddotazu

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
Improving Query Performance in SQL Server

Výpočty na sloupcích

SELECT PlayerName, 
       Team, 
       (DRebound+ORebound) AS TotalRebounds
FROM PlayerStats
WHERE (DRebound+ORebound) >= 1000
ORDER BY TotalRebounds DESC;
  • Výpočty na sloupcích v podmínce filtru WHERE mohou prodloužit dobu dotazu
PlayerName Team TotalRebounds
Andre Drummond DET 1247
DeAndre Jordan LAC 1171
Karl-Anthony Towns MIN 1012
Dwight Howard CHO 1012
Improving Query Performance in SQL Server

Funkce na sloupcích

SELECT PlayerName, College, DraftYear 
FROM Players
WHERE UPPER(LEFT(College,7)) = 'GEORGIA'; 
-- unnecessary use of functions 
-- on a filtering column
  • Použití funkcí na sloupcích v podmínce filtru WHERE může prodloužit dobu dotazu
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

Zjednodušení WHERE

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

Shrnutí

  • WHERE se zpracovává před SELECT
  • Výpočty na sloupcích v podmínce filtru WHERE mohou prodloužit dobu dotazu
  • Použití funkcí na sloupcích v podmínce filtru WHERE může prodloužit dobu dotazu
Improving Query Performance in SQL Server

Vyzkoušejte si to!

Improving Query Performance in SQL Server

Preparing Video For Download...