Ce scrieți nu este ce vede SQL

Îmbunătățirea performanței interogărilor în PostgreSQL

Amy McCarty

Instructor

Ordinea algebrică a operațiilor

 

  • Lexical (conform scrierii)
  • Logic (conform execuției)

 

PEMDAS BODMAS
Paranteze Brackets
Exponenți Order
Înmulțire /Împărțire Division /Multiplication
Adunare /Scădere Addition /Subtraction
Îmbunătățirea performanței interogărilor în PostgreSQL

Aplicarea ordinii operațiilor

 

Lexical:

$$ x = 2 + (8 + 4) \div 2 $$

$$ x = 10 + 4) \div 2 $$

$$ x = 14 \div 2 $$

$$ x = 7 $$

 

Logic:

$$ x = 2 + (8 + 4) \div 2 $$

$$ x = 2 + \frac{12}{2} $$

$$ x = 2 + 6 $$

$$ x = 8 $$

Îmbunătățirea performanței interogărilor în PostgreSQL

Ordinea logică a operațiilor SQL

Ordine Clauză Scop
1 FROM Indică tabelul sau tabelele (inclusiv join-uri)
2 WHERE Filtrează înregistrările
3 GROUP BY Grupează înregistrările în categorii
4 SUM(), COUNT(), etc Agregări
5 SELECT Identifică coloanele returnate
SELECT COUNT(*) FROM tableA WHERE col1 = 77
Îmbunătățirea performanței interogărilor în PostgreSQL

GROUP BY și agregări

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
Îmbunătățirea performanței interogărilor în PostgreSQL

Ordinea operațiilor pentru GROUP BY și agregări

SELECT elements, storm, COUNT(*)
FROM weather_events
GROUP BY elements
ordine clauză SQL coloane disponibile
1 FROM weather_events toate
3 GROUP BY elements elements
4 COUNT elements
Îmbunătățirea performanței interogărilor în PostgreSQL

GROUP BY corespunde agregărilor

SELECT elements, storm, COUNT(*)
FROM weather_events
GROUP BY elements, storm
Îmbunătățirea performanței interogărilor în PostgreSQL

GROUP BY corespunde agregărilor

SELECT elements, storm, COUNT(*)
FROM weather_events
GROUP BY elements, storm

Rezultate

elements storm count
water blizzard 1
water tornado 1
wind tornado 2
wind haboob 2
Îmbunătățirea performanței interogărilor în PostgreSQL

Ordinea logică a operațiilor SQL (continuare)

Ordine Clauză Scop
...
5 SELECT identifică coloanele returnate
6 DISTINCT elimină duplicatele
7 ORDER BY ordonează rezultatele
8 LIMIT elimină rânduri
Îmbunătățirea performanței interogărilor în PostgreSQL

DISTINCT și LIMIT

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
Îmbunătățirea performanței interogărilor în PostgreSQL
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
Îmbunătățirea performanței interogărilor în PostgreSQL
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
ordine clauză SQL rânduri disponibile
1 FROM weather_events toate
Îmbunătățirea performanței interogărilor în PostgreSQL
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
ordine clauză SQL rânduri disponibile
1 FROM weather_events toate
5 SELECT storm, elements toate
Îmbunătățirea performanței interogărilor în PostgreSQL
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
ordine clauză SQL rânduri disponibile
1 FROM weather_events toate
5 SELECT storm, elements toate
6 DISTINCT 1, 2, 3, 5
Îmbunătățirea performanței interogărilor în PostgreSQL
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
ordine clauză SQL rânduri disponibile
1 FROM weather_events toate
5 SELECT storm, elements toate
6 DISTINCT 1, 2, 3, 5
7 ORDER BY storm 1, 5, 2, 3
Îmbunătățirea performanței interogărilor în PostgreSQL
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
ordine clauză SQL rânduri disponibile
1 FROM weather_events toate
5 SELECT storm, elements toate
6 DISTINCT 1, 2, 3, 5
7 ORDER BY storm 1, 5, 2, 3
8 LIMIT 3 1, 5, 2
Îmbunătățirea performanței interogărilor în PostgreSQL
Ordine Clauză Scop Limite
1 FROM indică tabelul (tabelele)
2 WHERE filtrează înregistrările # rânduri
3 GROUP BY grupează înregistrările # coloane
4 SUM, COUNT, etc agregări # rânduri
5 SELECT identifică coloanele returnate # coloane
6 DISTINCT elimină duplicatele # rânduri
7 ORDER BY ordonează rezultatele
8 LIMIT filtrează înregistrările # rânduri
Îmbunătățirea performanței interogărilor în PostgreSQL

Să exersăm!

Îmbunătățirea performanței interogărilor în PostgreSQL

Preparing Video For Download...