Cải thiện hiệu năng truy vấn trong PostgreSQL
Amy McCarty
Instructor
| PEMDAS | BODMAS |
|---|---|
| Parenthesis | Brackets |
| Exponents | Order |
| Multiplication /Division | Division /Multiplication |
| Addition /Subtraction | Addition /Subtraction |
$$ x = 2 + (8 + 4) \div 2 $$
$$ x = 10 + 4) \div 2 $$
$$ x = 14 \div 2 $$
$$ x = 7 $$
$$ x = 2 + (8 + 4) \div 2 $$
$$ x = 2 + \frac{12}{2} $$
$$ x = 2 + 6 $$
$$ x = 8 $$
| Thứ tự | Mệnh đề | Mục đích |
|---|---|---|
| 1 | FROM | Chỉ định bảng hoặc các bảng nếu có JOIN |
| 2 | WHERE | Lọc/giới hạn bản ghi |
| 3 | GROUP BY | Nhóm bản ghi theo loại |
| 4 | SUM(), COUNT(), v.v. | Tổng hợp |
| 5 | SELECT | chọn cột trả về |
SELECT COUNT(*) FROM tableA WHERE col1 = 77
| event_location | storm | elements | days |
|---|---|---|---|
| Russia | blizzard | water | 1 |
| Argentina | tornado | water | 1 |
| Argentina | tornado | wind | 1 |
| Australia | tornado | wind | 1 |
| Kuwait | haboob | wind | 2 |
| USA | haboob | wind | 2 |
SELECT elements, storm, COUNT(*)
FROM weather_events
GROUP BY elements
No output -
your code generated an error
column "storm" must appear in the
GROUP BY clause or be used in an
aggregate function
SELECT elements, storm, COUNT(*)
FROM weather_events
GROUP BY elements
| thứ tự | mệnh đề SQL | cột khả dụng |
|---|---|---|
| 1 | FROM weather_events | tất cả |
| 3 | GROUP BY elements | elements |
| 4 | COUNT | elements |
SELECT elements, storm, COUNT(*)
FROM weather_events
GROUP BY elements, storm
SELECT elements, storm, COUNT(*)
FROM weather_events
GROUP BY elements, storm
Kết quả
| elements | storm | count |
|---|---|---|
| water | blizzard | 1 |
| water | tornado | 1 |
| wind | tornado | 2 |
| wind | haboob | 2 |
| Thứ tự | Mệnh đề | Mục đích |
|---|---|---|
| ... | ||
| 5 | SELECT | chọn cột trả về |
| 6 | DISTINCT | loại trùng lặp |
| 7 | ORDER BY | sắp xếp kết quả |
| 8 | LIMIT | giới hạn số hàng |
| row_no | location | storm | elements | days |
|---|---|---|---|---|
| 1 | Russia | blizzard | water | 1 |
| 2 | Argentina | tornado | water | 1 |
| 3 | Argentina | tornado | wind | 1 |
| 4 | Australia | tornado | wind | 1 |
| 5 | Kuwait | haboob | wind | 2 |
| 6 | USA | haboob | wind | 2 |
SELECT DISTINCT storm, elements
FROM weather_events
ORDER BY storm LIMIT 3
| row_no | location | storm | elements | days |
|---|---|---|---|---|
| 1 | Russia | blizzard | water | 1 |
| 2 | Argentina | tornado | water | 1 |
| 3 | Argentina | tornado | wind | 1 |
| 4 | Australia | tornado | wind | 1 |
| 5 | Kuwait | haboob | wind | 2 |
| 6 | USA | haboob | wind | 2 |
SELECT DISTINCT storm, elements
FROM weather_events
ORDER BY storm LIMIT 3
| row_no | location | storm | elements | days |
|---|---|---|---|---|
| 1 | Russia | blizzard | water | 1 |
| 2 | Argentina | tornado | water | 1 |
| 3 | Argentina | tornado | wind | 1 |
| 4 | Australia | tornado | wind | 1 |
| 5 | Kuwait | haboob | wind | 2 |
| 6 | USA | haboob | wind | 2 |
SELECT DISTINCT storm, elements
FROM weather_events
ORDER BY storm LIMIT 3
| thứ tự | mệnh đề SQL | hàng khả dụng |
|---|---|---|
| 1 | FROM weather_events | tất cả |
| row_no | storm | elements |
|---|---|---|
| 1 | blizzard | water |
| 2 | tornado | water |
| 3 | tornado | wind |
| 5 | haboob | wind |
SELECT DISTINCT storm, elements
FROM weather_events
ORDER BY storm LIMIT 3
| thứ tự | mệnh đề SQL | hàng khả dụng |
|---|---|---|
| 1 | FROM weather_events | tất cả |
| 5 | SELECT storm, elements | tất cả |
| row_no | storm | elements |
|---|---|---|
| 1 | blizzard | water |
| 2 | tornado | water |
| 3 | tornado | wind |
| 5 | haboob | wind |
SELECT DISTINCT storm, elements
FROM weather_events
ORDER BY storm LIMIT 3
| thứ tự | mệnh đề SQL | hàng khả dụng |
|---|---|---|
| 1 | FROM weather_events | tất cả |
| 5 | SELECT storm, elements | tất cả |
| 6 | DISTINCT | 1, 2, 3, 5 |
| row_no | storm | elements |
|---|---|---|
| 1 | blizzard | water |
| 5 | haboob | wind |
| 2 | tornado | water |
| 3 | tornado | wind |
SELECT DISTINCT storm, elements
FROM weather_events
ORDER BY storm LIMIT 3
| thứ tự | mệnh đề SQL | hàng khả dụng |
|---|---|---|
| 1 | FROM weather_events | tất cả |
| 5 | SELECT storm, elements | tất cả |
| 6 | DISTINCT | 1, 2, 3, 5 |
| 7 | ORDER BY storm | 1, 5, 2, 3 |
| row_no | storm | elements |
|---|---|---|
| 1 | blizzard | water |
| 5 | haboob | wind |
| 2 | tornado | water |
SELECT DISTINCT storm, elements
FROM weather_events
ORDER BY storm LIMIT 3
| thứ tự | mệnh đề SQL | hàng khả dụng |
|---|---|---|
| 1 | FROM weather_events | tất cả |
| 5 | SELECT storm, elements | tất cả |
| 6 | DISTINCT | 1, 2, 3, 5 |
| 7 | ORDER BY storm | 1, 5, 2, 3 |
| 8 | LIMIT 3 | 1, 5, 2 |
| Thứ tự | Mệnh đề | Mục đích | Giới hạn |
|---|---|---|---|
| 1 | FROM | tham chiếu bảng | |
| 2 | WHERE | lọc/giới hạn bản ghi | # hàng |
| 3 | GROUP BY | nhóm bản ghi theo loại | # cột |
| 4 | SUM, COUNT, v.v. | tổng hợp | # hàng |
| 5 | SELECT | chọn cột trả về | # cột |
| 6 | DISTINCT | loại trùng lặp | # hàng |
| 7 | ORDER BY | sắp xếp kết quả | |
| 8 | LIMIT | lọc bản ghi | # hàng |
Cải thiện hiệu năng truy vấn trong PostgreSQL