Améliorer la performance des requêtes dans PostgreSQL
Amy McCarty
Instructor
| PEMDAS | BODMAS |
|---|---|
| Parenthèses | Brackets |
| Exposants | Order |
| Multiplication /Division | Division /Multiplication |
| Addition /Soustraction | Addition /Soustraction |
$$ 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 $$
| Ordre | Clause | Rôle |
|---|---|---|
| 1 | FROM | Indique la table ou les jointures |
| 2 | WHERE | Filtre ou limite les enregistrements |
| 3 | GROUP BY | Regroupe les enregistrements |
| 4 | SUM(), COUNT(), etc. | Agrège |
| 5 | SELECT | Identifie les colonnes à retourner |
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
Aucun résultat -
votre code a généré une erreur
la colonne « storm » doit figurer dans la
clause GROUP BY ou être utilisée dans une
fonction d'agrégation
SELECT elements, storm, COUNT(*)
FROM weather_events
GROUP BY elements
| ordre | clause SQL | colonnes disponibles |
|---|---|---|
| 1 | FROM weather_events | toutes |
| 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
Résultats
| elements | storm | count |
|---|---|---|
| water | blizzard | 1 |
| water | tornado | 1 |
| wind | tornado | 2 |
| wind | haboob | 2 |
| Ordre | Clause | Rôle |
|---|---|---|
| ... | ||
| 5 | SELECT | Identifie les colonnes à retourner |
| 6 | DISTINCT | Supprime les doublons |
| 7 | ORDER BY | Trie les résultats |
| 8 | LIMIT | Retire des lignes |
| 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
| ordre | clause SQL | lignes disponibles |
|---|---|---|
| 1 | FROM weather_events | toutes |
| 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
| ordre | clause SQL | lignes disponibles |
|---|---|---|
| 1 | FROM weather_events | toutes |
| 5 | SELECT storm, elements | toutes |
| 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
| ordre | clause SQL | lignes disponibles |
|---|---|---|
| 1 | FROM weather_events | toutes |
| 5 | SELECT storm, elements | toutes |
| 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
| ordre | clause SQL | lignes disponibles |
|---|---|---|
| 1 | FROM weather_events | toutes |
| 5 | SELECT storm, elements | toutes |
| 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
| ordre | clause SQL | lignes disponibles |
|---|---|---|
| 1 | FROM weather_events | toutes |
| 5 | SELECT storm, elements | toutes |
| 6 | DISTINCT | 1, 2, 3, 5 |
| 7 | ORDER BY storm | 1, 5, 2, 3 |
| 8 | LIMIT 3 | 1, 5, 2 |
| Ordre | Clause | Rôle | Contraintes |
|---|---|---|---|
| 1 | FROM | Indique la ou les tables | |
| 2 | WHERE | Filtre ou limite les enregistrements | nb de lignes |
| 3 | GROUP BY | Regroupe les enregistrements | nb de colonnes |
| 4 | SUM, COUNT, etc. | Agrège | nb de lignes |
| 5 | SELECT | Identifie les colonnes à retourner | nb de colonnes |
| 6 | DISTINCT | Supprime les doublons | nb de lignes |
| 7 | ORDER BY | Trie les résultats | |
| 8 | LIMIT | Filtre les enregistrements | nb de lignes |
Améliorer la performance des requêtes dans PostgreSQL