SlideShare a Scribd company logo
The PostgreSQL Query Planner Robert Haas PostgreSQL East 2010
Why Does My Query Need a Plan? SQL is a declarative language.
In other words, a SQL query is not a program.
No control flow statements (e.g. for, while) and no way to control order of operations.
SQL describes results, not process.
Why Didn't The Planner Do It My Way? Maybe your way is actually slower, or
Maybe you gave the planner bad information, or
Maybe the query planner really did goof.
Related question: How do I force the planner to use my index?
Query Planning Make queries run fast. Minimize disk I/O.
Prefer sequential I/O to random I/O.
Minimize CPU processing. Don't use too much memory in the process.
Deliver correct results.
Query Planner Decisions Access strategy for each table. Sequential Scan, Index Scan, Bitmap Index Scan. Join strategy. Join order.
Join strategy: nested loop, merge join, hash join.
Inner vs. outer. Aggregation strategy. Plain, sorted, hashed.
Table Access Strategies Sequential Scan (Seq Scan) Read every row in the table. Index Scan or Bitmap Index Scan Read only part of the table by using the index to skip uninteresting parts.
Index scan reads index and table in alternation.
Bitmap index scan reads index first, populating bitmap, and then reads table in sequential order.
Sequential Scan Always works – no need to create indices in advance.
Doesn't require reading the index, which has both I/O and CPU cost.
Best way to access very small tables.
Usually the best way to access all or nearly the rows in a table.
Index Scan Potentially huge performance gain when reading only a small fraction of rows in a large table.
Only table access method that can return rows in sorted order – very useful in combination with LIMIT.
Random I/O against base table!
Bitmap Index Scan Scans all index rows before examining base table, populating a TID bitmap.
Table I/O is sequential, with skips; results in physical order.
Can efficiently combine data multiple indices – TID bitmap can handle boolean AND and OR operations.
Handles LIMIT poorly.
Join Planning Fixing the join order and join strategy is the “hard part” of query planning.
# of possibilities grows exponentially with number of tables.

More Related Content

What's hot (20)

PDF
Indexes in postgres
Louise Grandjonc
 
PDF
Deep dive into PostgreSQL statistics.
Alexey Lesovsky
 
PPTX
Introduction to PostgreSQL
Joel Brewer
 
PDF
Percona toolkit
Karwin Software Solutions LLC
 
PDF
PostgreSQL: Advanced indexing
Hans-Jürgen Schönig
 
PDF
Analyzing and Interpreting AWR
pasalapudi
 
PDF
Improving SparkSQL Performance by 30%: How We Optimize Parquet Pushdown and P...
Databricks
 
PDF
Patroni: Kubernetes-native PostgreSQL companion
Alexander Kukushkin
 
PDF
MariaDB Server Performance Tuning & Optimization
MariaDB plc
 
PDF
Using Optimizer Hints to Improve MySQL Query Performance
oysteing
 
PPTX
PostgreSQL Database Slides
metsarin
 
PDF
Flexible Indexing with Postgres
EDB
 
PPTX
Oracle sql high performance tuning
Guy Harrison
 
ODP
Introduction to PostgreSQL
Jim Mlodgenski
 
PDF
ClickHouse Deep Dive, by Aleksei Milovidov
Altinity Ltd
 
PDF
Chasing the optimizer
Mauro Pagano
 
PDF
PostgreSQL and RAM usage
Alexey Bashtanov
 
PPSX
Oracle Performance Tools of the Trade
Carlos Sierra
 
PDF
MySQL Administrator 2021 - 네오클로바
NeoClova
 
PDF
redis 소개자료 - 네오클로바
NeoClova
 
Indexes in postgres
Louise Grandjonc
 
Deep dive into PostgreSQL statistics.
Alexey Lesovsky
 
Introduction to PostgreSQL
Joel Brewer
 
PostgreSQL: Advanced indexing
Hans-Jürgen Schönig
 
Analyzing and Interpreting AWR
pasalapudi
 
Improving SparkSQL Performance by 30%: How We Optimize Parquet Pushdown and P...
Databricks
 
Patroni: Kubernetes-native PostgreSQL companion
Alexander Kukushkin
 
MariaDB Server Performance Tuning & Optimization
MariaDB plc
 
Using Optimizer Hints to Improve MySQL Query Performance
oysteing
 
PostgreSQL Database Slides
metsarin
 
Flexible Indexing with Postgres
EDB
 
Oracle sql high performance tuning
Guy Harrison
 
Introduction to PostgreSQL
Jim Mlodgenski
 
ClickHouse Deep Dive, by Aleksei Milovidov
Altinity Ltd
 
Chasing the optimizer
Mauro Pagano
 
PostgreSQL and RAM usage
Alexey Bashtanov
 
Oracle Performance Tools of the Trade
Carlos Sierra
 
MySQL Administrator 2021 - 네오클로바
NeoClova
 
redis 소개자료 - 네오클로바
NeoClova
 

Viewers also liked (20)

ODP
Basic Query Tuning Primer
Command Prompt., Inc
 
PDF
pg_proctab: Accessing System Stats in PostgreSQL
Command Prompt., Inc
 
PDF
PostgreSQL, Extensible to the Nth Degree: Functions, Languages, Types, Rules,...
Command Prompt., Inc
 
PDF
Not Just UNIQUE: Generalized Index Constraints
Command Prompt., Inc
 
PDF
Mastering PostgreSQL Administration
Command Prompt., Inc
 
PDF
PostgreSQL High Availability via SLONY and PG POOL II
Command Prompt., Inc
 
PPT
Building tungsten-clusters-with-postgre sql-hot-standby-and-streaming-replica...
Command Prompt., Inc
 
PDF
Pg migrator
Command Prompt., Inc
 
PDF
Backup and-recovery2
Command Prompt., Inc
 
PDF
Implementing the Future of PostgreSQL Clustering with Tungsten
Command Prompt., Inc
 
PDF
Go replicator
Command Prompt., Inc
 
PDF
Replication using PostgreSQL Replicator
Command Prompt., Inc
 
PDF
configuring a warm standby, the easy way
Command Prompt., Inc
 
ODP
Python utilities for data presentation
Command Prompt., Inc
 
PDF
A Practical Multi-Tenant Cluster
Command Prompt., Inc
 
PDF
Temporal Data
Command Prompt., Inc
 
PDF
Elephant Roads: a tour of Postgres forks
Command Prompt., Inc
 
PDF
Howdah - An Application using Pylons, PostgreSQL, Simpycity and Exceptable
Command Prompt., Inc
 
ODP
Introduction to triggers
Command Prompt., Inc
 
Basic Query Tuning Primer
Command Prompt., Inc
 
pg_proctab: Accessing System Stats in PostgreSQL
Command Prompt., Inc
 
PostgreSQL, Extensible to the Nth Degree: Functions, Languages, Types, Rules,...
Command Prompt., Inc
 
Not Just UNIQUE: Generalized Index Constraints
Command Prompt., Inc
 
Mastering PostgreSQL Administration
Command Prompt., Inc
 
PostgreSQL High Availability via SLONY and PG POOL II
Command Prompt., Inc
 
Building tungsten-clusters-with-postgre sql-hot-standby-and-streaming-replica...
Command Prompt., Inc
 
Backup and-recovery2
Command Prompt., Inc
 
Implementing the Future of PostgreSQL Clustering with Tungsten
Command Prompt., Inc
 
Go replicator
Command Prompt., Inc
 
Replication using PostgreSQL Replicator
Command Prompt., Inc
 
configuring a warm standby, the easy way
Command Prompt., Inc
 
Python utilities for data presentation
Command Prompt., Inc
 
A Practical Multi-Tenant Cluster
Command Prompt., Inc
 
Temporal Data
Command Prompt., Inc
 
Elephant Roads: a tour of Postgres forks
Command Prompt., Inc
 
Howdah - An Application using Pylons, PostgreSQL, Simpycity and Exceptable
Command Prompt., Inc
 
Introduction to triggers
Command Prompt., Inc
 
Ad

Similar to The PostgreSQL Query Planner (20)

PPTX
unit-2 Query processing and optimization,Query equivalence, Join strategies.pptx
RiteshSingh171777
 
PPTX
Join operation
Jeeva Nanthini
 
DOCX
DBMS ArchitectureQuery ExecutorBuffer ManagerStora
LinaCovington707
 
PPTX
Introduction to execution plan analysis
John Sterrett
 
PPTX
CS 542 -- Query Optimization
J Singh
 
PPTX
Relational Algebra Operator With Example
exitjogja
 
PPTX
SQL Tuning and VST
Kyle Hailey
 
PDF
Tech Talk - JPA and Query Optimization - publish
Gleydson Lima
 
PPTX
DOAG: Visual SQL Tuning
Kyle Hailey
 
PPTX
Query evaluation and optimization
lavanya marichamy
 
PDF
Optimizer features in recent releases of other databases
Sergey Petrunya
 
PDF
readme.pdf
ssuserbf6a4f
 
PPT
13. Query Processing in DBMS
koolkampus
 
PDF
Becoming a better developer with EXPLAIN
Louise Grandjonc
 
PDF
unit 3 DBMS.docx.pdf geometric transformer in query processing
FallenAngel35
 
PDF
unit 3 DBMS.docx.pdf geometry in query p
FallenAngel35
 
ODP
Talk PGConf Eu 2013
Atri Sharma
 
ODP
Basic Query Tuning Primer - Pg West 2009
mattsmiley
 
PDF
Understand the Query Plan to Optimize Performance with EXPLAIN and EXPLAIN AN...
EDB
 
PPT
The life of a query (oracle edition)
maclean liu
 
unit-2 Query processing and optimization,Query equivalence, Join strategies.pptx
RiteshSingh171777
 
Join operation
Jeeva Nanthini
 
DBMS ArchitectureQuery ExecutorBuffer ManagerStora
LinaCovington707
 
Introduction to execution plan analysis
John Sterrett
 
CS 542 -- Query Optimization
J Singh
 
Relational Algebra Operator With Example
exitjogja
 
SQL Tuning and VST
Kyle Hailey
 
Tech Talk - JPA and Query Optimization - publish
Gleydson Lima
 
DOAG: Visual SQL Tuning
Kyle Hailey
 
Query evaluation and optimization
lavanya marichamy
 
Optimizer features in recent releases of other databases
Sergey Petrunya
 
readme.pdf
ssuserbf6a4f
 
13. Query Processing in DBMS
koolkampus
 
Becoming a better developer with EXPLAIN
Louise Grandjonc
 
unit 3 DBMS.docx.pdf geometric transformer in query processing
FallenAngel35
 
unit 3 DBMS.docx.pdf geometry in query p
FallenAngel35
 
Talk PGConf Eu 2013
Atri Sharma
 
Basic Query Tuning Primer - Pg West 2009
mattsmiley
 
Understand the Query Plan to Optimize Performance with EXPLAIN and EXPLAIN AN...
EDB
 
The life of a query (oracle edition)
maclean liu
 
Ad

More from Command Prompt., Inc (12)

PDF
5 Steps to PostgreSQL Performance
Command Prompt., Inc
 
PPT
Normalization: A Workshop for Everybody Pt. 2
Command Prompt., Inc
 
PPT
Normalization: A Workshop for Everybody Pt. 1
Command Prompt., Inc
 
PDF
Integrating PostGIS in Web Applications
Command Prompt., Inc
 
PDF
Postgres for MySQL (and other database) people
Command Prompt., Inc
 
PDF
Building Grails applications with PostgreSQL
Command Prompt., Inc
 
PDF
Not Just UNIQUE: Exclusion Constraints
Command Prompt., Inc
 
PDF
pg_proctab: Accessing System Stats in PostgreSQL
Command Prompt., Inc
 
PDF
Database Hardware Benchmarking
Command Prompt., Inc
 
PDF
Vertically Challenged
Command Prompt., Inc
 
PDF
Simpycity and Exceptable
Command Prompt., Inc
 
5 Steps to PostgreSQL Performance
Command Prompt., Inc
 
Normalization: A Workshop for Everybody Pt. 2
Command Prompt., Inc
 
Normalization: A Workshop for Everybody Pt. 1
Command Prompt., Inc
 
Integrating PostGIS in Web Applications
Command Prompt., Inc
 
Postgres for MySQL (and other database) people
Command Prompt., Inc
 
Building Grails applications with PostgreSQL
Command Prompt., Inc
 
Not Just UNIQUE: Exclusion Constraints
Command Prompt., Inc
 
pg_proctab: Accessing System Stats in PostgreSQL
Command Prompt., Inc
 
Database Hardware Benchmarking
Command Prompt., Inc
 
Vertically Challenged
Command Prompt., Inc
 
Simpycity and Exceptable
Command Prompt., Inc
 

The PostgreSQL Query Planner

  • 1. The PostgreSQL Query Planner Robert Haas PostgreSQL East 2010
  • 2. Why Does My Query Need a Plan? SQL is a declarative language.
  • 3. In other words, a SQL query is not a program.
  • 4. No control flow statements (e.g. for, while) and no way to control order of operations.
  • 5. SQL describes results, not process.
  • 6. Why Didn't The Planner Do It My Way? Maybe your way is actually slower, or
  • 7. Maybe you gave the planner bad information, or
  • 8. Maybe the query planner really did goof.
  • 9. Related question: How do I force the planner to use my index?
  • 10. Query Planning Make queries run fast. Minimize disk I/O.
  • 11. Prefer sequential I/O to random I/O.
  • 12. Minimize CPU processing. Don't use too much memory in the process.
  • 14. Query Planner Decisions Access strategy for each table. Sequential Scan, Index Scan, Bitmap Index Scan. Join strategy. Join order.
  • 15. Join strategy: nested loop, merge join, hash join.
  • 16. Inner vs. outer. Aggregation strategy. Plain, sorted, hashed.
  • 17. Table Access Strategies Sequential Scan (Seq Scan) Read every row in the table. Index Scan or Bitmap Index Scan Read only part of the table by using the index to skip uninteresting parts.
  • 18. Index scan reads index and table in alternation.
  • 19. Bitmap index scan reads index first, populating bitmap, and then reads table in sequential order.
  • 20. Sequential Scan Always works – no need to create indices in advance.
  • 21. Doesn't require reading the index, which has both I/O and CPU cost.
  • 22. Best way to access very small tables.
  • 23. Usually the best way to access all or nearly the rows in a table.
  • 24. Index Scan Potentially huge performance gain when reading only a small fraction of rows in a large table.
  • 25. Only table access method that can return rows in sorted order – very useful in combination with LIMIT.
  • 26. Random I/O against base table!
  • 27. Bitmap Index Scan Scans all index rows before examining base table, populating a TID bitmap.
  • 28. Table I/O is sequential, with skips; results in physical order.
  • 29. Can efficiently combine data multiple indices – TID bitmap can handle boolean AND and OR operations.
  • 31. Join Planning Fixing the join order and join strategy is the “hard part” of query planning.
  • 32. # of possibilities grows exponentially with number of tables.
  • 33. When search space is small, planner does a nearly exhaustive search.
  • 34. When search space is too large, planner uses heuristics or GEQO to limit planning time and memory usage.
  • 36. Nested loop with inner index-scan.
  • 39. Each join strategy takes an “outer” relation and an “inner” relation and produces a result relation.
  • 40. Nested Loop Pseudocode for (each outer tuple) for (each inner tuple) if (join condition is met) emit result row; Outer or inner loop could be scanning output of some other join, or a base table. Base table scan could be using an index.
  • 41. Cost is roughly proportional to product of table sizes – bad if BOTH are large.
  • 42. Nested Loop Example #1 SELECT * FROM foo, bar WHERE foo.x = bar.x Nested Loop Join Filter: (foo.x = bar.x) -> Seq Scan on bar -> Materialize -> Seq Scan on foo This might be very slow!
  • 43. Nested Loop Example #2 SELECT * FROM foo, bar WHERE foo.x = bar.x Nested Loop -> Seq Scan on foo -> Index Scan using bar_pkey on bar Index Cond: (bar.x = foo.x) Nested loop with inner index-scan! Much better... though probably still not the best plan.
  • 44. Merge Join Only handles equality joins – something like a.x = b.x.
  • 45. Put both input relations into sorted order (using sort or index scan) and scan through the two in parallel, matching up equal values.
  • 46. Normally visits each input tuple only once, but may need to “rescan” portions of the inner input if there are duplicate values in the outer input. Take OUTER={1 2 2 3} and INNER={2 2 3 4}
  • 47. Merge Join Example SELECT * FROM foo, bar WHERE foo.x = bar.x Merge Join Merge Cond: (foo.x = bar.x) -> Sort Sort Key: foo.x -> Seq Scan on foo -> Materialize -> Sort Sort Key: bar.x -> Seq Scan on bar
  • 48. Hash Join Like merge join, only handles equality joins.
  • 49. Hash each row from the inner relation to create a hash table. Then, hash each row from the outer relation and probe the hash table for matches.
  • 50. Very fast – but requires enough memory to store inner tuples. Can get around this using multiple “batches”.
  • 51. Not guaranteed to retain input ordering.
  • 52. Hash Join Example SELECT * FROM foo, bar WHERE foo.x = bar.x Hash Join Hash Cond: (foo.x = bar.x) -> Seq Scan on foo -> Hash -> Seq Scan on bar
  • 53. Join Removal Upcoming 9.0 feature.
  • 55. SELECT p.id, p.name FROM projects p LEFT JOIN person pm ON p.project_manager_id = pm.id;
  • 56. If there is a unique index on person (id), then the join need not be performed at all.
  • 57. Common scenario when using views.
  • 58. Join Reordering SELECT * FROM foo JOIN bar ON foo.x = bar.x JOIN baz ON foo.y = baz.y
  • 59. SELECT * FROM foo JOIN baz ON foo.y = baz.y JOIN bar ON foo.x = bar.x
  • 60. SELECT * FROM foo JOIN (bar JOIN baz ON true) ON foo.x = bar.x AND foo.y = baz.y
  • 61. EXPLAIN Estimates Hash Join (cost=8.28..404.52 rows=9000 width=118) Hash Cond: (foo.x = bar.x) -> Hash Join (cost=3.02..275.52 rows=9000 width=12) Hash Cond: (foo.y = baz.y) -> Seq Scan on foo (cost=0.00..145.00 rows=10000 width=8) -> Hash (cost=1.90..1.90 rows=90 width=4) -> Seq Scan on baz (cost=0.00..1.90 rows=90 width=4) -> Hash (cost=4.00..4.00 rows=100 width=106) -> Seq Scan on bar (cost=0.00..4.00 rows=100 width=106)
  • 62. EXPLAIN ANALYZE Hash Join (cost=8.28..404.52 rows=9000 width=118) (actual time=0.743..51.582 rows=9000 loops=1) Hash Cond: (foo.x = bar.x) -> Hash Join (cost=3.02..275.52 rows=9000 width=12) (actual time=0.368..30.964 rows=9000 loops=1) Hash Cond: (foo.y = baz.y) -> Seq Scan on foo (cost=0.00..145.00 rows=10000 width=8) (actual time=0.021..9.908 rows=10000 loops=1) -> Hash (cost=1.90..1.90 rows=90 width=4) (actual time=0.280..0.280 rows=90 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 4kB -> Seq Scan on baz (cost=0.00..1.90 rows=90 width=4) (actual time=0.010..0.138 rows=90 loops=1) -> Hash (cost=4.00..4.00 rows=100 width=106) (actual time=0.354..0.354 rows=100 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 14kB -> Seq Scan on bar (cost=0.00..4.00 rows=100 width=106) (actual time=0.007..0.167 rows=100 loops=1) Total runtime: 59.376 ms
  • 63. Not The Same Thing! SELECT * FROM (foo JOIN bar ON foo.x = bar.x) LEFT JOIN baz ON foo.y = baz.y
  • 64. SELECT * FROM (foo LEFT JOIN baz ON foo.y = baz.y) JOIN bar ON foo.x = bar.x
  • 65. Review of Join Planning Join Order
  • 67. Nested loop with inner index-scan
  • 70. Join removal Inner vs. outer
  • 71. Aggregates and DISTINCT Plain aggregate. e.g. SELECT count(*) FROM foo; Sorted aggregate. Sort the data (or use pre-sorted data); when you see a new value, aggregate the prior group. Hashed aggregate. Insert each input row into a hash table based on the grouping columns; at the end, aggregate all the groups.
  • 72. Statistics All of the decisions discussed earlier in this talk are made using statistics. Seq scan vs. index scan vs. bitmap index scan
  • 73. Nested loop vs. merge join vs. hash join ANALYZE (manual or via autovacuum) gathers this information.
  • 74. You must have good statistics or you will get bad plans!
  • 75. Confusing The Planner SELECT * FROM foo WHERE a = 1 AND b = 1 If 20% of the rows have a = 1 and 10% of the rows have b = 1, the planner will assume that 20% * 10% = 2% of the rows meet both criteria.
  • 76. SELECT * FROM foo WHERE (a + 0) = a
  • 77. Planner doesn't have a clue, so will assume 0.5% of rows will match.
  • 78. What Could Go Wrong? If the planner underestimates the row count, it may choose an index scan instead of a sequential scan, or a nested loop instead of a hash or merge join.
  • 79. If the planner overestimates the row count, it may choose a sequential scan instead of an index scan, or a merge or hash join instead of a nested loop.
  • 80. Small values for LIMIT tilt the planner toward fast-start plans and magnify the effect of bad estimates.
  • 81. Query Planner Parameters seq_page_cost (1.0), random_page_cost (4.0) – Reduce these costs to account for caching effects. If database is fully cached, try 0.005.
  • 82. default_statistics_target (10 or 100) – Level of detail for statistics gathering. Can also be overridden on a per-column basis.
  • 83. enable_hashjoin, enable_sort, etc. - Just for testing.
  • 84. work_mem – Amount of memory per sort or hash.
  • 85. from_collapse_limit, join_collapse_limit, geqo_threshold – Sometimes need to be raised, but be careful!
  • 86. Things That Are Slow DISTINCT.
  • 87. PL/pgsql loops. FOR x IN SELECT ... LOOP SELECT ... END LOOP
  • 88. Repeated calls to SQL or PL/pgsql functions. SELECT id, some_function(id) FROM table;
  • 89. Upcoming Features Join removal (right now just for LEFT joins).
  • 92. Better model for Materialize costs.
  • 93. Improved use of indices to handle MIN(x), MAX(x), and x IS NOT NULL.