📄 explain.7
字号:
.\\" auto-generated by docbook2man-spec $Revision: 1.1.1.1 $.TH "EXPLAIN" "" "2008-01-03" "SQL - Language Statements" "SQL Commands".SH NAMEEXPLAIN \- show the execution plan of a statement.SH SYNOPSIS.sp.nfEXPLAIN [ ANALYZE ] [ VERBOSE ] \fIstatement\fR.sp.fi.SH "DESCRIPTION".PPThis command displays the execution plan that thePostgreSQL planner generates for thesupplied statement. The execution plan shows how the table(s)referenced by the statement will be scanned \(em by plain sequential scan,index scan, etc. \(em and if multiple tables are referenced, what joinalgorithms will be used to bring together the required rows fromeach input table..PPThe most critical part of the display is the estimated statement executioncost, which is the planner's guess at how long it will take to run thestatement (measured in units of disk page fetches). Actually two numbersare shown: the start-up time before the first row can be returned, andthe total time to return all the rows. For most queries the total timeis what matters, but in contexts such as a subquery in EXISTS, the plannerwill choose the smallest start-up time instead of the smallest total time(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 endpointcosts to estimate which plan is really the cheapest..PPThe ANALYZE option causes the statement to be actually executed, not onlyplanned. The total elapsed time expended within each plan node (inmilliseconds) and total number of rows it actually returned are added tothe display. This is useful for seeing whether the planner's estimatesare close to reality..sp.RS.B "Important:"Keep in mind that the statement is actually executed whenANALYZE is used. Although\fBEXPLAIN\fR will discard any output that a\fBSELECT\fR would return, other side effects of thestatement will happen as usual. If you wish to use\fBEXPLAIN ANALYZE\fR on an\fBINSERT\fR, \fBUPDATE\fR,\fBDELETE\fR, or \fBEXECUTE\fR statementwithout letting the command affect your data, use this approach:.sp.nfBEGIN;EXPLAIN ANALYZE ...;ROLLBACK;.sp.fi.RE.sp.SH "PARAMETERS".TP\fBANALYZE\fRCarry out the command and show the actual run times..TP\fBVERBOSE\fRShow the full internal representation of the plan tree, ratherthan just a summary. Usually this option is only useful forspecialized debugging purposes. TheVERBOSE output is either pretty-printed ornot, depending on the setting of the explain_pretty_print configuration parameter..TP\fB\fIstatement\fB\fRAny \fBSELECT\fR, \fBINSERT\fR, \fBUPDATE\fR,\fBDELETE\fR, \fBVALUES\fR, \fBEXECUTE\fR, or\fBDECLARE\fR statement, whose execution plan you wish to see..SH "NOTES".PPThere is only sparse documentation on the optimizer's use of costinformation in PostgreSQL. Refer toin the documentation for more information..PPIn order to allow the PostgreSQL queryplanner to make reasonably informed decisions when optimizingqueries, the \fBANALYZE\fR statement should be run torecord statistics about the distribution of data within thetable. If you have not done this (or if the statisticaldistribution of the data in the table has changed significantlysince the last time \fBANALYZE\fR was run), theestimated costs are unlikely to conform to the real properties ofthe query, and consequently an inferior query plan may be chosen..PPGenetic query optimization (GEQO) randomly tests execution plans. Therefore, when the number of tables exceedsgeqo_threshold causing genetic query optimization to be used, the execution plan is likely to change each time the statementis executed..SH "EXAMPLES".PPTo show the plan for a simple query on a table with a single\fBinteger\fR column and 10000 rows:.sp.nfEXPLAIN SELECT * FROM foo; QUERY PLAN--------------------------------------------------------- Seq Scan on foo (cost=0.00..155.00 rows=10000 width=4)(1 row).sp.fi.PPIf there is an index and we use a query with an indexableWHERE condition, \fBEXPLAIN\fRmight show a different plan:.sp.nfEXPLAIN 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).sp.fi.PPAnd here is an example of a query plan for a queryusing an aggregate function:.sp.nfEXPLAIN 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).sp.fi.PPHere is an example of using \fBEXPLAIN EXECUTE\fR todisplay the execution plan for a prepared query:.sp.nfPREPARE 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.661..0.672 rows=7 loops=1) -> 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) Index Cond: ((id > $1) AND (id < $2)) Total runtime: 0.851 ms(4 rows).sp.fi.PPOf course, the specific numbers shown here depend on the actualcontents of the tables involved. Also note that the numbers, andeven the selected query strategy, may vary betweenPostgreSQL releases due to plannerimprovements. In addition, the \fBANALYZE\fR commanduses random sampling to estimate data statistics; therefore, it ispossible for cost estimates to change after a fresh run of\fBANALYZE\fR, even if the actual distribution of datain the table has not changed..SH "COMPATIBILITY".PPThere is no \fBEXPLAIN\fR statement defined in the SQL standard..SH "SEE ALSO"ANALYZE [\fBanalyze\fR(7)]
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -