Debian Codegraph @

www/eqp.html

raw
1 <!DOCTYPE html>
2 <html><head>
3 <meta name="viewport" content="width=device-width, initial-scale=1.0">
4 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
5 <link href="sqlite.css" rel="stylesheet">
6 <title>EXPLAIN QUERY PLAN</title>
7 <!-- path= -->
8 </head>
9 <body>
10 <div class=nosearch>
11 <a href="index.html">
12 <img class="logo" src="images/sqlite370_banner.gif" alt="SQLite" border="0">
13 </a>
14 <div><!-- IE hack to prevent disappearing logo --></div>
15 <div class="tagline desktoponly">
16 Small. Fast. Reliable.<br>Choose any three.
17 </div>
18 <div class="menu mainmenu">
19 <ul>
20 <li><a href="index.html">Home</a>
21 <li class='mobileonly'><a href="javascript:void(0)" onclick='toggle_div("submenu")'>Menu</a>
22 <li class='wideonly'><a href='about.html'>About</a>
23 <li class='desktoponly'><a href="docs.html">Documentation</a>
24 <li class='desktoponly'><a href="download.html">Download</a>
25 <li class='wideonly'><a href='copyright.html'>License</a>
26 <li class='desktoponly'><a href="support.html">Support</a>
27 <li class='desktoponly'><a href="prosupport.html">Purchase</a>
28 <li class='search' id='search_menubutton'>
29 <a href="javascript:void(0)" onclick='toggle_search()'>Search</a>
30 </ul>
31 </div>
32 <div class="menu submenu" id="submenu">
33 <ul>
34 <li><a href='about.html'>About</a>
35 <li><a href='docs.html'>Documentation</a>
36 <li><a href='download.html'>Download</a>
37 <li><a href='support.html'>Support</a>
38 <li><a href='prosupport.html'>Purchase</a>
39 </ul>
40 </div>
41 <div class="searchmenu" id="searchmenu">
42 <form method="GET" action="search">
43 <select name="s" id="searchtype">
44 <option value="d">Search Documentation</option>
45 <option value="c">Search Changelog</option>
46 </select>
47 <input type="text" name="q" id="searchbox" value="">
48 <input type="submit" value="Go">
49 </form>
50 </div>
51 </div>
52 <script>
53 function toggle_div(nm) {
54 var w = document.getElementById(nm);
55 if( w.style.display=="block" ){
56 w.style.display = "none";
57 }else{
58 w.style.display = "block";
59 }
60 }
61 function toggle_search() {
62 var w = document.getElementById("searchmenu");
63 if( w.style.display=="block" ){
64 w.style.display = "none";
65 } else {
66 w.style.display = "block";
67 setTimeout(function(){
68 document.getElementById("searchbox").focus()
69 }, 30);
70 }
71 }
72 function div_off(nm){document.getElementById(nm).style.display="none";}
73 window.onbeforeunload = function(e){div_off("submenu");}
74 /* Disable the Search feature if we are not operating from CGI, since */
75 /* Search is accomplished using CGI and will not work without it. */
76 if( !location.origin || !location.origin.match || !location.origin.match(/http/) ){
77 document.getElementById("search_menubutton").style.display = "none";
78 }
79 /* Used by the Hide/Show button beside syntax diagrams, to toggle the */
80 function hideorshow(btn,obj){
81 var x = document.getElementById(obj);
82 var b = document.getElementById(btn);
83 if( x.style.display!='none' ){
84 x.style.display = 'none';
85 b.innerHTML='show';
86 }else{
87 x.style.display = '';
88 b.innerHTML='hide';
89 }
90 return false;
91 }
92 var antiRobot = 0;
93 function antiRobotGo(){
94 if( antiRobot!=3 ) return;
95 antiRobot = 7;
96 var j = document.getElementById("mtimelink");
97 if(j && j.hasAttribute("data-href")) j.href=j.getAttribute("data-href");
98 }
99 function antiRobotDefense(){
100 document.body.onmousedown=function(){
101 antiRobot |= 2;
102 antiRobotGo();
103 document.body.onmousedown=null;
104 }
105 document.body.onmousemove=function(){
106 antiRobot |= 2;
107 antiRobotGo();
108 document.body.onmousemove=null;
109 }
110 setTimeout(function(){
111 antiRobot |= 1;
112 antiRobotGo();
113 }, 100)
114 antiRobotGo();
115 }
116 antiRobotDefense();
117 </script>
118 <div class=fancy>
119 <div class=nosearch>
120 <div class="fancy_title">
121 EXPLAIN QUERY PLAN
122 </div>
123 <div class="fancy_toc">
124 <a onclick="toggle_toc()">
125 <span class="fancy_toc_mark" id="toc_mk">&#x25ba;</span>
126 Table Of Contents
127 </a>
128 <div id="toc_sub"><div class="fancy-toc1"><a href="#the_explain_query_plan_command">1. The EXPLAIN QUERY PLAN Command</a></div>
129 <div class="fancy-toc2"><a href="#table_and_index_scans">1.1. Table and Index Scans</a></div>
130 <div class="fancy-toc2"><a href="#temporary_sorting_b_trees">1.2. Temporary Sorting B-Trees</a></div>
131 <div class="fancy-toc2"><a href="#subqueries">1.3. Subqueries</a></div>
132 <div class="fancy-toc2"><a href="#compound_queries">1.4. Compound Queries</a></div>
133 </div>
134 </div>
135 <script>
136 function toggle_toc(){
137 var sub = document.getElementById("toc_sub")
138 var mk = document.getElementById("toc_mk")
139 if( sub.style.display!="block" ){
140 sub.style.display = "block";
141 mk.innerHTML = "&#x25bc;";
142 } else {
143 sub.style.display = "none";
144 mk.innerHTML = "&#x25ba;";
145 }
146 }
147 </script>
148 </div>
149
150
151
152
153
154
155 <h1 id="the_explain_query_plan_command"><span>1. </span>The EXPLAIN QUERY PLAN Command</h1>
156
157 <p style="margin-left:10ex;margin-right:10ex">
158 <b>Warning:</b> The data returned by the EXPLAIN QUERY PLAN command is
159 intended for interactive debugging only. The output format may change
160 between SQLite releases. Applications should not depend on the output
161 format of the EXPLAIN QUERY PLAN command.
162
163 </p><p style="margin-left:10ex;margin-right:10ex">
164 <b>Alert:</b> As warned above, the EXPLAIN QUERY PLAN output format did
165 change substantially with the version 3.24.0 release (2018-06-04).
166 Additional minor changes occurred in version 3.36.0 (2021-06-18).
167 Further changes are possible in subsequent releases.
168
169
170 </p><p>The <a href="lang_explain.html">EXPLAIN QUERY PLAN</a> SQL command is used to obtain a high-level
171 description of the strategy or plan that SQLite uses to implement a specific
172 SQL query. Most significantly, EXPLAIN QUERY PLAN reports on the way in
173 which the query uses database indices. This document is a guide to
174 understanding and interpreting the EXPLAIN QUERY PLAN output. Background
175 information is available separately:
176
177 </p><ul>
178 <li> A primer on <a href="howitworks.html">How SQLite Works</a>.
179 </li><li> Notes on the <a href="optoverview.html">query optimizer</a>.
180 </li><li> How <a href="queryplanner.html">indexing</a> works.
181 </li><li> The <a href="queryplanner-ng.html">next generation query planner</a>.
182 </li></ul>
183
184 <p>A query plan is represented as a tree.
185 In raw form, as returned by <a href="c3ref/step.html">sqlite3_step()</a>, each node of the tree
186 consists of four fields: An integer node id, an integer parent id,
187 an auxiliary integer field that is not currently used, and a description
188 of the node.
189 The entire tree is therefore a table with four columns and zero or more
190 rows.
191 The <a href="cli.html">command-line shell</a> will usually intercept this table and renders
192 it as an ASCII-art graph for more convenient viewing. To disable the
193 shells automatic graph rendering and to display EXPLAIN QUERY PLAN
194 output in its tabular format, run the command ".explain off" to set
195 the "EXPLAIN formatting mode" to off. To restore automatic graph rendering,
196 run ".explain auto". You can see the current "EXPLAIN formatting mode"
197 setting using the ".show" command.
198
199 </p><p>One can also set the <a href="cli.html">CLI</a> into automatic EXPLAIN QUERY PLAN mode
200 using the ".eqp on" command:
201
202 </p><div class="codeblock"><pre>sqlite&gt; .eqp on
203 </pre></div>
204
205 <p> In automatic EXPLAIN QUERY PLAN mode, the shell automatically runs
206 a separate EXPLAIN QUERY PLAN query for each statement you enter and
207 displays the result before actually running the query. Use the
208 ".eqp off" command to turn automatic EXPLAIN QUERY PLAN mode back off.
209
210 </p><p>EXPLAIN QUERY PLAN is most useful on a SELECT statement,
211 but may also appear with other statements that read data from database
212 tables (e.g. UPDATE, DELETE, INSERT INTO ... SELECT).
213
214 </p><h2 id="table_and_index_scans"><span>1.1. </span>Table and Index Scans</h2>
215
216 <p>
217 When processing a SELECT (or other) statement, SQLite may retrieve data from
218 database tables in a variety of ways. It may scan through all the records in
219 a table (a full-table scan), scan a contiguous subset of the records in a
220 table based on the rowid index, scan a contiguous subset of the entries in a
221 database <a href="lang_createtable.html">index</a>, or use a combination of the above strategies
222 in a single scan. The various ways in which SQLite may retrieve data from a
223 table or index are described in detail <a href="queryplanner.html#searching">here</a>.
224
225 </p><p>
226 For each table read by the query, the output of EXPLAIN QUERY
227 PLAN includes a record for which the value in the "detail" column begins
228 with either "SCAN" or "SEARCH". "SCAN" is used for a full-table scan,
229 including cases where SQLite iterates through all records in a table
230 in an order defined by an index. "SEARCH" indicates that only a subset of
231 the table rows are visited. Each SCAN or SEARCH record includes the
232 following information:
233
234 </p><ul>
235 <li> The name of the table, view, or subquery that data is read from.
236 </li><li> Whether or not an index or <a href="optoverview.html#autoindex">automatic index</a> is used.
237 </li><li> Whether or not the <a href="queryplanner.html#covidx">covering index</a> optimization applies.
238 </li><li> Which terms of the WHERE clause are used for indexing.
239 </li></ul>
240
241 <p>
242 For example, the following EXPLAIN QUERY PLAN command operates on a SELECT
243 statement that is implemented by performing a full-table scan on table t1:
244 </p><div class="codeblock"><pre>sqlite&gt; EXPLAIN QUERY PLAN SELECT a, b FROM t1 WHERE a=1;
245 QUERY PLAN
246 `--SCAN t1
247 </pre></div>
248
249 <p>
250 The example above shows
251 SQLite picking full-table scan will visit all rows in the table.
252 If the query were able to use an index, then the
253 SCAN/SEARCH record would include the name of the index and, for a
254 SEARCH record, an indication of how the subset of rows visited is
255 identified. For example:
256 </p><div class="codeblock"><pre>sqlite&gt; CREATE INDEX i1 ON t1(a);
257 sqlite&gt; EXPLAIN QUERY PLAN SELECT a, b FROM t1 WHERE a=1;
258 QUERY PLAN
259 `--SEARCH t1 USING INDEX i1 (a=?)
260 </pre></div>
261
262 <p>
263 The previous example, SQLite uses index "i1" to optimize
264 a WHERE clause term of the form (a=?) - in this case "a=1".
265 The previous example could not use a <a href="queryplanner.html#covidx">covering index</a>, but the following
266 example can, and that fact is reflected in the output:
267 </p><div class="codeblock"><pre>sqlite&gt; CREATE INDEX i2 ON t1(a, b);
268 sqlite&gt; EXPLAIN QUERY PLAN SELECT a, b FROM t1 WHERE a=1;
269 QUERY PLAN
270 `--SEARCH t1 USING COVERING INDEX i2 (a=?)
271 </pre></div>
272
273 <p>
274 All joins in SQLite are <a href="optoverview.html#joins">implemented using nested scans</a>. When a
275 SELECT query that features a join is analyzed using EXPLAIN QUERY PLAN, one
276 SCAN or SEARCH record is output for each nested loop. For example:
277 </p><div class="codeblock"><pre>sqlite&gt; EXPLAIN QUERY PLAN SELECT t1.*, t2.* FROM t1, t2 WHERE t1.a=1 AND t1.b>2;
278 QUERY PLAN
279 |--SEARCH t1 USING INDEX i2 (a=? AND b>?)
280 `--SCAN t2
281 </pre></div>
282
283 <p>
284 The order of the entries indicates the nesting order. In
285 this case, the scan of table t1 using index i2 is the outer loop (since it
286 appears first)
287 and the full-table scan of table t2 is the inner loop (since it appears
288 last).
289 In the following example, the positions of t1 and t2 in the FROM
290 clause of the SELECT are reversed. The query strategy remains the same.
291 The output from EXPLAIN QUERY PLAN shows how the query is actually
292 evaluated, not how it is specified in the SQL statement.
293 </p><div class="codeblock"><pre>sqlite&gt; EXPLAIN QUERY PLAN SELECT t1.*, t2.* FROM t2, t1 WHERE t1.a=1 AND t1.b>2;
294 QUERY PLAN
295 |--SEARCH t1 USING INDEX i2 (a=? AND b>?)
296 `--SCAN t2
297 </pre></div>
298
299 <a name="or-opt"></a>
300
301 <p>
302 If the WHERE clause of a query contains an OR expression, then SQLite might
303 use the <a href="queryplanner.html#or_in_where">"OR by union"</a> strategy (also known as the
304 <a href="optoverview.html#or_opt">OR optimization</a>). In this case there will be single top-level record
305 for the search, with two sub-records, one for each index:
306 </p><div class="codeblock"><pre>sqlite&gt; CREATE INDEX i3 ON t1(b);
307 sqlite&gt; EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=1 OR b=2;
308 QUERY PLAN
309 `--MULTI-INDEX OR
310 |--SEARCH t1 USING COVERING INDEX i2 (a=?)
311 `--SEARCH t1 USING INDEX i3 (b=?)
312 </pre></div>
313
314 <h2 id="temporary_sorting_b_trees"><span>1.2. </span>Temporary Sorting B-Trees</h2>
315
316 <p>
317 If a SELECT query contains an ORDER BY, GROUP BY or DISTINCT clause,
318 SQLite may need to use a temporary b-tree structure to sort the output
319 rows. Or, it might <a href="queryplanner.html#sorting">use an index</a>. Using an index is
320 almost always much more efficient than performing a sort.
321 If a temporary b-tree is required, a record is added to the EXPLAIN
322 QUERY PLAN output with the "detail" field set to a string value of
323 the form "USE TEMP B-TREE FOR xxx", where xxx is one of "ORDER BY",
324 "GROUP BY" or "DISTINCT". For example:
325
326 </p><div class="codeblock"><pre>sqlite&gt; EXPLAIN QUERY PLAN SELECT c, d FROM t2 ORDER BY c;
327 QUERY PLAN
328 |--SCAN t2
329 `--USE TEMP B-TREE FOR ORDER BY
330 </pre></div>
331
332 <p>
333 In this case using the temporary b-tree can be avoided by creating an index
334 on t2(c), as follows:
335
336 </p><div class="codeblock"><pre>sqlite&gt; CREATE INDEX i4 ON t2(c);
337 sqlite&gt; EXPLAIN QUERY PLAN SELECT c, d FROM t2 ORDER BY c;
338 QUERY PLAN
339 `--SCAN t2 USING INDEX i4
340 </pre></div>
341
342 <h2 id="subqueries"><span>1.3. </span>Subqueries</h2>
343
344 <p>
345 In all the examples above, there has only been a single SELECT statement.
346 If a query contains sub-selects, those are shown as being children of
347 the outer SELECT. For example:
348
349 </p><div class="codeblock"><pre>sqlite&gt; EXPLAIN QUERY PLAN SELECT (SELECT b FROM t1 WHERE a=0), (SELECT a FROM t1 WHERE b=t2.c) FROM t2;
350 |--SCAN TABLE t2 USING COVERING INDEX i4
351 |--SCALAR SUBQUERY
352 | `--SEARCH t1 USING COVERING INDEX i2 (a=?)
353 `--CORRELATED SCALAR SUBQUERY
354 `--SEARCH t1 USING INDEX i3 (b=?)
355 </pre></div>
356
357 <p>
358 The example above contains two "SCALAR" subqueries. The subqueries
359 are SCALAR in the sense that they return a single value - a one-row,
360 one-column table. If the actual query returns more than that, then
361 only the first column of the first row is used.
362 </p><p>
363 The first subquery above is constant with respect to the outer query.
364 The value for the first subquery can be computed once and then reused
365 for each row of the outer SELECT. The second subquery, however, is
366 "CORRELATED". The value of the second subquery changes depending
367 on values in the current row of the outer query. Hence, the second
368 subquery must be run once for each output row in the outer SELECT.
369
370 </p><p>
371 Unless the <a href="optoverview.html#flattening">flattening optimization</a> is applied, if a subquery appears in
372 the FROM clause of a SELECT statement, SQLite can either run the subquery and
373 stores the results in a temporary table, or it can run the subquery as a
374 co-routine. The following query is an example of the latter. The subquery
375 is run by a co-routine. The outer query blocks whenever it needs another
376 row of input from the subquery. Control switches to the co-routine which
377 produces the desired output row, then control switches back to the main
378 routine which continues processing.
379
380 </p><div class="codeblock"><pre>sqlite&gt; EXPLAIN QUERY PLAN SELECT count(*)
381 &gt; FROM (SELECT max(b) AS x FROM t1 GROUP BY a) AS qqq
382 &gt; GROUP BY x;
383 QUERY PLAN
384 |--CO-ROUTINE qqq
385 | `--SCAN t1 USING COVERING INDEX i2
386 |--SCAN qqqq
387 `--USE TEMP B-TREE FOR GROUP BY
388 </pre></div>
389
390 <p>
391
392 If the <a href="optoverview.html#flattening">flattening optimization</a> is used on a subquery in the FROM clause
393 of a SELECT statement, that effectively merges the subquery into the outer
394 query. The output of EXPLAIN QUERY PLAN reflects this, as in the following
395 example:
396
397 </p><div class="codeblock"><pre>sqlite&gt; EXPLAIN QUERY PLAN SELECT * FROM (SELECT * FROM t2 WHERE c=1) AS t3, t1;
398 QUERY PLAN
399 |--SEARCH t2 USING INDEX i4 (c=?)
400 `--SCAN t1
401 </pre></div>
402
403 <p>
404
405 If the content of a subquery might need to be visited more than once, then
406 the use of a co-routine is undesirable, as the co-routine would then have to
407 compute the data more than once. And if the subquery cannot be flattened,
408 that means the subquery must be manifested into a transient table.
409
410 </p><div class="codeblock"><pre>sqlite&gt; SELECT * FROM
411 &gt; (SELECT * FROM t1 WHERE a=1 ORDER BY b LIMIT 2) AS x,
412 &gt; (SELECT * FROM t2 WHERE c=1 ORDER BY d LIMIT 2) AS y;
413 QUERY PLAN
414 |--MATERIALIZE x
415 | `--SEARCH t1 USING COVERING INDEX i2 (a=?)
416 |--MATERIALIZE y
417 | |--SEARCH t2 USING INDEX i4 (c=?)
418 | `--USE TEMP B-TREE FOR ORDER BY
419 |--SCAN x
420 `--SCAN y
421 </pre></div>
422
423 <h2 id="compound_queries"><span>1.4. </span>Compound Queries</h2>
424
425 <p>
426 Each component query of a <a href="lang_select.html#compound">compound query</a> (UNION, UNION ALL, EXCEPT or
427 INTERSECT) is assigned computed separately and is given its own line in
428 the EXPLAIN QUERY PLAN output.
429
430 </p><div class="codeblock"><pre>sqlite&gt; EXPLAIN QUERY PLAN SELECT a FROM t1 UNION SELECT c FROM t2;
431 QUERY PLAN
432 `--COMPOUND QUERY
433 |--LEFT-MOST SUBQUERY
434 | `--SCAN t1 USING COVERING INDEX i1
435 `--UNION USING TEMP B-TREE
436 `--SCAN t2 USING COVERING INDEX i4
437 </pre></div>
438
439 <p>
440 The "USING TEMP B-TREE" clause in the above output indicates that a
441 temporary b-tree structure is used to implement the UNION of the results
442 of the two sub-selects. An alternative method of computing a compound
443 is to run each subquery as a co-routine, arrange for their outputs to
444 appear in sorted order, and merge the results together. When the query
445 planner chooses this latter approach, the EXPLAIN QUERY PLAN output
446 looks like this:
447
448 </p><div class="codeblock"><pre>sqlite&gt; EXPLAIN QUERY PLAN SELECT a FROM t1 EXCEPT SELECT d FROM t2 ORDER BY 1;
449 QUERY PLAN
450 `--MERGE (EXCEPT)
451 |--LEFT
452 | `--SCAN t1 USING COVERING INDEX i1
453 `--RIGHT
454 |--SCAN t2
455 `--USE TEMP B-TREE FOR ORDER BY
456 </pre></div>
457 <p align="center"><small><i>This page last modified on <a href="https://sqlite.org/docsrc/honeypot" id="mtimelink" data-href="https://sqlite.org/docsrc/finfo/pages/eqp.in?m=719c38ad7e">2022-01-08 05:02:57</a> UTC </small></i></p>
458