From 20aae3047fb6a04c7cd042873aefa5cba404cfa9 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Sat, 26 Apr 2003 23:56:51 +0000 Subject: Editing of more reference pages. --- doc/src/sgml/ref/explain.sgml | 337 ++++++++++++++++++------------------------ 1 file changed, 142 insertions(+), 195 deletions(-) (limited to 'doc/src/sgml/ref/explain.sgml') diff --git a/doc/src/sgml/ref/explain.sgml b/doc/src/sgml/ref/explain.sgml index 02a6a63c42e..2ac74e8a383 100644 --- a/doc/src/sgml/ref/explain.sgml +++ b/doc/src/sgml/ref/explain.sgml @@ -1,5 +1,5 @@ @@ -8,255 +8,212 @@ PostgreSQL documentation EXPLAIN SQL - Language Statements + - - EXPLAIN - - - show the execution plan of a statement - + EXPLAIN + show the execution plan of a statement - - 1999-07-20 - - -EXPLAIN [ ANALYZE ] [ VERBOSE ] query - - - - - 1998-09-01 - - - Inputs - - - - - - ANALYZE - - - Flag to carry out the query and show actual run times. - - - - - VERBOSE - - - Flag to show detailed query plan dump. - - - - - query - - - Any SELECT, INSERT, UPDATE, - DELETE, EXECUTE, - or DECLARE CURSOR query. - - - - - - - - - - 1998-04-15 - - - Outputs - - - - - - Query plan - - - Explicit query plan from the PostgreSQL - planner. - - - - - - - - - Prior to PostgreSQL 7.3, the query - plan was emitted in the form of a NOTICE - message. Now it appears as a query result (formatted like a - table with a single text column). - - - + +EXPLAIN [ ANALYZE ] [ VERBOSE ] statement + - - - 1998-04-15 - - - Description - + + Description This command displays the execution plan that the PostgreSQL planner generates for the - supplied query. The execution plan shows how the table(s) - referenced by the query will be scanned---by plain sequential scan, + supplied statenebt. The execution plan shows how the table(s) + referenced by the statement will be scanned---by plain sequential scan, index scan, etc.---and if multiple tables are referenced, what join - algorithms will be used to bring together the required tuples from + algorithms will be used to bring together the required row from each input table. - The most critical part of the display is the estimated query execution + The most critical part of the display is the estimated statement execution cost, which is the planner's guess at how long it will take to run the - query (measured in units of disk page fetches). Actually two numbers - are shown: the start-up time before the first tuple can be returned, and - the total time to return all the tuples. For most queries the total time - is what matters, but in contexts such as an EXISTS sub-query the planner + statement (measured in units of disk page fetches). Actually two numbers + are shown: the start-up time before the first row can be returned, and + the total time to return all the rows. For most queries the total time + is what matters, but in contexts such as a subquery in EXISTS, the planner will choose the smallest start-up time instead of the smallest total time - (since the executor will stop after getting one tuple, anyway). - Also, if you limit the number of tuples to return with a LIMIT clause, + (since the executor will stop after getting one row, anyway). + Also, if you limit the number of rows to return with a LIMIT clause, the planner makes an appropriate interpolation between the endpoint costs to estimate which plan is really the cheapest. - - - In order to allow the PostgreSQL query - planner to make reasonably informed decisions when optimizing - queries, the ANALYZE statement should be run - to record statistics about the distribution of data within the - table. If you have not done this (or if the statistical distribution - of the data in the table has changed significantly since the last - time ANALYZE was run), the estimated costs - are unlikely to conform to the real properties of the query, - and consequently an inferior query plan may be chosen. - - - - The ANALYZE option causes the query to be actually executed, not only + The ANALYZE option causes the statement to be actually executed, not only planned. The total elapsed time expended within each plan node (in milliseconds) and total number of rows it actually returned are added to the display. This is useful for seeing whether the planner's estimates are close to reality. - + - Keep in mind that the query is actually executed when ANALYZE is used. - Although EXPLAIN will discard any output that a SELECT - would return, - other side-effects of the query will happen as usual. - If you wish to use EXPLAIN ANALYZE on an INSERT, - UPDATE, DELETE, or EXECUTE query without letting the query affect your - data, use this approach: - + Keep in mind that the statement is actually executed when + ANALYZE is used. Although + EXPLAIN will discard any output that a + SELECT would return, other side effects of the + statement will happen as usual. If you wish to use + EXPLAIN ANALYZE on an + INSERT, UPDATE, + DELETE, or EXECUTE statement + without letting the command affect your data, use this approach: + BEGIN; EXPLAIN ANALYZE ...; ROLLBACK; - + - + + + + + Parameter + + + + ANALYZE + + + Carry out the command and show the actual run times. + + + + + + VERBOSE + + + Show the full internal representation of the plan tree, rather + than just a summary. Usually this option is only useful for + debugging PostgreSQL. The + VERBOSE output is either pretty-printed or + not, depending on the setting of the + explain_pretty_print configuration parameter. + + + + + + statement + + + Any SELECT, INSERT, UPDATE, + DELETE, EXECUTE, or DECLARE + statement, whose execution plan you wish to see. + + + + + + + + Diagnostics - The VERBOSE option emits the full internal representation of the plan tree, - rather than just a summary. - Usually this option is only useful for debugging - PostgreSQL. The VERBOSE dump is either - pretty-printed or not, depending on the setting of the - configuration parameter. + EXPLAIN prints the execution plan of the + specified statement from the PostgreSQL + planner. + - - - 1998-04-15 - - - Notes - - - There is only sparse documentation on the optimizer's use of cost - information in PostgreSQL. - Refer to for more information. - - + + Notes + + + There is only sparse documentation on the optimizer's use of cost + information in PostgreSQL. Refer to + for more information. + + + + In order to allow the PostgreSQL query + planner to make reasonably informed decisions when optimizing + queries, the ANALYZE statement should be run to + record statistics about the distribution of data within the + table. If you have not done this (or if the statistical + distribution of the data in the table has changed significantly + since the last time ANALYZE was run), the + estimated costs are unlikely to conform to the real properties of + the query, and consequently an inferior query plan may be chosen. + + + + Prior to PostgreSQL 7.3, the plan was + emitted in the form of a NOTICE message. Now it + appears as a query result (formatted like a table with a single + text column). + - - - Usage - + + Examples - To show a query plan for a simple query on a table with a single - int4 column and 10000 rows: + To show the plan for a simple query on a table with a single + integer column and 10000 rows: - + EXPLAIN SELECT * FROM foo; - + QUERY PLAN --------------------------------------------------------- Seq Scan on foo (cost=0.00..155.00 rows=10000 width=4) (1 row) - - + - If there is an index and we use a query with an indexable WHERE condition, - EXPLAIN will show a different plan: + If there is an index and we use a query with an indexable + WHERE condition, EXPLAIN + might show a different plan: - + EXPLAIN SELECT * FROM foo WHERE i = 4; - + QUERY PLAN -------------------------------------------------------------- Index Scan using fi on foo (cost=0.00..5.98 rows=1 width=4) Index Cond: (i = 4) (2 rows) - - + And here is an example of a query plan for a query using an aggregate function: - + EXPLAIN SELECT sum(i) FROM foo WHERE i < 10; - + QUERY PLAN --------------------------------------------------------------------- Aggregate (cost=23.93..23.93 rows=1 width=4) -> Index Scan using fi on foo (cost=0.00..23.92 rows=6 width=4) Index Cond: (i < 10) (3 rows) - - + - Here is an example of using EXPLAIN EXECUTE to display the query - plan for a prepared query: + Here is an example of using EXPLAIN EXECUTE to + display the execution plan for a prepared query: - + PREPARE query(int, int) AS SELECT sum(bar) FROM test WHERE id > $1 AND id < $2 GROUP BY foo; EXPLAIN ANALYZE EXECUTE query(100, 200); - + QUERY PLAN ------------------------------------------------------------------------------------------------------------------------- HashAggregate (cost=39.53..39.53 rows=1 width=8) (actual time=0.66..0.67 rows=7 loops=1) @@ -264,38 +221,28 @@ EXPLAIN ANALYZE EXECUTE query(100, 200); Index Cond: ((id > $1) AND (id < $2)) Total runtime: 0.85 msec (4 rows) - - + - Note that the specific numbers shown, and even the selected query - strategy, may vary between PostgreSQL - releases due to planner improvements. In addition, the - ANALYZE command uses random sampling to estimate - data statistics; therefore, it is possible - for cost estimates to change after a fresh run of + Of course, the specific numbers shown here depend on the actual + contents of the tables involved. Also note that the numbers, and + even the selected query strategy, may vary between + PostgreSQL releases due to planner + improvements. In addition, the ANALYZE command + uses random sampling to estimate data statistics; therefore, it is + possible for cost estimates to change after a fresh run of ANALYZE, even if the actual distribution of data in the table has not changed. - - - Compatibility - - - - - 1998-09-01 - - - SQL92 - - - There is no EXPLAIN statement defined in SQL92. - - + + Compatibility + + + There is no EXPLAIN statement defined in the SQL standard. + -- cgit v1.2.3