Wprowadzenie

Poprawa wydajności zapytań w SQL Server

Dean Smith

Founder, Atamai Analytics

Baza danych Earthquakes

Baza danych Earthquakes

Poprawa wydajności zapytań w SQL Server

Baza danych NBA Season 2017-2018

Baza danych NBA Season 2017-2018

Poprawa wydajności zapytań w SQL Server

Baza danych Customer Orders

Baza danych Customer Orders

Poprawa wydajności zapytań w SQL Server

Czy to jest czytelne?

Select ps.Team, count(p.PlayerName) 
As NonNthAmerPlayers from 
 PlayerStats ps inner 
join (select PlayerName FROM Players 
    WHERE Country <> 'USA' Or Country 
 <> 'Canada' ) 
 p on p.PlayerName = ps.PlayerName 
 group BY ps.Team
having Count(p.PlayerName) 
>=24 Order by NonNthAmerPlayers desc
Team NonNthAmerPlayers
HOU 24
LAL 24
MEM 24
MIL 24
Poprawa wydajności zapytań w SQL Server

Zalecenia

  • Zachowaj spójność
  • Używaj WIELKICH LITER dla całej składni SQL
  • Każde główne słowo kluczowe w nowej linii: SELECT, FROM, WHERE itp.
  • Stosuj wcięcia:
    • Podzapytania
    • Wyrażenia ON
    • Warunki AND/OR
    • Aby unikać długich linii, np. przy wielu nazwach kolumn
  • Zakończ zapytanie średnikiem (;)
  • Używaj aliasów tam, gdzie jest to wymagane — za pomocą AS
Poprawa wydajności zapytań w SQL Server

Znacznie lepiej...

Przed

Select ps.Team, count(p.PlayerName) 
As NonNthAmerPlayers from 
 PlayerStats ps inner 
join (select PlayerName FROM Players 
    WHERE Country <> 'USA' Or Country 
 <> 'Canada' ) 
 p on p.PlayerName = ps.PlayerName 
 group BY ps.Team
having Count(p.PlayerName) 
>=24 Order by NonNthAmerPlayers desc

Po

SELECT ps.Team, 
    COUNT(p.PlayerName) NonNthAmerPlayers
FROM PlayerStats ps
INNER JOIN
        (SELECT PlayerName 
         FROM Players 
         WHERE Country <> 'USA'
                OR Country <> 'Canada' ) p
    ON p.PlayerName = ps.PlayerName
GROUP BY ps.Team
HAVING COUNT(p.PlayerName) >=24
ORDER BY NonNthAmerPlayers DESC;
Poprawa wydajności zapytań w SQL Server

Komentowanie bloków

/* 
Returns a list of NBA teams with 24 or more non-North 
American players on the team roster.
*/

SELECT ps.Team, COUNT(p.PlayerName) NonNthAmerPlayers FROM PlayerStats ps INNER JOIN (SELECT PlayerName FROM Players WHERE Country <> 'USA' OR Country <> 'Canada' ) p ON p.PlayerName = ps.PlayerName GROUP BY ps.Team HAVING COUNT(p.PlayerName) >=24 ORDER BY NonNthAmerPlayers DESC;

Użyj /* i */, aby zakomentować blok kodu lub tekstu

Poprawa wydajności zapytań w SQL Server

Komentowanie bloków

/* 
Returns a list of NBA teams with 24 or more non-North 
American players on the team roster.
*/

SELECT ps.Team, COUNT(p.PlayerName) NonNthAmerPlayers FROM PlayerStats ps INNER JOIN (SELECT PlayerName FROM Players WHERE Country <> 'USA' OR Country <> 'Canada' ) p ON p.PlayerName = ps.PlayerName GROUP BY ps.Team HAVING COUNT(p.PlayerName) >=24 ORDER BY NonNthAmerPlayers DESC;

Użyj /* i */, aby zakomentować blok kodu lub tekstu

Team NonNthAmerPlayers
HOU 24
LAL 24
MEM 24
MIL 24
Poprawa wydajności zapytań w SQL Server

Komentowanie linii

Użyj --, aby zakomentować pojedynczą linię kodu lub tekstu

SELECT ps.Team, 
  COUNT(p.PlayerName) NonNthAmerPlayers
FROM PlayerStats ps

INNER JOIN 
    (SELECT PlayerName 
     FROM Players 
     WHERE Country <> 'USA'
        OR Country <> 'Canada' ) p
    ON p.PlayerName = ps.PlayerName
GROUP BY ps.Team
HAVING COUNT(p.PlayerName) >=24

ORDER BY NonNthAmerPlayers DESC;
Poprawa wydajności zapytań w SQL Server

Komentowanie linii

Użyj --, aby zakomentować pojedynczą linię kodu lub tekstu

SELECT ps.Team, 
  COUNT(p.PlayerName) NonNthAmerPlayers -- Count of players
FROM PlayerStats ps

INNER JOIN 
    (SELECT PlayerName 
     FROM Players 
     WHERE Country <> 'USA'
        OR Country <> 'Canada' ) p -- Indented qub-suery
    ON p.PlayerName = ps.PlayerName
GROUP BY ps.Team
HAVING COUNT(p.PlayerName) >=24

ORDER BY NonNthAmerPlayers DESC;

 

  • Komentarz informujący, że nowa kolumna zawiera liczbę graczy

 

  • Komentarz informujący, że podzapytanie jest wcięte
Poprawa wydajności zapytań w SQL Server

Komentowanie linii

Użyj --, aby zakomentować pojedynczą linię kodu lub tekstu

SELECT ps.Team, 
  COUNT(p.PlayerName) NonNthAmerPlayers -- Count of players
FROM PlayerStats ps
-- Inner join starts here
INNER JOIN 
    (SELECT PlayerName 
     FROM Players 
     WHERE Country <> 'USA'
        OR Country <> 'Canada' ) p -- Indented qub-suery
    ON p.PlayerName = ps.PlayerName
GROUP BY ps.Team
HAVING COUNT(p.PlayerName) >=24
-- Remove the ORDER BY, it is not required
ORDER BY NonNthAmerPlayers DESC;

 

 

  • Komentarz oznaczający przejście do INNER JOIN

 

 

  • Komentarz dotyczący konieczności użycia ORDER BY
Poprawa wydajności zapytań w SQL Server

Komentowanie linii

Użyj --, aby zakomentować pojedynczą linię kodu lub tekstu

SELECT ps.Team, 
  COUNT(p.PlayerName) NonNthAmerPlayers -- Count of players
FROM PlayerStats ps
-- Inner join starts here
INNER JOIN 
    (SELECT PlayerName 
     FROM Players 
     WHERE Country <> 'USA'
        OR Country <> 'Canada' ) p -- Indented qub-suery
    ON p.PlayerName = ps.PlayerName
GROUP BY ps.Team
HAVING COUNT(p.PlayerName) >=24;
-- Remove the ORDER BY, it is not required
-- ORDER BY NonNthAmerPlayers DESC

 

 

 

 

 

 

  • Zakomentowane wyrażenie ORDER BY
Poprawa wydajności zapytań w SQL Server

Lass uns üben!

Poprawa wydajności zapytań w SQL Server

Preparing Video For Download...