Úvod

Improving Query Performance in SQL Server

Dean Smith

Founder, Atamai Analytics

Databáze zemětřesení

Databáze zemětřesení

Improving Query Performance in SQL Server

Databáze NBA sezóny 2017–2018

Databáze NBA sezóny 2017–2018

Improving Query Performance in SQL Server

Databáze zákaznických objednávek

Databáze zákaznických objednávek

Improving Query Performance in SQL Server

Je tento kód přehledný?

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

Doporučení

  • Buďte konzistentní
  • Používejte VELKÁ PÍSMENA pro veškerou syntaxi SQL
  • Každou hlavní část dotazu pište na nový řádek: SELECT, FROM, WHERE atd.
  • Odsazujte kód:
    • Poddotazy
    • Klauzule ON
    • Podmínky AND/OR
    • Dlouhé řádky, např. výpis více sloupců
  • Dotaz ukončete středníkem (;)
  • Používejte aliasy pomocí AS
Improving Query Performance in SQL Server

Mnohem lepší...

Původní

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

Upravené

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

Komentování bloků

/* 
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;

Použijte /* a */ pro zakomentování bloku kódu nebo textu

Improving Query Performance in SQL Server

Komentování bloků

/* 
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;

Použijte /* a */ pro zakomentování bloku kódu nebo textu

Team NonNthAmerPlayers
HOU 24
LAL 24
MEM 24
MIL 24
Improving Query Performance in SQL Server

Komentování řádků

Použijte -- pro zakomentování jednoho řádku kódu nebo textu

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

Komentování řádků

Použijte -- pro zakomentování jednoho řádku kódu nebo textu

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;

 

  • Komentář označující, že nový sloupec je počet hráčů

 

  • Komentář označující, že poddotaz je odsazen
Improving Query Performance in SQL Server

Komentování řádků

Použijte -- pro zakomentování jednoho řádku kódu nebo textu

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;

 

 

  • Komentář označující začátek INNER JOIN

 

 

  • Komentář k nutnosti použití ORDER BY
Improving Query Performance in SQL Server

Komentování řádků

Použijte -- pro zakomentování jednoho řádku kódu nebo textu

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

 

 

 

 

 

 

  • Zakomentovaný příkaz ORDER BY
Improving Query Performance in SQL Server

Lass uns üben!

Improving Query Performance in SQL Server

Preparing Video For Download...