explain.sgml 7.4 KB
Newer Older
1
<!--
2
$Header: /cvsroot/pgsql/doc/src/sgml/ref/explain.sgml,v 1.20 2002/04/21 19:02:39 thomas Exp $
3
PostgreSQL documentation
4 5
-->

6 7
<refentry id="SQL-EXPLAIN">
 <refmeta>
8
  <refentrytitle id="SQL-EXPLAIN-TITLE">EXPLAIN</refentrytitle>
9 10 11 12 13 14 15
  <refmiscinfo>SQL - Language Statements</refmiscinfo>
 </refmeta>
 <refnamediv>
  <refname>
   EXPLAIN
  </refname>
  <refpurpose>
16
   show the execution plan of a statement
17 18
  </refpurpose>
 </refnamediv>
19

20 21
 <refsynopsisdiv>
  <refsynopsisdivinfo>
22
   <date>1999-07-20</date>
23 24
  </refsynopsisdivinfo>
  <synopsis>
25
EXPLAIN [ ANALYZE ] [ VERBOSE ] <replaceable class="PARAMETER">query</replaceable>        
26
  </synopsis>
27

28 29 30 31 32 33 34 35
  <refsect2 id="R2-SQL-EXPLAIN-1">
   <refsect2info>
    <date>1998-09-01</date>
   </refsect2info>
   <title>
    Inputs
   </title>
   <para>
36

37
    <variablelist>
38 39 40 41
     <varlistentry>
      <term>ANALYZE</term>
      <listitem>
       <para>
42
	Flag to carry out the query and show actual run times.
43 44 45
       </para>
      </listitem>
     </varlistentry>
46 47 48 49
     <varlistentry>
      <term>VERBOSE</term>
      <listitem>
       <para>
50
	Flag to show detailed query plan dump.
51 52 53 54 55 56 57 58 59 60 61 62 63 64
       </para>
      </listitem>
     </varlistentry>
     <varlistentry>
      <term><replaceable class="PARAMETER">query</replaceable></term>
      <listitem>
       <para>
	Any <replaceable class="PARAMETER">query</replaceable>.
       </para>
      </listitem>
     </varlistentry>
    </variablelist>
   </para>
  </refsect2>
65

66 67 68 69 70 71 72 73
  <refsect2 id="R2-SQL-EXPLAIN-2">
   <refsect2info>
    <date>1998-04-15</date>
   </refsect2info>
   <title>
    Outputs
   </title>
   <para>
74

75 76
    <variablelist>
     <varlistentry>
77
      <term>Query plan</term>
78 79
      <listitem>
       <para>
80 81
	Explicit query plan from the <productname>PostgreSQL</productname>
	planner.
82 83 84 85 86
       </para>
      </listitem>
     </varlistentry>
    </variablelist>
   </para>
87 88 89 90 91 92 93 94

   <note>
    <para>
     Prior to <application>PostgreSQL</application> 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).
    </para>
   </note>
95 96
  </refsect2>
 </refsynopsisdiv>
97

98 99 100 101 102 103 104
 <refsect1 id="R1-SQL-EXPLAIN-1">
  <refsect1info>
   <date>1998-04-15</date>
  </refsect1info>
  <title>
   Description
  </title>
105

106
  <para>
107 108
   This command displays the execution plan that the
   <application>PostgreSQL</application> planner
109
   generates for the supplied query.  The execution plan shows how
B
Bruce Momjian 已提交
110 111
   the table(s) referenced by the query will be scanned---by plain
   sequential scan, index scan, etc.---and if multiple tables are
112 113 114 115 116 117 118 119
   referenced, what join algorithms will be used to bring together
   the required tuples from each input table.
  </para>

  <para>
   The most critical part of the display is the estimated query 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
B
Bruce Momjian 已提交
120
   are shown: the start-up time before the first tuple can be returned, and
121 122
   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
B
Bruce Momjian 已提交
123
   will choose the smallest start-up time instead of the smallest total time
124 125 126 127 128 129
   (since the executor will stop after getting one tuple, anyway).
   Also, if you limit the number of tuples to return with a LIMIT clause,
   the planner makes an appropriate interpolation between the endpoint
   costs to estimate which plan is really the cheapest.
  </para>

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
  <para>
   The ANALYZE option causes the query 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.
  </para>

  <caution>
   <para>
    Keep in mind that the query is actually executed when ANALYZE is used.
    Although <command>EXPLAIN</command> will discard any output that a SELECT
    would return,
    other side-effects of the query will happen as usual.
    If you wish to use <command>EXPLAIN ANALYZE</command> on an INSERT,
    UPDATE, or DELETE query without letting the query affect your data,
    use this approach:
      <programlisting>
BEGIN;
EXPLAIN ANALYZE ...;
ROLLBACK;
      </programlisting>
   </para>
  </caution>

155 156 157 158 159 160 161 162 163
  <para>
   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
   <application>PostgreSQL</application>.   The VERBOSE dump is either
   pretty-printed or not, depending on the setting of the
   <option>EXPLAIN_PRETTY_PRINT</option> configuration parameter.
  </para>

164 165 166 167 168 169 170 171 172
  <refsect2 id="R2-SQL-EXPLAIN-3">
   <refsect2info>
    <date>1998-04-15</date>
   </refsect2info>
   <title>
    Notes
   </title>
   <para>
    There is only sparse documentation on the optimizer's use of cost
173
    information in <productname>PostgreSQL</productname>.
174 175
    Refer to the <citetitle>User's Guide</citetitle> and
    <citetitle>Programmer's Guide</citetitle> for more information.
176 177 178
   </para>
  </refsect2>
 </refsect1>
179

180 181 182 183
 <refsect1 id="R1-SQL-EXPLAIN-2">
  <title>
   Usage
  </title>
184 185 186

  <para>
   To show a query plan for a simple query on a table with a single
187
   <type>int4</type> column and 10000 rows:
188 189 190 191

   <programlisting>
EXPLAIN SELECT * FROM foo;
    <computeroutput>
192 193 194 195
                       QUERY PLAN
---------------------------------------------------------
 Seq Scan on foo  (cost=0.00..155.00 rows=10000 width=4)
(1 row)
196 197 198 199
    </computeroutput>
   </programlisting>
  </para>

200
  <para>
201
   If there is an index and we use a query with an indexable WHERE condition,
202
   <command>EXPLAIN</command> will show a different plan:
203

204
   <programlisting>
205 206
EXPLAIN SELECT * FROM foo WHERE i = 4;
    <computeroutput>
207 208 209
                         QUERY PLAN
--------------------------------------------------------------
 Index Scan using fi on foo  (cost=0.00..5.98 rows=1 width=4)
210
   Index Cond: (i = 4)
211
(2 rows)
212 213 214 215 216
    </computeroutput>
   </programlisting>
  </para>

  <para>
217
   And here is an example of a query plan for a query
218 219 220
   using an aggregate function:

   <programlisting>
T
Tom Lane 已提交
221
EXPLAIN SELECT sum(i) FROM foo WHERE i &lt; 10;
222
    <computeroutput>
223 224 225
                             QUERY PLAN
---------------------------------------------------------------------
 Aggregate  (cost=23.93..23.93 rows=1 width=4)
T
Tom Lane 已提交
226
   -&gt;  Index Scan using fi on foo  (cost=0.00..23.92 rows=6 width=4)
227
         Index Cond: (i &lt; 10)
228
(3 rows)
229
    </computeroutput>
230 231
   </programlisting>
  </para>
232 233 234

  <para>
   Note that the specific numbers shown, and even the selected query
235 236
   strategy, may vary between <application>PostgreSQL</application>
   releases due to planner improvements.
237
  </para>
238
 </refsect1>
239

240 241 242 243
 <refsect1 id="R1-SQL-EXPLAIN-3">
  <title>
   Compatibility
  </title>
244

245 246 247 248 249 250 251 252 253 254 255 256 257
  <refsect2 id="R2-SQL-EXPLAIN-4">
   <refsect2info>
    <date>1998-09-01</date>
   </refsect2info>
   <title>
    SQL92
   </title>
   <para>
    There is no <command>EXPLAIN</command> statement defined in SQL92.
   </para>
  </refsect2>
 </refsect1>
</refentry>
258

259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:nil
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
sgml-parent-document:nil
sgml-default-dtd-file:"../reference.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:"/usr/lib/sgml/catalog"
sgml-local-ecat-files:nil
End:
274
-->