Interogasi setelah SELECT

Meningkatkan Kinerja Kueri di SQL Server

Dean Smith

Founder, Atamai Analytics

Urutan pemrosesan setelah SELECT

1.  FROM

2. ON
3. JOIN
4. WHERE
5. GROUP BY
6. HAVING
7. SELECT
8. DISTINCT
9. ORDER BY
10. TOP
Meningkatkan Kinerja Kueri di SQL Server

Semua tidak selalu baik

SELECT *
FROM PlayerStats;

SELECT * bagus untuk interogasi data tetapi bisa buruk untuk performa

PlayerName Team Position ... ...
Alex Abrines OKC SG ... ...
Quincy Acy BRK PF ... ...
Steven Adams OKC C ... ...
Bam Adebayo MIA C ... ...
... ... ... ... ...
Meningkatkan Kinerja Kueri di SQL Server

Yang dibutuhkan saja lebih baik

SELECT PlayerName, 
        Team, 
        TotalPoints
FROM PlayerStats;

Pilih hanya kolom yang diperlukan

PlayerName Team TotalPoints
Alex Abrines OKC 353
Quincy Acy BRK 411
Steven Adams OKC 1056
Bam Adebayo MIA 477
... ... ...
Meningkatkan Kinerja Kueri di SQL Server

Semua dalam JOIN

SELECT * 
FROM Players p
  INNER JOIN PlayerStats ps
  ON ps.PlayerName = p.PlayerName;

SELECT * pada join mengembalikan duplikat kolom penghubung

PlayerName Age PlayerName Team ...
Alex Abrines 24 Alex Abrines OKC ...
Quincy Acy 27 Quincy Acy BRK ...
Steven Adams 24 Steven Adams OKC ...
Bam Adebayo 20 Bam Adebayo MIA ...
... ... ... ... ...
Meningkatkan Kinerja Kueri di SQL Server

Semua yang Anda perlu di JOIN

SELECT p.PlayerName, 
       ps.Team, 
       p.Country, 
       ps.Position
FROM Players p
  INNER JOIN PlayerStats ps
  ON ps.PlayerName = p.PlayerName;

Sebutkan kolom yang dikembalikan dan dari tabel mana

PlayerName Country Team Position
Alex Abrines Spain OKC SG
Quincy Acy USA BRK PF
Steven Adams New Zealand OKC C
Bam Adebayo USA MIA C
... ... ... ...
Meningkatkan Kinerja Kueri di SQL Server

Baris di TOP

SELECT TOP 5 PlayerName, 
        Team, 
        TotalPoints
FROM PlayerStats;
PlayerName Team TotalPoints
Alex Abrines OKC 353
Quincy Acy BRK 411
Steven Adams OKC 1056
Bam Adebayo MIA 477
Arron Afflalo ORL 179
Meningkatkan Kinerja Kueri di SQL Server

Persentase di TOP

SELECT TOP 1 PERCENT PlayerName, 
        Team, 
        TotalPoints
FROM PlayerStats;
PlayerName Team TotalPoints
Alex Abrines OKC 353
Quincy Acy BRK 411
Steven Adams OKC 1056
Bam Adebayo MIA 477
Arron Afflalo ORL 179
Cole Aldrich MIN 12
LaMarcus Aldridge SAS 1735
Meningkatkan Kinerja Kueri di SQL Server

Tidak ada atas atau bawah

SELECT TOP 5 PlayerName, 
        Team, 
        TotalPoints
FROM PlayerStats
ORDER BY TotalPoints DESC
PlayerName Team TotalPoints
LeBron James CLE 2251
James Harden HOU 2191
Anthony Davis NOP 2110
Russell Westbrook OKC 2028
Giannis Antetokounmpo MIL 2014
Meningkatkan Kinerja Kueri di SQL Server

Pembatas baris lainnya

Pembatas baris Klausa pengurutan Basis data
TOP ORDER BY Microsoft SQL Server
ROWNUM ORDER BY Oracle
LIMIT ORDER BY PostgreSQL
Meningkatkan Kinerja Kueri di SQL Server

Kapan gunakan ORDER BY

Gunakan ORDER BY dalam kueri:

  • Untuk menginterogasi data
  • Saat ada alasan kuat hasil akhir perlu diurutkan
Meningkatkan Kinerja Kueri di SQL Server

Ayo berlatih!

Meningkatkan Kinerja Kueri di SQL Server

Preparing Video For Download...