บทนำ

การปรับปรุงประสิทธิภาพคิวรีใน SQL Server

Dean Smith

Founder, Atamai Analytics

ฐานข้อมูลแผ่นดินไหว

ฐานข้อมูลแผ่นดินไหว

การปรับปรุงประสิทธิภาพคิวรีใน SQL Server

ฐานข้อมูล NBA ฤดูกาล 2017-2018

ฐานข้อมูล NBA ฤดูกาล 2017-2018

การปรับปรุงประสิทธิภาพคิวรีใน SQL Server

ฐานข้อมูลคำสั่งซื้อของลูกค้า

ฐานข้อมูลคำสั่งซื้อของลูกค้า

การปรับปรุงประสิทธิภาพคิวรีใน SQL Server

อ่านง่ายไหม?

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
การปรับปรุงประสิทธิภาพคิวรีใน SQL Server

คำแนะนำ

  • ใช้รูปแบบให้สม่ำเสมอ
  • ใช้ตัวพิมพ์ใหญ่สำหรับ SQL syntax ทั้งหมด
  • ขึ้นบรรทัดใหม่สำหรับแต่ละคำสั่งหลัก: SELECT, FROM, WHERE เป็นต้น
  • เยื้องโค้ด:
    • Sub-queries
    • คำสั่ง ON
    • เงื่อนไข AND/OR
    • เพื่อหลีกเลี่ยงบรรทัดที่ยาวเกินไป เช่น การระบุชื่อคอลัมน์หลายตัว
  • ปิดท้ายคิวรีด้วยเซมิโคลอน (;)
  • ตั้งชื่อแทน (alias) เมื่อจำเป็น โดยใช้ AS
การปรับปรุงประสิทธิภาพคิวรีใน SQL Server

ดีขึ้นมาก...

จาก

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

เป็น

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;
การปรับปรุงประสิทธิภาพคิวรีใน SQL Server

การคอมเมนต์แบบบล็อก

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

ใช้ /* และ */ เพื่อคอมเมนต์โค้ดหรือข้อความทั้งบล็อก

การปรับปรุงประสิทธิภาพคิวรีใน SQL Server

การคอมเมนต์แบบบล็อก

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

ใช้ /* และ */ เพื่อคอมเมนต์โค้ดหรือข้อความทั้งบล็อก

Team NonNthAmerPlayers
HOU 24
LAL 24
MEM 24
MIL 24
การปรับปรุงประสิทธิภาพคิวรีใน SQL Server

การคอมเมนต์แบบบรรทัด

ใช้ -- เพื่อคอมเมนต์โค้ดหรือข้อความทีละบรรทัด

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;
การปรับปรุงประสิทธิภาพคิวรีใน SQL Server

การคอมเมนต์แบบบรรทัด

ใช้ -- เพื่อคอมเมนต์โค้ดหรือข้อความทีละบรรทัด

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;

 

  • คอมเมนต์ระบุว่าคอลัมน์ใหม่คือจำนวนผู้เล่น

 

  • คอมเมนต์ระบุว่า sub-query มีการเยื้องโค้ด
การปรับปรุงประสิทธิภาพคิวรีใน SQL Server

การคอมเมนต์แบบบรรทัด

ใช้ -- เพื่อคอมเมนต์โค้ดหรือข้อความทีละบรรทัด

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;

 

 

  • คอมเมนต์ระบุจุดเริ่มต้นของ INNER JOIN

 

 

  • คอมเมนต์เกี่ยวกับความจำเป็นของ ORDER BY
การปรับปรุงประสิทธิภาพคิวรีใน SQL Server

การคอมเมนต์แบบบรรทัด

ใช้ -- เพื่อคอมเมนต์โค้ดหรือข้อความทีละบรรทัด

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

 

 

 

 

 

 

  • คอมเมนต์คำสั่ง ORDER BY ออก
การปรับปรุงประสิทธิภาพคิวรีใน SQL Server

มาฝึกกันเถอะ!

การปรับปรุงประสิทธิภาพคิวรีใน SQL Server

Preparing Video For Download...