Improving Query Performance in 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 $$
Order | Clause | Purpose |
---|---|---|
1 | FROM | Provides directions to the table or tables if the query includes joins |
2 | WHERE | Filters or limits the records |
3 | GROUP BY | Places records into categories |
4 | SUM(), COUNT(), etc | Aggregates |
5 | SELECT | identifies columns to return |
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
order | SQL clause | available columns |
---|---|---|
1 | FROM weather_events | all |
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
Results
elements | storm | count |
---|---|---|
water | blizzard | 1 |
water | tornado | 1 |
wind | tornado | 2 |
wind | haboob | 2 |
Order | Clause | Purpose |
---|---|---|
... | ||
5 | SELECT | identifies columns to return |
6 | DISTINCT | removes duplicates |
7 | ORDER BY | arranges results |
8 | LIMIT | removes rows |
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
order | SQL clause | available rows |
---|---|---|
1 | FROM weather_events | all |
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
order | SQL clause | available rows |
---|---|---|
1 | FROM weather_events | all |
5 | SELECT storm, elements | all |
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
order | SQL clause | available rows |
---|---|---|
1 | FROM weather_events | all |
5 | SELECT storm, elements | all |
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
order | SQL clause | available rows |
---|---|---|
1 | FROM weather_events | all |
5 | SELECT storm, elements | all |
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
order | SQL clause | available rows |
---|---|---|
1 | FROM weather_events | all |
5 | SELECT storm, elements | all |
6 | DISTINCT | 1, 2, 3, 5 |
7 | ORDER BY storm | 1, 5, 2, 3 |
8 | LIMIT 3 | 1, 5, 2 |
Order | Clause | Purpose | Limits |
---|---|---|---|
1 | FROM | provides directions to the table(s) | |
2 | WHERE | filters or limits the records | # rows |
3 | GROUP BY | places records into categories | # columns |
4 | SUM, COUNT, etc | aggregates | # rows |
5 | SELECT | identifies columns to return | # columns |
6 | DISTINCT | removes duplicates | # rows |
7 | ORDER BY | arranges results | |
8 | LIMIT | filters records | # rows |
Improving Query Performance in PostgreSQL