gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

295 lines
18KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>EXPLAIN</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="sql-execute.html" title="EXECUTE" /><link rel="next" href="sql-fetch.html" title="FETCH" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">EXPLAIN</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-execute.html" title="EXECUTE">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-fetch.html" title="FETCH">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-EXPLAIN"><div class="titlepage"></div><a id="id-1.9.3.148.1" class="indexterm"></a><a id="id-1.9.3.148.2" class="indexterm"></a><a id="id-1.9.3.148.3" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">EXPLAIN</span></h2><p>EXPLAIN — show the execution plan of a statement</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. EXPLAIN [ ( <em class="replaceable"><code>option</code></em> [, ...] ) ] <em class="replaceable"><code>statement</code></em>
  4. EXPLAIN [ ANALYZE ] [ VERBOSE ] <em class="replaceable"><code>statement</code></em>
  5. <span class="phrase">where <em class="replaceable"><code>option</code></em> can be one of:</span>
  6. ANALYZE [ <em class="replaceable"><code>boolean</code></em> ]
  7. VERBOSE [ <em class="replaceable"><code>boolean</code></em> ]
  8. COSTS [ <em class="replaceable"><code>boolean</code></em> ]
  9. SETTINGS [ <em class="replaceable"><code>boolean</code></em> ]
  10. BUFFERS [ <em class="replaceable"><code>boolean</code></em> ]
  11. TIMING [ <em class="replaceable"><code>boolean</code></em> ]
  12. SUMMARY [ <em class="replaceable"><code>boolean</code></em> ]
  13. FORMAT { TEXT | XML | JSON | YAML }
  14. </pre></div><div class="refsect1" id="id-1.9.3.148.7"><h2>Description</h2><p>
  15. This command displays the execution plan that the
  16. <span class="productname">PostgreSQL</span> planner generates for the
  17. supplied statement. The execution plan shows how the table(s)
  18. referenced by the statement will be scanned — by plain sequential scan,
  19. index scan, etc. — and if multiple tables are referenced, what join
  20. algorithms will be used to bring together the required rows from
  21. each input table.
  22. </p><p>
  23. The most critical part of the display is the estimated statement execution
  24. cost, which is the planner's guess at how long it will take to run the
  25. statement (measured in cost units that are arbitrary, but conventionally
  26. mean disk page fetches). Actually two numbers
  27. are shown: the start-up cost before the first row can be returned, and
  28. the total cost to return all the rows. For most queries the total cost
  29. is what matters, but in contexts such as a subquery in <code class="literal">EXISTS</code>, the planner
  30. will choose the smallest start-up cost instead of the smallest total cost
  31. (since the executor will stop after getting one row, anyway).
  32. Also, if you limit the number of rows to return with a <code class="literal">LIMIT</code> clause,
  33. the planner makes an appropriate interpolation between the endpoint
  34. costs to estimate which plan is really the cheapest.
  35. </p><p>
  36. The <code class="literal">ANALYZE</code> option causes the statement to be actually
  37. executed, not only planned. Then actual run time statistics are added to
  38. the display, including the total elapsed time expended within each plan
  39. node (in milliseconds) and the total number of rows it actually returned.
  40. This is useful for seeing whether the planner's estimates
  41. are close to reality.
  42. </p><div class="important"><h3 class="title">Important</h3><p>
  43. Keep in mind that the statement is actually executed when
  44. the <code class="literal">ANALYZE</code> option is used. Although
  45. <code class="command">EXPLAIN</code> will discard any output that a
  46. <code class="command">SELECT</code> would return, other side effects of the
  47. statement will happen as usual. If you wish to use
  48. <code class="command">EXPLAIN ANALYZE</code> on an
  49. <code class="command">INSERT</code>, <code class="command">UPDATE</code>,
  50. <code class="command">DELETE</code>, <code class="command">CREATE TABLE AS</code>,
  51. or <code class="command">EXECUTE</code> statement
  52. without letting the command affect your data, use this approach:
  53. </p><pre class="programlisting">
  54. BEGIN;
  55. EXPLAIN ANALYZE ...;
  56. ROLLBACK;
  57. </pre><p>
  58. </p></div><p>
  59. Only the <code class="literal">ANALYZE</code> and <code class="literal">VERBOSE</code> options
  60. can be specified, and only in that order, without surrounding the option
  61. list in parentheses. Prior to <span class="productname">PostgreSQL</span> 9.0,
  62. the unparenthesized syntax was the only one supported. It is expected that
  63. all new options will be supported only in the parenthesized syntax.
  64. </p></div><div class="refsect1" id="id-1.9.3.148.8"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">ANALYZE</code></span></dt><dd><p>
  65. Carry out the command and show actual run times and other statistics.
  66. This parameter defaults to <code class="literal">FALSE</code>.
  67. </p></dd><dt><span class="term"><code class="literal">VERBOSE</code></span></dt><dd><p>
  68. Display additional information regarding the plan. Specifically, include
  69. the output column list for each node in the plan tree, schema-qualify
  70. table and function names, always label variables in expressions with
  71. their range table alias, and always print the name of each trigger for
  72. which statistics are displayed. This parameter defaults to
  73. <code class="literal">FALSE</code>.
  74. </p></dd><dt><span class="term"><code class="literal">COSTS</code></span></dt><dd><p>
  75. Include information on the estimated startup and total cost of each
  76. plan node, as well as the estimated number of rows and the estimated
  77. width of each row.
  78. This parameter defaults to <code class="literal">TRUE</code>.
  79. </p></dd><dt><span class="term"><code class="literal">SETTINGS</code></span></dt><dd><p>
  80. Include information on configuration parameters. Specifically, include
  81. options affecting query planning with value different from the built-in
  82. default value. This parameter defaults to <code class="literal">FALSE</code>.
  83. </p></dd><dt><span class="term"><code class="literal">BUFFERS</code></span></dt><dd><p>
  84. Include information on buffer usage. Specifically, include the number of
  85. shared blocks hit, read, dirtied, and written, the number of local blocks
  86. hit, read, dirtied, and written, and the number of temp blocks read and
  87. written.
  88. A <span class="emphasis"><em>hit</em></span> means that a read was avoided because the block was
  89. found already in cache when needed.
  90. Shared blocks contain data from regular tables and indexes;
  91. local blocks contain data from temporary tables and indexes;
  92. while temp blocks contain short-term working data used in sorts, hashes,
  93. Materialize plan nodes, and similar cases.
  94. The number of blocks <span class="emphasis"><em>dirtied</em></span> indicates the number of
  95. previously unmodified blocks that were changed by this query; while the
  96. number of blocks <span class="emphasis"><em>written</em></span> indicates the number of
  97. previously-dirtied blocks evicted from cache by this backend during
  98. query processing.
  99. The number of blocks shown for an
  100. upper-level node includes those used by all its child nodes. In text
  101. format, only non-zero values are printed. This parameter may only be
  102. used when <code class="literal">ANALYZE</code> is also enabled. It defaults to
  103. <code class="literal">FALSE</code>.
  104. </p></dd><dt><span class="term"><code class="literal">TIMING</code></span></dt><dd><p>
  105. Include actual startup time and time spent in each node in the output.
  106. The overhead of repeatedly reading the system clock can slow down the
  107. query significantly on some systems, so it may be useful to set this
  108. parameter to <code class="literal">FALSE</code> when only actual row counts, and
  109. not exact times, are needed. Run time of the entire statement is
  110. always measured, even when node-level timing is turned off with this
  111. option.
  112. This parameter may only be used when <code class="literal">ANALYZE</code> is also
  113. enabled. It defaults to <code class="literal">TRUE</code>.
  114. </p></dd><dt><span class="term"><code class="literal">SUMMARY</code></span></dt><dd><p>
  115. Include summary information (e.g., totaled timing information) after the
  116. query plan. Summary information is included by default when
  117. <code class="literal">ANALYZE</code> is used but otherwise is not included by
  118. default, but can be enabled using this option. Planning time in
  119. <code class="command">EXPLAIN EXECUTE</code> includes the time required to fetch
  120. the plan from the cache and the time required for re-planning, if
  121. necessary.
  122. </p></dd><dt><span class="term"><code class="literal">FORMAT</code></span></dt><dd><p>
  123. Specify the output format, which can be TEXT, XML, JSON, or YAML.
  124. Non-text output contains the same information as the text output
  125. format, but is easier for programs to parse. This parameter defaults to
  126. <code class="literal">TEXT</code>.
  127. </p></dd><dt><span class="term"><em class="replaceable"><code>boolean</code></em></span></dt><dd><p>
  128. Specifies whether the selected option should be turned on or off.
  129. You can write <code class="literal">TRUE</code>, <code class="literal">ON</code>, or
  130. <code class="literal">1</code> to enable the option, and <code class="literal">FALSE</code>,
  131. <code class="literal">OFF</code>, or <code class="literal">0</code> to disable it. The
  132. <em class="replaceable"><code>boolean</code></em> value can also
  133. be omitted, in which case <code class="literal">TRUE</code> is assumed.
  134. </p></dd><dt><span class="term"><em class="replaceable"><code>statement</code></em></span></dt><dd><p>
  135. Any <code class="command">SELECT</code>, <code class="command">INSERT</code>, <code class="command">UPDATE</code>,
  136. <code class="command">DELETE</code>, <code class="command">VALUES</code>, <code class="command">EXECUTE</code>,
  137. <code class="command">DECLARE</code>, <code class="command">CREATE TABLE AS</code>, or
  138. <code class="command">CREATE MATERIALIZED VIEW AS</code> statement, whose execution
  139. plan you wish to see.
  140. </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.148.9"><h2>Outputs</h2><p>
  141. The command's result is a textual description of the plan selected
  142. for the <em class="replaceable"><code>statement</code></em>,
  143. optionally annotated with execution statistics.
  144. <a class="xref" href="using-explain.html" title="14.1. Using EXPLAIN">Section 14.1</a> describes the information provided.
  145. </p></div><div class="refsect1" id="id-1.9.3.148.10"><h2>Notes</h2><p>
  146. In order to allow the <span class="productname">PostgreSQL</span> query
  147. planner to make reasonably informed decisions when optimizing
  148. queries, the <a class="link" href="catalog-pg-statistic.html" title="51.50. pg_statistic"><code class="structname">pg_statistic</code></a>
  149. data should be up-to-date for all tables used in the query. Normally
  150. the <a class="link" href="routine-vacuuming.html#AUTOVACUUM" title="24.1.6. The Autovacuum Daemon">autovacuum daemon</a> will take care
  151. of that automatically. But if a table has recently had substantial
  152. changes in its contents, you might need to do a manual
  153. <a class="xref" href="sql-analyze.html" title="ANALYZE"><span class="refentrytitle">ANALYZE</span></a> rather than wait for autovacuum to catch up
  154. with the changes.
  155. </p><p>
  156. In order to measure the run-time cost of each node in the execution
  157. plan, the current implementation of <code class="command">EXPLAIN
  158. ANALYZE</code> adds profiling overhead to query execution.
  159. As a result, running <code class="command">EXPLAIN ANALYZE</code>
  160. on a query can sometimes take significantly longer than executing
  161. the query normally. The amount of overhead depends on the nature of
  162. the query, as well as the platform being used. The worst case occurs
  163. for plan nodes that in themselves require very little time per
  164. execution, and on machines that have relatively slow operating
  165. system calls for obtaining the time of day.
  166. </p></div><div class="refsect1" id="id-1.9.3.148.11"><h2>Examples</h2><p>
  167. To show the plan for a simple query on a table with a single
  168. <code class="type">integer</code> column and 10000 rows:
  169. </p><pre class="programlisting">
  170. EXPLAIN SELECT * FROM foo;
  171. QUERY PLAN
  172. ---------------------------------------------------------
  173. Seq Scan on foo (cost=0.00..155.00 rows=10000 width=4)
  174. (1 row)
  175. </pre><p>
  176. </p><p>
  177. Here is the same query, with JSON output formatting:
  178. </p><pre class="programlisting">
  179. EXPLAIN (FORMAT JSON) SELECT * FROM foo;
  180. QUERY PLAN
  181. --------------------------------
  182. [ +
  183. { +
  184. "Plan": { +
  185. "Node Type": "Seq Scan",+
  186. "Relation Name": "foo", +
  187. "Alias": "foo", +
  188. "Startup Cost": 0.00, +
  189. "Total Cost": 155.00, +
  190. "Plan Rows": 10000, +
  191. "Plan Width": 4 +
  192. } +
  193. } +
  194. ]
  195. (1 row)
  196. </pre><p>
  197. </p><p>
  198. If there is an index and we use a query with an indexable
  199. <code class="literal">WHERE</code> condition, <code class="command">EXPLAIN</code>
  200. might show a different plan:
  201. </p><pre class="programlisting">
  202. EXPLAIN SELECT * FROM foo WHERE i = 4;
  203. QUERY PLAN
  204. --------------------------------------------------------------
  205. Index Scan using fi on foo (cost=0.00..5.98 rows=1 width=4)
  206. Index Cond: (i = 4)
  207. (2 rows)
  208. </pre><p>
  209. </p><p>
  210. Here is the same query, but in YAML format:
  211. </p><pre class="programlisting">
  212. EXPLAIN (FORMAT YAML) SELECT * FROM foo WHERE i='4';
  213. QUERY PLAN
  214. -------------------------------
  215. - Plan: +
  216. Node Type: "Index Scan" +
  217. Scan Direction: "Forward"+
  218. Index Name: "fi" +
  219. Relation Name: "foo" +
  220. Alias: "foo" +
  221. Startup Cost: 0.00 +
  222. Total Cost: 5.98 +
  223. Plan Rows: 1 +
  224. Plan Width: 4 +
  225. Index Cond: "(i = 4)"
  226. (1 row)
  227. </pre><p>
  228. XML format is left as an exercise for the reader.
  229. </p><p>
  230. Here is the same plan with cost estimates suppressed:
  231. </p><pre class="programlisting">
  232. EXPLAIN (COSTS FALSE) SELECT * FROM foo WHERE i = 4;
  233. QUERY PLAN
  234. ----------------------------
  235. Index Scan using fi on foo
  236. Index Cond: (i = 4)
  237. (2 rows)
  238. </pre><p>
  239. </p><p>
  240. Here is an example of a query plan for a query using an aggregate
  241. function:
  242. </p><pre class="programlisting">
  243. EXPLAIN SELECT sum(i) FROM foo WHERE i &lt; 10;
  244. QUERY PLAN
  245. ---------------------------------------------------------------------
  246. Aggregate (cost=23.93..23.93 rows=1 width=4)
  247. -&gt; Index Scan using fi on foo (cost=0.00..23.92 rows=6 width=4)
  248. Index Cond: (i &lt; 10)
  249. (3 rows)
  250. </pre><p>
  251. </p><p>
  252. Here is an example of using <code class="command">EXPLAIN EXECUTE</code> to
  253. display the execution plan for a prepared query:
  254. </p><pre class="programlisting">
  255. PREPARE query(int, int) AS SELECT sum(bar) FROM test
  256. WHERE id &gt; $1 AND id &lt; $2
  257. GROUP BY foo;
  258. EXPLAIN ANALYZE EXECUTE query(100, 200);
  259. QUERY PLAN
  260. ------------------------------------------------------------------------------------------------------------------------
  261. HashAggregate (cost=9.54..9.54 rows=1 width=8) (actual time=0.156..0.161 rows=11 loops=1)
  262. Group Key: foo
  263. -&gt; Index Scan using test_pkey on test (cost=0.29..9.29 rows=50 width=8) (actual time=0.039..0.091 rows=99 loops=1)
  264. Index Cond: ((id &gt; $1) AND (id &lt; $2))
  265. Planning time: 0.197 ms
  266. Execution time: 0.225 ms
  267. (6 rows)
  268. </pre><p>
  269. </p><p>
  270. Of course, the specific numbers shown here depend on the actual
  271. contents of the tables involved. Also note that the numbers, and
  272. even the selected query strategy, might vary between
  273. <span class="productname">PostgreSQL</span> releases due to planner
  274. improvements. In addition, the <code class="command">ANALYZE</code> command
  275. uses random sampling to estimate data statistics; therefore, it is
  276. possible for cost estimates to change after a fresh run of
  277. <code class="command">ANALYZE</code>, even if the actual distribution of data
  278. in the table has not changed.
  279. </p></div><div class="refsect1" id="id-1.9.3.148.12"><h2>Compatibility</h2><p>
  280. There is no <code class="command">EXPLAIN</code> statement defined in the SQL standard.
  281. </p></div><div class="refsect1" id="id-1.9.3.148.13"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-analyze.html" title="ANALYZE"><span class="refentrytitle">ANALYZE</span></a></span></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-execute.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-fetch.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">EXECUTE </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> FETCH</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1