Introdução

Melhorando o Desempenho de Consultas no SQL Server

Dean Smith

Founder, Atamai Analytics

Banco de dados de terremotos

Banco de dados de terremotos

Melhorando o Desempenho de Consultas no SQL Server

Banco de dados da NBA 2017–2018

Banco de dados NBA Season 2017-2018

Melhorando o Desempenho de Consultas no SQL Server

Banco de dados de pedidos de clientes

Banco de dados de pedidos de clientes

Melhorando o Desempenho de Consultas no SQL Server

Isso é fácil de ler?

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
Equipe NonNthAmerPlayers
HOU 24
LAL 24
MEM 24
MIL 24
Melhorando o Desempenho de Consultas no SQL Server

Sugestões

  • Seja consistente
  • Use UPPER CASE para toda a sintaxe SQL
  • Quebre linha para cada parte principal: SELECT, FROM, WHERE, etc.
  • Indente o código:
    • Subconsultas
    • Instruções ON
    • Condições AND/OR
    • Para evitar linhas únicas longas, por exemplo, muitos nomes de colunas
  • Termine a consulta com ponto e vírgula (;)
  • Use alias quando precisar, com AS
Melhorando o Desempenho de Consultas no SQL Server

Bem melhor...

De

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

Para

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;
Melhorando o Desempenho de Consultas no SQL Server

Comentando blocos

/* 
Retorna uma lista de equipes da NBA com 24 ou mais jogadores 
fora da América do Norte no elenco.
*/

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;

Use /* e */ para comentar um bloco de código ou texto

Melhorando o Desempenho de Consultas no SQL Server

Comentando blocos

/* 
Retorna uma lista de equipes da NBA com 24 ou mais jogadores 
fora da América do Norte no elenco.
*/

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;

Use /* e */ para comentar um bloco de código ou texto

Equipe NonNthAmerPlayers
HOU 24
LAL 24
MEM 24
MIL 24
Melhorando o Desempenho de Consultas no SQL Server

Comentando linhas

Use -- para comentar uma única linha de código ou texto

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;
Melhorando o Desempenho de Consultas no SQL Server

Comentando linhas

Use -- para comentar uma única linha de código ou texto

SELECT ps.Team, 
  COUNT(p.PlayerName) NonNthAmerPlayers -- Contagem de jogadores
FROM PlayerStats ps

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

ORDER BY NonNthAmerPlayers DESC;

 

  • Comentário indicando que a nova coluna é a contagem de jogadores

 

  • Comentário indicando que a subconsulta está indentada
Melhorando o Desempenho de Consultas no SQL Server

Comentando linhas

Use -- para comentar uma única linha de código ou texto

SELECT ps.Team, 
  COUNT(p.PlayerName) NonNthAmerPlayers -- Contagem de jogadores
FROM PlayerStats ps
-- Inner join começa aqui
INNER JOIN 
    (SELECT PlayerName 
     FROM Players 
     WHERE Country <> 'USA'
        OR Country <> 'Canada' ) p -- Subconsulta indentada
    ON p.PlayerName = ps.PlayerName
GROUP BY ps.Team
HAVING COUNT(p.PlayerName) >=24
-- Remova o ORDER BY, não é obrigatório
ORDER BY NonNthAmerPlayers DESC;

 

 

  • Comentário marcando uma quebra antes de INNER JOIN

 

 

  • Comentário sobre a necessidade de ORDER BY
Melhorando o Desempenho de Consultas no SQL Server

Comentando linhas

Use -- para comentar uma única linha de código ou texto

SELECT ps.Team, 
  COUNT(p.PlayerName) NonNthAmerPlayers -- Contagem de jogadores
FROM PlayerStats ps
-- Inner join começa aqui
INNER JOIN 
    (SELECT PlayerName 
     FROM Players 
     WHERE Country <> 'USA'
        OR Country <> 'Canada' ) p -- Subconsulta indentada
    ON p.PlayerName = ps.PlayerName
GROUP BY ps.Team
HAVING COUNT(p.PlayerName) >=24;
-- Remova o ORDER BY, não é obrigatório
-- ORDER BY NonNthAmerPlayers DESC

 

 

 

 

 

 

  • ORDER BY comentado
Melhorando o Desempenho de Consultas no SQL Server

Vamos praticar!

Melhorando o Desempenho de Consultas no SQL Server

Preparing Video For Download...