explain.sgml 8.6 KB
Newer Older
1
<!--
2
$PostgreSQL: pgsql/doc/src/sgml/ref/explain.sgml,v 1.39 2007/01/31 23:26:04 momjian Exp $
3
PostgreSQL documentation
4 5
-->

6 7
<refentry id="SQL-EXPLAIN">
 <refmeta>
8
  <refentrytitle id="SQL-EXPLAIN-TITLE">EXPLAIN</refentrytitle>
9 10
  <refmiscinfo>SQL - Language Statements</refmiscinfo>
 </refmeta>
11

12
 <refnamediv>
13 14
  <refname>EXPLAIN</refname>
  <refpurpose>show the execution plan of a statement</refpurpose>
15
 </refnamediv>
16

P
Peter Eisentraut 已提交
17 18 19 20
 <indexterm zone="sql-explain">
  <primary>EXPLAIN</primary>
 </indexterm>

21 22 23 24 25 26 27 28 29 30
 <indexterm zone="sql-explain">
  <primary>prepared statements</primary>
  <secondary>showing the query plan</secondary>
 </indexterm>

 <indexterm zone="sql-explain">
  <primary>cursor</primary>
  <secondary>showing the query plan</secondary>
 </indexterm>

31
 <refsynopsisdiv>
32 33 34
<synopsis>
EXPLAIN [ ANALYZE ] [ VERBOSE ] <replaceable class="parameter">statement</replaceable>
</synopsis>
35
 </refsynopsisdiv>
36

37 38
 <refsect1>
  <title>Description</title>
39

40
  <para>
41
   This command displays the execution plan that the
B
Bruce Momjian 已提交
42
   <productname>PostgreSQL</productname> planner generates for the
T
Tom Lane 已提交
43
   supplied statement.  The execution plan shows how the table(s)
T
Tom Lane 已提交
44 45 46
   referenced by the statement will be scanned &mdash; by plain sequential scan,
   index scan, etc. &mdash; and if multiple tables are referenced, what join
   algorithms will be used to bring together the required rows from
B
Bruce Momjian 已提交
47
   each input table.
48 49 50
  </para>

  <para>
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 54 55 56
   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 <literal>EXISTS</literal>, the planner
B
Bruce Momjian 已提交
57
   will choose the smallest start-up time instead of the smallest total time
58 59
   (since the executor will stop after getting one row, anyway).
   Also, if you limit the number of rows to return with a <literal>LIMIT</literal> clause,
60 61 62 63
   the planner makes an appropriate interpolation between the endpoint
   costs to estimate which plan is really the cheapest.
  </para>

64
  <para>
65
   The <literal>ANALYZE</literal> option causes the statement to be actually executed, not only
66 67 68
   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
69
   are close to reality.
70 71
  </para>

72
  <important>
73
   <para>
74 75 76 77 78 79 80 81 82 83
    Keep in mind that the statement is actually executed when
    <literal>ANALYZE</literal> is used.  Although
    <command>EXPLAIN</command> will discard any output that a
    <command>SELECT</command> would return, other side effects of the
    statement will happen as usual.  If you wish to use
    <command>EXPLAIN ANALYZE</command> on an
    <command>INSERT</command>, <command>UPDATE</command>,
    <command>DELETE</command>, or <command>EXECUTE</command> statement
    without letting the command affect your data, use this approach:
<programlisting>
84 85 86
BEGIN;
EXPLAIN ANALYZE ...;
ROLLBACK;
87
</programlisting>
88
   </para>
89 90 91 92
  </important>
 </refsect1>

 <refsect1>
T
Tom Lane 已提交
93
  <title>Parameters</title>
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

  <variablelist>
   <varlistentry>
    <term><literal>ANALYZE</literal></term>
    <listitem>
     <para>
      Carry out the command and show the actual run times.
     </para>
    </listitem>
   </varlistentry>

   <varlistentry>
    <term><literal>VERBOSE</literal></term>
    <listitem>
     <para>
      Show the full internal representation of the plan tree, rather
      than just a summary.  Usually this option is only useful for
T
Tom Lane 已提交
111
      specialized debugging purposes.  The
112
      <literal>VERBOSE</literal> output is either pretty-printed or
113 114
      not, depending on the setting of the <xref
      linkend="guc-explain-pretty-print"> configuration parameter.
115 116 117 118 119 120 121 122 123
     </para>
    </listitem>
   </varlistentry>

   <varlistentry>
    <term><replaceable class="parameter">statement</replaceable></term>
    <listitem>
     <para>
      Any <command>SELECT</>, <command>INSERT</>, <command>UPDATE</>,
124 125
      <command>DELETE</>, <command>VALUES</>, <command>EXECUTE</>, or
      <command>DECLARE</> statement, whose execution plan you wish to see.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
     </para>
    </listitem>
   </varlistentry>
  </variablelist>
 </refsect1>

 <refsect1>
  <title>Notes</title>

  <para>
   There is only sparse documentation on the optimizer's use of cost
   information in <productname>PostgreSQL</productname>.  Refer to
   <xref linkend="using-explain"> for more information.
  </para>

  <para>
   In order to allow the <productname>PostgreSQL</productname> query
   planner to make reasonably informed decisions when optimizing
   queries, the <command>ANALYZE</command> 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 <command>ANALYZE</command> was run), the
   estimated costs are unlikely to conform to the real properties of
150
   the query, and consequently an inferior query plan might be chosen.
151 152 153
  </para>

  <para>
154 155 156 157 158
   Genetic query optimization (<acronym>GEQO</acronym>) randomly 
   tests execution plans.  Therefore, when the number of tables exceeds
   <varname>geqo_threshold</> causing genetic query optimization to be 
   used, the execution plan is likely to change each time the statement
   is executed.
159
  </para>
160

161
 </refsect1>
162

163 164
 <refsect1>
  <title>Examples</title>
165 166

  <para>
167 168
   To show the plan for a simple query on a table with a single
   <type>integer</type> column and 10000 rows:
169

170
<programlisting>
171
EXPLAIN SELECT * FROM foo;
172

173 174 175 176
                       QUERY PLAN
---------------------------------------------------------
 Seq Scan on foo  (cost=0.00..155.00 rows=10000 width=4)
(1 row)
177
</programlisting>
178 179
  </para>

180
  <para>
181 182 183
   If there is an index and we use a query with an indexable
   <literal>WHERE</literal> condition, <command>EXPLAIN</command>
   might show a different plan:
184

185
<programlisting>
186
EXPLAIN SELECT * FROM foo WHERE i = 4;
187

188 189 190
                         QUERY PLAN
--------------------------------------------------------------
 Index Scan using fi on foo  (cost=0.00..5.98 rows=1 width=4)
191
   Index Cond: (i = 4)
192
(2 rows)
193
</programlisting>
194 195 196
  </para>

  <para>
197
   And here is an example of a query plan for a query
198 199
   using an aggregate function:

200
<programlisting>
T
Tom Lane 已提交
201
EXPLAIN SELECT sum(i) FROM foo WHERE i &lt; 10;
202

203 204 205
                             QUERY PLAN
---------------------------------------------------------------------
 Aggregate  (cost=23.93..23.93 rows=1 width=4)
T
Tom Lane 已提交
206
   -&gt;  Index Scan using fi on foo  (cost=0.00..23.92 rows=6 width=4)
207
         Index Cond: (i &lt; 10)
208
(3 rows)
209
</programlisting>
210
  </para>
211

212
  <para>
213 214
   Here is an example of using <command>EXPLAIN EXECUTE</command> to
   display the execution plan for a prepared query:
215

216
<programlisting>
217 218 219 220 221
PREPARE query(int, int) AS SELECT sum(bar) FROM test
    WHERE id &gt; $1 AND id &lt; $2
    GROUP BY foo;

EXPLAIN ANALYZE EXECUTE query(100, 200);
222

223 224
                                                       QUERY PLAN                                                        
-------------------------------------------------------------------------------------------------------------------------
225 226
 HashAggregate  (cost=39.53..39.53 rows=1 width=8) (actual time=0.661..0.672 rows=7 loops=1)
   -&gt;  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)
227
         Index Cond: ((id &gt; $1) AND (id &lt; $2))
228
 Total runtime: 0.851 ms
229
(4 rows)
230
</programlisting>
231 232
  </para>

233
  <para>
234 235
   Of course, the specific numbers shown here depend on the actual
   contents of the tables involved.  Also note that the numbers, and
236
   even the selected query strategy, might vary between
237 238 239 240
   <productname>PostgreSQL</productname> releases due to planner
   improvements. In addition, the <command>ANALYZE</command> command
   uses random sampling to estimate data statistics; therefore, it is
   possible for cost estimates to change after a fresh run of
241 242
   <command>ANALYZE</command>, even if the actual distribution of data
   in the table has not changed.
243
  </para>
244
 </refsect1>
245

246 247 248 249 250 251
 <refsect1>
  <title>Compatibility</title>

  <para>
   There is no <command>EXPLAIN</command> statement defined in the SQL standard.
  </para>
252
 </refsect1>
T
Tom Lane 已提交
253 254 255 256 257 258 259 260

 <refsect1>
  <title>See Also</title>

  <simplelist type="inline">
   <member><xref linkend="sql-analyze" endterm="sql-analyze-title"></member>
  </simplelist>
 </refsect1>
261
</refentry>