planejador de consultas do postgresql
DESCRIPTION
Apresentação sobre o "Query Planner" (planejador de consultas) do PostgreSQLTRANSCRIPT
![Page 1: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/1.jpg)
The PostgreSQL Query Planner
![Page 2: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/2.jpg)
Por que minha consulta (SQL) precisa de um plano?
![Page 3: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/3.jpg)
Simples...
● SQL é declarativo, ou seja, não é um programa
● Não possui controle de fluxo e não tem uma forma de controlar a ordem das operações– A não ser “While” e “For”, mas isso é conversa para
outro treinamento ;-)
● SQL pergunta O QUE, e não diz COMO
![Page 4: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/4.jpg)
Planejador/Otimizador é o Cérebro do banco de dados...
… pois interpreta consultas SQL e
determina o método de execução mais
rápido.
![Page 5: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/5.jpg)
Objetivos do Planejador
● Executar consultas mais rapidamente– Diminuir I/O de disco– Priorizar I/O sequencial a I/O randômico– Diminuir uso de CPU
● Não utilizar muita memória no processo
● Entregar resultados corretos
![Page 6: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/6.jpg)
Decisões do planejador
● Método de Pesquisa (Scan Method)
● Método de Junção (Join Method)
● Ordem de Junção (Join Order)
![Page 7: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/7.jpg)
Métodos de Pesquisa (Scan Methods)
● Sequential Scan● Bitmap Index Scan● Index Scan
![Page 8: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/8.jpg)
Sequential Scan
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
8K
Heap
![Page 9: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/9.jpg)
Sequential Scan
![Page 10: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/10.jpg)
Bitmap Index Scan
0
1
0
1
AND =
0
1
1
0
0
1
0
0
801 AND 11
Index 1numcgm = 801
Index 2receit = 11
CombinedIndex
TABLE
![Page 11: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/11.jpg)
Bitmap Index Scan
![Page 12: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/12.jpg)
Bitmap Index Scan
Index 1k00_numcgm = 801
Index 2k00_receit = 11
Combined Index(And Operator)
![Page 13: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/13.jpg)
Index Scankey
< = >
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
DATA
key
< = >
key
< = >
![Page 14: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/14.jpg)
Index Scan
![Page 15: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/15.jpg)
Index Scan
![Page 16: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/16.jpg)
Dúvidas???
![Page 17: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/17.jpg)
Tabela de exemplo
![Page 18: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/18.jpg)
Função para EXPLAIN
![Page 19: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/19.jpg)
Distribuição dos dados
![Page 20: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/20.jpg)
Distribuição dos dados
tipo | count | % ------+-------+------ 5 | 14892 | 77.6 34 | 2714 | 14.1 14 | 575 | 3.0 6 | 555 | 2.9 25 | 384 | 2.0 13 | 70 | 0.4 1 | 3 | 0.0 32 | 1 | 0.0 27 | 1 | 0.0(9 rows)
![Page 21: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/21.jpg)
Planos de execução
![Page 22: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/22.jpg)
tipo | count | % | explain_arrecad ------+-------+------+------------------------------------------------------------------------------- 5 | 14892 | 77.6 | Bitmap Heap Scan on arrecad (cost=3.00..122.14 rows=96 width=52) 5 | 14892 | 77.6 | Recheck Cond: (k00_tipo = 5) 5 | 14892 | 77.6 | -> Bitmap Index Scan on arrecad_tipo_in (cost=0.00..2.97 rows=96 width=0) 5 | 14892 | 77.6 | Index Cond: (k00_tipo = 5) 34 | 2714 | 14.1 | Bitmap Heap Scan on arrecad (cost=3.00..122.14 rows=96 width=52) 34 | 2714 | 14.1 | Recheck Cond: (k00_tipo = 34) 34 | 2714 | 14.1 | -> Bitmap Index Scan on arrecad_tipo_in (cost=0.00..2.97 rows=96 width=0) 34 | 2714 | 14.1 | Index Cond: (k00_tipo = 34) 14 | 575 | 3.0 | Bitmap Heap Scan on arrecad (cost=3.00..122.14 rows=96 width=52) 14 | 575 | 3.0 | Recheck Cond: (k00_tipo = 14) 14 | 575 | 3.0 | -> Bitmap Index Scan on arrecad_tipo_in (cost=0.00..2.97 rows=96 width=0) 14 | 575 | 3.0 | Index Cond: (k00_tipo = 14) 6 | 555 | 2.9 | Bitmap Heap Scan on arrecad (cost=3.00..122.14 rows=96 width=52) 6 | 555 | 2.9 | Recheck Cond: (k00_tipo = 6) 6 | 555 | 2.9 | -> Bitmap Index Scan on arrecad_tipo_in (cost=0.00..2.97 rows=96 width=0) 6 | 555 | 2.9 | Index Cond: (k00_tipo = 6) 25 | 384 | 2.0 | Bitmap Heap Scan on arrecad (cost=3.00..122.14 rows=96 width=52) 25 | 384 | 2.0 | Recheck Cond: (k00_tipo = 25) 25 | 384 | 2.0 | -> Bitmap Index Scan on arrecad_tipo_in (cost=0.00..2.97 rows=96 width=0) 25 | 384 | 2.0 | Index Cond: (k00_tipo = 25) 13 | 70 | 0.4 | Bitmap Heap Scan on arrecad (cost=3.00..122.14 rows=96 width=52) 13 | 70 | 0.4 | Recheck Cond: (k00_tipo = 13) 13 | 70 | 0.4 | -> Bitmap Index Scan on arrecad_tipo_in (cost=0.00..2.97 rows=96 width=0) 13 | 70 | 0.4 | Index Cond: (k00_tipo = 13) 1 | 3 | 0.0 | Bitmap Heap Scan on arrecad (cost=3.00..122.14 rows=96 width=52) 1 | 3 | 0.0 | Recheck Cond: (k00_tipo = 1) 1 | 3 | 0.0 | -> Bitmap Index Scan on arrecad_tipo_in (cost=0.00..2.97 rows=96 width=0) 1 | 3 | 0.0 | Index Cond: (k00_tipo = 1) 27 | 1 | 0.0 | Bitmap Heap Scan on arrecad (cost=3.00..122.14 rows=96 width=52) 27 | 1 | 0.0 | Recheck Cond: (k00_tipo = 27) 27 | 1 | 0.0 | -> Bitmap Index Scan on arrecad_tipo_in (cost=0.00..2.97 rows=96 width=0) 27 | 1 | 0.0 | Index Cond: (k00_tipo = 27) 32 | 1 | 0.0 | Bitmap Heap Scan on arrecad (cost=3.00..122.14 rows=96 width=52) 32 | 1 | 0.0 | Recheck Cond: (k00_tipo = 32) 32 | 1 | 0.0 | -> Bitmap Index Scan on arrecad_tipo_in (cost=0.00..2.97 rows=96 width=0) 32 | 1 | 0.0 | Index Cond: (k00_tipo = 32)(36 rows)
![Page 23: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/23.jpg)
Tá bom, só uma linha do EXPLAIN
![Page 24: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/24.jpg)
Melhorou agora?
tipo | count | % | explain_arrecad ------+-------+------+------------------------------------------------------------------- 5 | 14892 | 77.6 | Bitmap Heap Scan on arrecad (cost=3.00..122.14 rows=96 width=52) 34 | 2714 | 14.1 | Bitmap Heap Scan on arrecad (cost=3.00..122.14 rows=96 width=52) 14 | 575 | 3.0 | Bitmap Heap Scan on arrecad (cost=3.00..122.14 rows=96 width=52) 6 | 555 | 2.9 | Bitmap Heap Scan on arrecad (cost=3.00..122.14 rows=96 width=52) 25 | 384 | 2.0 | Bitmap Heap Scan on arrecad (cost=3.00..122.14 rows=96 width=52) 13 | 70 | 0.4 | Bitmap Heap Scan on arrecad (cost=3.00..122.14 rows=96 width=52) 1 | 3 | 0.0 | Bitmap Heap Scan on arrecad (cost=3.00..122.14 rows=96 width=52) 27 | 1 | 0.0 | Bitmap Heap Scan on arrecad (cost=3.00..122.14 rows=96 width=52) 32 | 1 | 0.0 | Bitmap Heap Scan on arrecad (cost=3.00..122.14 rows=96 width=52)(9 rows)
![Page 25: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/25.jpg)
Nada mudou né...
● Alguém arrisca dizer porque os planos estão iguais???
● Mesmo buscando qtds diferentes de dados da tabela???
![Page 26: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/26.jpg)
Estatísticas desatualizadas
![Page 27: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/27.jpg)
Estatísticas desatualizadas
![Page 28: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/28.jpg)
ANALYZE é seu amigo...
Esses valores não são familiares???
![Page 29: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/29.jpg)
Verificar planos de novo...
![Page 30: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/30.jpg)
Interessante não?
tipo | count | % | explain_arrecad ------+-------+------+---------------------------------------------------------------------------------- 5 | 14892 | 77.6 | Seq Scan on arrecad (cost=0.00..499.94 rows=14892 width=52) 34 | 2714 | 14.1 | Bitmap Heap Scan on arrecad (cost=39.28..333.21 rows=2714 width=52) 14 | 575 | 3.0 | Bitmap Heap Scan on arrecad (cost=8.71..275.89 rows=575 width=52) 6 | 555 | 2.9 | Bitmap Heap Scan on arrecad (cost=8.55..275.49 rows=555 width=52) 25 | 384 | 2.0 | Bitmap Heap Scan on arrecad (cost=7.23..250.28 rows=384 width=52) 13 | 70 | 0.4 | Index Scan using arrecad_tipo_in on arrecad (cost=0.00..29.27 rows=19 width=52) 1 | 3 | 0.0 | Index Scan using arrecad_tipo_in on arrecad (cost=0.00..29.27 rows=19 width=52) 32 | 1 | 0.0 | Index Scan using arrecad_tipo_in on arrecad (cost=0.00..29.27 rows=19 width=52) 27 | 1 | 0.0 | Index Scan using arrecad_tipo_in on arrecad (cost=0.00..29.27 rows=19 width=52)(9 rows)
![Page 31: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/31.jpg)
Vamos forçar um “IndexScan”?
![Page 32: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/32.jpg)
Ficou mais rápido??
tipo | count | % | explain_arrecad ------+-------+------+-------------------------------------------------------------------------------------- 5 | 14892 | 77.6 | Index Scan using arrecad_tipo_in on arrecad (cost=0.00..779.24 rows=14892 width=52) 34 | 2714 | 14.1 | Index Scan using arrecad_tipo_in on arrecad (cost=0.00..434.27 rows=2714 width=52) 14 | 575 | 3.0 | Index Scan using arrecad_tipo_in on arrecad (cost=0.00..373.73 rows=575 width=52) 6 | 555 | 2.9 | Index Scan using arrecad_tipo_in on arrecad (cost=0.00..373.38 rows=555 width=52) 25 | 384 | 2.0 | Index Scan using arrecad_tipo_in on arrecad (cost=0.00..316.27 rows=384 width=52) 13 | 70 | 0.4 | Index Scan using arrecad_tipo_in on arrecad (cost=0.00..29.27 rows=19 width=52) 1 | 3 | 0.0 | Index Scan using arrecad_tipo_in on arrecad (cost=0.00..29.27 rows=19 width=52) 27 | 1 | 0.0 | Index Scan using arrecad_tipo_in on arrecad (cost=0.00..29.27 rows=19 width=52) 32 | 1 | 0.0 | Index Scan using arrecad_tipo_in on arrecad (cost=0.00..29.27 rows=19 width=52)(9 rows)
tipo | count | % | explain_arrecad ------+-------+------+---------------------------------------------------------------------------------- 5 | 14892 | 77.6 | Seq Scan on arrecad (cost=0.00..499.94 rows=14892 width=52) 34 | 2714 | 14.1 | Bitmap Heap Scan on arrecad (cost=39.28..333.21 rows=2714 width=52) 14 | 575 | 3.0 | Bitmap Heap Scan on arrecad (cost=8.71..275.89 rows=575 width=52) 6 | 555 | 2.9 | Bitmap Heap Scan on arrecad (cost=8.55..275.49 rows=555 width=52) 25 | 384 | 2.0 | Bitmap Heap Scan on arrecad (cost=7.23..250.28 rows=384 width=52) 13 | 70 | 0.4 | Index Scan using arrecad_tipo_in on arrecad (cost=0.00..29.27 rows=19 width=52) 1 | 3 | 0.0 | Index Scan using arrecad_tipo_in on arrecad (cost=0.00..29.27 rows=19 width=52) 32 | 1 | 0.0 | Index Scan using arrecad_tipo_in on arrecad (cost=0.00..29.27 rows=19 width=52) 27 | 1 | 0.0 | Index Scan using arrecad_tipo_in on arrecad (cost=0.00..29.27 rows=19 width=52)(9 rows)
![Page 33: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/33.jpg)
DICA!
“Não tente ser mais esperto que o planejador.”
Mas ele não é “infalível”, se você já tentou de tudo e mesmo assim tem problemas, colabore seu plano conosco e nos
ajude a melhorá-lo:
[email protected]@postgresql.org
![Page 34: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/34.jpg)
Métodos de Junção(Join Methods)
● Nested Loop– With inner Sequencial Scan
– With inner Index Scan
● Hash Join● Merge Join
![Page 35: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/35.jpg)
Junção de duas tabelas com alta restrição (=)
![Page 36: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/36.jpg)
Junção de duas tabelas com alta restrição (=)
![Page 37: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/37.jpg)
Nested Loop(with Inner Sequencial Scan)
413
2134
5
27
6
Outer Inner
![Page 38: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/38.jpg)
Nested Loop(with Inner Sequencial Scan)
413
2134
5
27
6
Outer Inner
![Page 39: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/39.jpg)
Nested Loop(with Inner Sequencial Scan)
413
2134
5
27
6
Outer Inner
![Page 40: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/40.jpg)
Nested Loop(with Inner Sequencial Scan)
413
2134
5
27
6
Outer Inner
![Page 41: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/41.jpg)
Pseudocode for Nested Loop(with Inner Sequencial Scan)
![Page 42: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/42.jpg)
Junção de duas tabelas com baixa restrição (>, <, <>)
![Page 43: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/43.jpg)
Junção de duas tabelas com baixa restrição (>, <, <>)
![Page 44: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/44.jpg)
Hash Join
413
2 1
34
5
2 7
6
Outer Inner
Deve caber na RAM
Hashed
![Page 45: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/45.jpg)
Pseudocode for Hash Join
![Page 46: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/46.jpg)
Junção de duas tabelassem restrição
![Page 47: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/47.jpg)
Junção de duas tabelassem restrição
![Page 48: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/48.jpg)
Merge Join
Outer Inner
Ideal para grandes tabelasUm índice pode ser usado para eliminar o “Sort”
234
1223
1
45
4
Sorted Sorted
![Page 49: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/49.jpg)
Pseudocode for Merge Join
![Page 50: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/50.jpg)
Algumas considerações
● Ordem das junções é insignificante● Outer Joins (left, right) podem afetar o
otimizador● Nested Loop pode usar índice na pesquisa● Restrições (where) afetam o uso de junções● LIMIT pode afetar o uso de junções
![Page 51: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/51.jpg)
Dúvidas???
![Page 52: Planejador de Consultas do PostgreSQL](https://reader036.vdocuments.site/reader036/viewer/2022062312/556252dfd8b42a6c368b50f2/html5/thumbnails/52.jpg)