Giới thiệu

Cải thiện hiệu năng truy vấn trong SQL Server

Dean Smith

Founder, Atamai Analytics

Cơ sở dữ liệu Động đất

Cơ sở dữ liệu Động đất

Cải thiện hiệu năng truy vấn trong SQL Server

Cơ sở dữ liệu NBA Mùa 2017-2018

Cơ sở dữ liệu NBA Mùa 2017-2018

Cải thiện hiệu năng truy vấn trong SQL Server

Cơ sở dữ liệu Đơn hàng Khách hàng

Cơ sở dữ liệu Đơn hàng Khách hàng

Cải thiện hiệu năng truy vấn trong SQL Server

Dễ đọc không?

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
Cải thiện hiệu năng truy vấn trong SQL Server

Gợi ý

  • Hãy nhất quán
  • Viết HOA toàn bộ cú pháp SQL
  • Xuống dòng cho mỗi mệnh đề chính: SELECT, FROM, WHERE, ...
  • Thụt lề mã:
    • Truy vấn con
    • Mệnh đề ON
    • Điều kiện AND/OR
    • Tránh dòng quá dài, ví dụ liệt kê nhiều cột
  • Kết thúc truy vấn bằng dấu chấm phẩy (;)
  • Đặt bí danh khi cần với AS
Cải thiện hiệu năng truy vấn trong SQL Server

Tốt hơn nhiều...

Từ

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

Thành

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;
Cải thiện hiệu năng truy vấn trong SQL Server

Chú thích khối

/* 
Trả về danh sách đội NBA có từ 24 cầu thủ 
không thuộc Bắc Mỹ trở lên trong danh sách.
*/

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;

Dùng /**/ để chú thích một khối mã hoặc văn bản

Cải thiện hiệu năng truy vấn trong SQL Server

Chú thích khối

/* 
Trả về danh sách đội NBA có từ 24 cầu thủ 
không thuộc Bắc Mỹ trở lên trong danh sách.
*/

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;

Dùng /**/ để chú thích một khối mã hoặc văn bản

Team NonNthAmerPlayers
HOU 24
LAL 24
MEM 24
MIL 24
Cải thiện hiệu năng truy vấn trong SQL Server

Chú thích dòng

Dùng -- để chú thích một dòng mã hoặc văn bản

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;
Cải thiện hiệu năng truy vấn trong SQL Server

Chú thích dòng

Dùng -- để chú thích một dòng mã hoặc văn bản

SELECT ps.Team, 
  COUNT(p.PlayerName) NonNthAmerPlayers -- Số lượng cầu thủ
FROM PlayerStats ps

INNER JOIN 
    (SELECT PlayerName 
     FROM Players 
     WHERE Country <> 'USA'
        OR Country <> 'Canada' ) p -- Thụt lề truy vấn con
    ON p.PlayerName = ps.PlayerName
GROUP BY ps.Team
HAVING COUNT(p.PlayerName) >=24

ORDER BY NonNthAmerPlayers DESC;

 

  • Ghi chú cho biết cột mới là số lượng cầu thủ

 

  • Ghi chú cho biết truy vấn con được thụt lề
Cải thiện hiệu năng truy vấn trong SQL Server

Chú thích dòng

Dùng -- để chú thích một dòng mã hoặc văn bản

SELECT ps.Team, 
  COUNT(p.PlayerName) NonNthAmerPlayers -- Số lượng cầu thủ
FROM PlayerStats ps
-- Inner join bắt đầu tại đây
INNER JOIN 
    (SELECT PlayerName 
     FROM Players 
     WHERE Country <> 'USA'
        OR Country <> 'Canada' ) p -- Thụt lề truy vấn con
    ON p.PlayerName = ps.PlayerName
GROUP BY ps.Team
HAVING COUNT(p.PlayerName) >=24
-- Bỏ ORDER BY, không bắt buộc
ORDER BY NonNthAmerPlayers DESC;

 

 

  • Ghi chú đánh dấu ngắt trước INNER JOIN

 

 

  • Ghi chú về yêu cầu của ORDER BY
Cải thiện hiệu năng truy vấn trong SQL Server

Chú thích dòng

Dùng -- để chú thích một dòng mã hoặc văn bản

SELECT ps.Team, 
  COUNT(p.PlayerName) NonNthAmerPlayers -- Số lượng cầu thủ
FROM PlayerStats ps
-- Inner join bắt đầu tại đây
INNER JOIN 
    (SELECT PlayerName 
     FROM Players 
     WHERE Country <> 'USA'
        OR Country <> 'Canada' ) p -- Thụt lề truy vấn con
    ON p.PlayerName = ps.PlayerName
GROUP BY ps.Team
HAVING COUNT(p.PlayerName) >=24;
-- Bỏ ORDER BY, không bắt buộc
-- ORDER BY NonNthAmerPlayers DESC

 

 

 

 

 

 

  • Đã chú thích mệnh đề ORDER BY
Cải thiện hiệu năng truy vấn trong SQL Server

Ayo berlatih!

Cải thiện hiệu năng truy vấn trong SQL Server

Preparing Video For Download...