2 $PostgreSQL: pgsql/doc/src/sgml/ref/explain.sgml,v 1.34 2004/09/30 04:23:27 neilc Exp $
3 PostgreSQL documentation
6 <refentry id="SQL-EXPLAIN">
8 <refentrytitle id="SQL-EXPLAIN-TITLE">EXPLAIN</refentrytitle>
9 <refmiscinfo>SQL - Language Statements</refmiscinfo>
13 <refname>EXPLAIN</refname>
14 <refpurpose>show the execution plan of a statement</refpurpose>
17 <indexterm zone="sql-explain">
18 <primary>EXPLAIN</primary>
21 <indexterm zone="sql-explain">
22 <primary>prepared statements</primary>
23 <secondary>showing the query plan</secondary>
26 <indexterm zone="sql-explain">
27 <primary>cursor</primary>
28 <secondary>showing the query plan</secondary>
33 EXPLAIN [ ANALYZE ] [ VERBOSE ] <replaceable class="parameter">statement</replaceable>
38 <title>Description</title>
41 This command displays the execution plan that the
42 <productname>PostgreSQL</productname> planner generates for the
43 supplied statement. The execution plan shows how the table(s)
44 referenced by the statement will be scanned---by plain sequential scan,
45 index scan, etc.---and if multiple tables are referenced, what join
46 algorithms will be used to bring together the required row from
51 The most critical part of the display is the estimated statement execution
52 cost, which is the planner's guess at how long it will take to run the
53 statement (measured in units of disk page fetches). Actually two numbers
54 are shown: the start-up time before the first row can be returned, and
55 the total time to return all the rows. For most queries the total time
56 is what matters, but in contexts such as a subquery in <literal>EXISTS</literal>, the planner
57 will choose the smallest start-up time instead of the smallest total time
58 (since the executor will stop after getting one row, anyway).
59 Also, if you limit the number of rows to return with a <literal>LIMIT</literal> clause,
60 the planner makes an appropriate interpolation between the endpoint
61 costs to estimate which plan is really the cheapest.
65 The <literal>ANALYZE</literal> option causes the statement to be actually executed, not only
66 planned. The total elapsed time expended within each plan node (in
67 milliseconds) and total number of rows it actually returned are added to
68 the display. This is useful for seeing whether the planner's estimates
74 Keep in mind that the statement is actually executed when
75 <literal>ANALYZE</literal> is used. Although
76 <command>EXPLAIN</command> will discard any output that a
77 <command>SELECT</command> would return, other side effects of the
78 statement will happen as usual. If you wish to use
79 <command>EXPLAIN ANALYZE</command> on an
80 <command>INSERT</command>, <command>UPDATE</command>,
81 <command>DELETE</command>, or <command>EXECUTE</command> statement
82 without letting the command affect your data, use this approach:
93 <title>Parameters</title>
97 <term><literal>ANALYZE</literal></term>
100 Carry out the command and show the actual run times.
106 <term><literal>VERBOSE</literal></term>
109 Show the full internal representation of the plan tree, rather
110 than just a summary. Usually this option is only useful for
111 debugging <productname>PostgreSQL</productname>. The
112 <literal>VERBOSE</literal> output is either pretty-printed or
113 not, depending on the setting of the <xref
114 linkend="guc-explain-pretty-print"> configuration parameter.
120 <term><replaceable class="parameter">statement</replaceable></term>
123 Any <command>SELECT</>, <command>INSERT</>, <command>UPDATE</>,
124 <command>DELETE</>, <command>EXECUTE</>, or <command>DECLARE</>
125 statement, whose execution plan you wish to see.
136 There is only sparse documentation on the optimizer's use of cost
137 information in <productname>PostgreSQL</productname>. Refer to
138 <xref linkend="using-explain"> for more information.
142 In order to allow the <productname>PostgreSQL</productname> query
143 planner to make reasonably informed decisions when optimizing
144 queries, the <command>ANALYZE</command> statement should be run to
145 record statistics about the distribution of data within the
146 table. If you have not done this (or if the statistical
147 distribution of the data in the table has changed significantly
148 since the last time <command>ANALYZE</command> was run), the
149 estimated costs are unlikely to conform to the real properties of
150 the query, and consequently an inferior query plan may be chosen.
154 Prior to <productname>PostgreSQL</productname> 7.3, the plan was
155 emitted in the form of a <literal>NOTICE</literal> message. Now it
156 appears as a query result (formatted like a table with a single
162 <title>Examples</title>
165 To show the plan for a simple query on a table with a single
166 <type>integer</type> column and 10000 rows:
169 EXPLAIN SELECT * FROM foo;
172 ---------------------------------------------------------
173 Seq Scan on foo (cost=0.00..155.00 rows=10000 width=4)
179 If there is an index and we use a query with an indexable
180 <literal>WHERE</literal> condition, <command>EXPLAIN</command>
181 might show a different plan:
184 EXPLAIN SELECT * FROM foo WHERE i = 4;
187 --------------------------------------------------------------
188 Index Scan using fi on foo (cost=0.00..5.98 rows=1 width=4)
195 And here is an example of a query plan for a query
196 using an aggregate function:
199 EXPLAIN SELECT sum(i) FROM foo WHERE i < 10;
202 ---------------------------------------------------------------------
203 Aggregate (cost=23.93..23.93 rows=1 width=4)
204 -> Index Scan using fi on foo (cost=0.00..23.92 rows=6 width=4)
205 Index Cond: (i < 10)
211 Here is an example of using <command>EXPLAIN EXECUTE</command> to
212 display the execution plan for a prepared query:
215 PREPARE query(int, int) AS SELECT sum(bar) FROM test
216 WHERE id > $1 AND id < $2
219 EXPLAIN ANALYZE EXECUTE query(100, 200);
222 -------------------------------------------------------------------------------------------------------------------------
223 HashAggregate (cost=39.53..39.53 rows=1 width=8) (actual time=0.661..0.672 rows=7 loops=1)
224 -> Index Scan using test_pkey on test (cost=0.00..32.97 rows=1311 width=8) (actual time=0.050..0.395 rows=99 loops=1)
225 Index Cond: ((id > $1) AND (id < $2))
226 Total runtime: 0.851 ms
232 Of course, the specific numbers shown here depend on the actual
233 contents of the tables involved. Also note that the numbers, and
234 even the selected query strategy, may vary between
235 <productname>PostgreSQL</productname> releases due to planner
236 improvements. In addition, the <command>ANALYZE</command> command
237 uses random sampling to estimate data statistics; therefore, it is
238 possible for cost estimates to change after a fresh run of
239 <command>ANALYZE</command>, even if the actual distribution of data
240 in the table has not changed.
245 <title>Compatibility</title>
248 There is no <command>EXPLAIN</command> statement defined in the SQL standard.
253 <!-- Keep this comment at the end of the file
258 sgml-minimize-attributes:nil
259 sgml-always-quote-attributes:t
262 sgml-parent-document:nil
263 sgml-default-dtd-file:"../reference.ced"
264 sgml-exposed-tags:nil
265 sgml-local-catalogs:"/usr/lib/sgml/catalog"
266 sgml-local-ecat-files:nil