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.svg" 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 <details class="fancy_toc">
124 <summary>Table Of Contents</summary>
125 <div id="toc_sub"><div class="fancy-toc1"><a href="#the_explain_query_plan_command">1. The EXPLAIN QUERY PLAN Command</a></div>
126 <div class="fancy-toc2"><a href="#table_and_index_scans">1.1. Table and Index Scans</a></div>
127 <div class="fancy-toc2"><a href="#temporary_sorting_b_trees">1.2. Temporary Sorting B-Trees</a></div>
128 <div class="fancy-toc2"><a href="#subqueries">1.3. Subqueries</a></div>
129 <div class="fancy-toc2"><a href="#compound_queries">1.4. Compound Queries</a></div>
130 </div>
131 </details>
132 </div>
133
134
135
136
137
138
139 <h1 id="the_explain_query_plan_command"><span>1. </span>The EXPLAIN QUERY PLAN Command</h1>
140
141 <p style="margin-left:10ex;margin-right:10ex">
142 <b>Warning:</b> The data returned by the EXPLAIN QUERY PLAN command is
143 intended for interactive debugging only. The output format may change
144 between SQLite releases. Applications should not depend on the output
145 format of the EXPLAIN QUERY PLAN command.
146
147 </p><p style="margin-left:10ex;margin-right:10ex">
148 <b>Alert:</b> As warned above, the EXPLAIN QUERY PLAN output format did
149 change substantially with the version 3.24.0 release (2018-06-04).
150 Additional minor changes occurred in version 3.36.0 (2021-06-18).
151 Further changes are possible in subsequent releases.
152
153
154 </p><p>The <a href="lang_explain.html">EXPLAIN QUERY PLAN</a> SQL command is used to obtain a high-level
155 description of the strategy or plan that SQLite uses to implement a specific
156 SQL query. Most significantly, EXPLAIN QUERY PLAN reports on the way in
157 which the query uses database indices. This document is a guide to
158 understanding and interpreting the EXPLAIN QUERY PLAN output. Background
159 information is available separately:
160
161 </p><ul>
162 <li> A primer on <a href="howitworks.html">How SQLite Works</a>.
163 </li><li> Notes on the <a href="optoverview.html">query optimizer</a>.
164 </li><li> How <a href="queryplanner.html">indexing</a> works.
165 </li><li> The <a href="queryplanner-ng.html">next generation query planner</a>.
166 </li></ul>
167
168 <p>A query plan is represented as a tree.
169 In raw form, as returned by <a href="c3ref/step.html">sqlite3_step()</a>, each node of the tree
170 consists of four fields: An integer node id, an integer parent id,
171 an auxiliary integer field that is not currently used, and a description
172 of the node.
173 The entire tree is therefore a table with four columns and zero or more
174 rows.
175 The <a href="cli.html">command-line shell</a> will usually intercept this table and renders
176 it as an ASCII-art graph for more convenient viewing. To disable the
177 shells automatic graph rendering and to display EXPLAIN QUERY PLAN
178 output in its tabular format, run the command ".explain off" to set
179 the "EXPLAIN formatting mode" to off. To restore automatic graph rendering,
180 run ".explain auto". You can see the current "EXPLAIN formatting mode"
181 setting using the ".show" command.
182
183 </p><p>One can also set the <a href="cli.html">CLI</a> into automatic EXPLAIN QUERY PLAN mode
184 using the ".eqp on" command:
185
186 </p><div class="codeblock"><pre>sqlite&gt; .eqp on
187 </pre></div>
188
189 <p> In automatic EXPLAIN QUERY PLAN mode, the shell automatically runs
190 a separate EXPLAIN QUERY PLAN query for each statement you enter and
191 displays the result before actually running the query. Use the
192 ".eqp off" command to turn automatic EXPLAIN QUERY PLAN mode back off.
193
194 </p><p>EXPLAIN QUERY PLAN is most useful on a SELECT statement,
195 but may also appear with other statements that read data from database
196 tables (e.g. UPDATE, DELETE, INSERT INTO ... SELECT).
197
198 </p><h2 id="table_and_index_scans"><span>1.1. </span>Table and Index Scans</h2>
199
200 <p>
201 When processing a SELECT (or other) statement, SQLite may retrieve data from
202 database tables in a variety of ways. It may scan through all the records in
203 a table (a full-table scan), scan a contiguous subset of the records in a
204 table based on the rowid index, scan a contiguous subset of the entries in a
205 database <a href="lang_createtable.html">index</a>, or use a combination of the above strategies
206 in a single scan. The various ways in which SQLite may retrieve data from a
207 table or index are described in detail <a href="queryplanner.html#searching">here</a>.
208
209 </p><p>
210 For each table read by the query, the output of EXPLAIN QUERY
211 PLAN includes a record for which the value in the "detail" column begins
212 with either "SCAN" or "SEARCH". "SCAN" is used for a full-table scan,
213 including cases where SQLite iterates through all records in a table
214 in an order defined by an index. "SEARCH" indicates that only a subset of
215 the table rows are visited. Each SCAN or SEARCH record includes the
216 following information:
217
218 </p><ul>
219 <li> The name of the table, view, or subquery that data is read from.
220 </li><li> Whether or not an index or <a href="optoverview.html#autoindex">automatic index</a> is used.
221 </li><li> Whether or not the <a href="queryplanner.html#covidx">covering index</a> optimization applies.
222 </li><li> Which terms of the WHERE clause are used for indexing.
223 </li></ul>
224
225 <p>
226 For example, the following EXPLAIN QUERY PLAN command operates on a SELECT
227 statement that is implemented by performing a full-table scan on table t1:
228 </p><div class="codeblock"><pre>sqlite&gt; EXPLAIN QUERY PLAN SELECT a, b FROM t1 WHERE a=1;
229 QUERY PLAN
230 `--SCAN t1
231 </pre></div>
232
233 <p>
234 If the query were able to use an index, then the
235 SCAN/SEARCH record would include the name of the index and, for a
236 SEARCH record, an indication of how the subset of rows visited is
237 identified. For example:
238 </p><div class="codeblock"><pre>sqlite&gt; CREATE INDEX i1 ON t1(a);
239 sqlite&gt; EXPLAIN QUERY PLAN SELECT a, b FROM t1 WHERE a=1;
240 QUERY PLAN
241 `--SEARCH t1 USING INDEX i1 (a=?)
242 </pre></div>
243
244 <p>
245 In the previous example, SQLite uses index "i1" to optimize
246 a WHERE clause term of the form (a=?) - in this case "a=1".
247 The previous example could not use a <a href="queryplanner.html#covidx">covering index</a>, but the following
248 example can, and that fact is reflected in the output:
249 </p><div class="codeblock"><pre>sqlite&gt; CREATE INDEX i2 ON t1(a, b);
250 sqlite&gt; EXPLAIN QUERY PLAN SELECT a, b FROM t1 WHERE a=1;
251 QUERY PLAN
252 `--SEARCH t1 USING COVERING INDEX i2 (a=?)
253 </pre></div>
254
255 <p>
256 All joins in SQLite are <a href="optoverview.html#joins">implemented using nested scans</a>. When a
257 SELECT query that features a join is analyzed using EXPLAIN QUERY PLAN, one
258 SCAN or SEARCH record is output for each nested loop. For example:
259 </p><div class="codeblock"><pre>sqlite&gt; EXPLAIN QUERY PLAN SELECT t1.*, t2.* FROM t1, t2 WHERE t1.a=1 AND t1.b>2;
260 QUERY PLAN
261 |--SEARCH t1 USING INDEX i2 (a=? AND b>?)
262 `--SCAN t2
263 </pre></div>
264
265 <p>
266 The order of the entries indicates the nesting order. In
267 this case, the scan of table t1 using index i2 is the outer loop (since it
268 appears first)
269 and the full-table scan of table t2 is the inner loop (since it appears
270 last).
271 In the following example, the positions of t1 and t2 in the FROM
272 clause of the SELECT are reversed. The query strategy remains the same.
273 The output from EXPLAIN QUERY PLAN shows how the query is actually
274 evaluated, not how it is specified in the SQL statement.
275 </p><div class="codeblock"><pre>sqlite&gt; EXPLAIN QUERY PLAN SELECT t1.*, t2.* FROM t2, t1 WHERE t1.a=1 AND t1.b>2;
276 QUERY PLAN
277 |--SEARCH t1 USING INDEX i2 (a=? AND b>?)
278 `--SCAN t2
279 </pre></div>
280
281 <a name="or-opt"></a>
282
283 <p>
284 If the WHERE clause of a query contains an OR expression, then SQLite might
285 use the <a href="queryplanner.html#or_in_where">"OR by union"</a> strategy (also known as the
286 <a href="optoverview.html#or_opt">OR optimization</a>). In this case there will be a single top-level record
287 for the search, with two sub-records, one for each index:
288 </p><div class="codeblock"><pre>sqlite&gt; CREATE INDEX i3 ON t1(b);
289 sqlite&gt; EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=1 OR b=2;
290 QUERY PLAN
291 `--MULTI-INDEX OR
292 |--SEARCH t1 USING COVERING INDEX i2 (a=?)
293 `--SEARCH t1 USING INDEX i3 (b=?)
294 </pre></div>
295
296 <h2 id="temporary_sorting_b_trees"><span>1.2. </span>Temporary Sorting B-Trees</h2>
297
298 <p>
299 If a SELECT query contains an ORDER BY, GROUP BY or DISTINCT clause,
300 SQLite may need to use a temporary b-tree structure to sort the output
301 rows. Or, it might <a href="queryplanner.html#sorting">use an index</a>. Using an index is
302 almost always much more efficient than performing a sort.
303 If a temporary b-tree is required, a record is added to the EXPLAIN
304 QUERY PLAN output with the "detail" field set to a string value of
305 the form "USE TEMP B-TREE FOR xxx", where xxx is one of "ORDER BY",
306 "GROUP BY" or "DISTINCT". For example:
307
308 </p><div class="codeblock"><pre>sqlite&gt; EXPLAIN QUERY PLAN SELECT c, d FROM t2 ORDER BY c;
309 QUERY PLAN
310 |--SCAN t2
311 `--USE TEMP B-TREE FOR ORDER BY
312 </pre></div>
313
314 <p>
315 In this case using the temporary b-tree can be avoided by creating an index
316 on t2(c), as follows:
317
318 </p><div class="codeblock"><pre>sqlite&gt; CREATE INDEX i4 ON t2(c);
319 sqlite&gt; EXPLAIN QUERY PLAN SELECT c, d FROM t2 ORDER BY c;
320 QUERY PLAN
321 `--SCAN t2 USING INDEX i4
322 </pre></div>
323
324 <h2 id="subqueries"><span>1.3. </span>Subqueries</h2>
325
326 <p>
327 In all the examples above, there has only been a single SELECT statement.
328 If a query contains sub-selects, those are shown as being children of
329 the outer SELECT. For example:
330
331 </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;
332 |--SCAN TABLE t2 USING COVERING INDEX i4
333 |--SCALAR SUBQUERY
334 | `--SEARCH t1 USING COVERING INDEX i2 (a=?)
335 `--CORRELATED SCALAR SUBQUERY
336 `--SEARCH t1 USING INDEX i3 (b=?)
337 </pre></div>
338
339 <p>
340 The example above contains two "SCALAR" subqueries. The subqueries
341 are SCALAR in the sense that they return a single value - a one-row,
342 one-column table. If the actual query returns more than that, then
343 only the first column of the first row is used.
344 </p><p>
345 The first subquery above is constant with respect to the outer query.
346 The value for the first subquery can be computed once and then reused
347 for each row of the outer SELECT. The second subquery, however, is
348 "CORRELATED". The value of the second subquery changes depending
349 on values in the current row of the outer query. Hence, the second
350 subquery must be run once for each output row in the outer SELECT.
351
352 </p><p>
353 Unless the <a href="optoverview.html#flattening">flattening optimization</a> is applied, if a subquery appears in
354 the FROM clause of a SELECT statement, SQLite can either run the subquery and
355 store the results in a temporary table, or it can run the subquery as a
356 co-routine. The following query is an example of the latter. The subquery
357 is run by a co-routine. The outer query blocks whenever it needs another
358 row of input from the subquery. Control switches to the co-routine which
359 produces the desired output row, then control switches back to the main
360 routine which continues processing.
361
362 </p><div class="codeblock"><pre>sqlite&gt; EXPLAIN QUERY PLAN SELECT count(*)
363 &gt; FROM (SELECT max(b) AS x FROM t1 GROUP BY a) AS qqq
364 &gt; GROUP BY x;
365 QUERY PLAN
366 |--CO-ROUTINE qqq
367 | `--SCAN t1 USING COVERING INDEX i2
368 |--SCAN qqqq
369 `--USE TEMP B-TREE FOR GROUP BY
370 </pre></div>
371
372 <p>
373
374 If the <a href="optoverview.html#flattening">flattening optimization</a> is used on a subquery in the FROM clause
375 of a SELECT statement, that effectively merges the subquery into the outer
376 query. The output of EXPLAIN QUERY PLAN reflects this, as in the following
377 example:
378
379 </p><div class="codeblock"><pre>sqlite&gt; EXPLAIN QUERY PLAN SELECT * FROM (SELECT * FROM t2 WHERE c=1) AS t3, t1;
380 QUERY PLAN
381 |--SEARCH t2 USING INDEX i4 (c=?)
382 `--SCAN t1
383 </pre></div>
384
385 <p>
386
387 If the content of a subquery might need to be visited more than once, then
388 the use of a co-routine is undesirable, as the co-routine would then have to
389 compute the data more than once. And if the subquery cannot be flattened,
390 that means the subquery must be manifested into a transient table.
391
392 </p><div class="codeblock"><pre>sqlite&gt; SELECT * FROM
393 &gt; (SELECT * FROM t1 WHERE a=1 ORDER BY b LIMIT 2) AS x,
394 &gt; (SELECT * FROM t2 WHERE c=1 ORDER BY d LIMIT 2) AS y;
395 QUERY PLAN
396 |--MATERIALIZE x
397 | `--SEARCH t1 USING COVERING INDEX i2 (a=?)
398 |--MATERIALIZE y
399 | |--SEARCH t2 USING INDEX i4 (c=?)
400 | `--USE TEMP B-TREE FOR ORDER BY
401 |--SCAN x
402 `--SCAN y
403 </pre></div>
404
405 <h2 id="compound_queries"><span>1.4. </span>Compound Queries</h2>
406
407 <p>
408 Each component query of a <a href="lang_select.html#compound">compound query</a> (UNION, UNION ALL, EXCEPT or
409 INTERSECT) is assigned computed separately and is given its own line in
410 the EXPLAIN QUERY PLAN output.
411
412 </p><div class="codeblock"><pre>sqlite&gt; EXPLAIN QUERY PLAN SELECT a FROM t1 UNION SELECT c FROM t2;
413 QUERY PLAN
414 `--COMPOUND QUERY
415 |--LEFT-MOST SUBQUERY
416 | `--SCAN t1 USING COVERING INDEX i1
417 `--UNION USING TEMP B-TREE
418 `--SCAN t2 USING COVERING INDEX i4
419 </pre></div>
420
421 <p>
422 The "USING TEMP B-TREE" clause in the above output indicates that a
423 temporary b-tree structure is used to implement the UNION of the results
424 of the two sub-selects. An alternative method of computing a compound
425 is to run each subquery as a co-routine, arrange for their outputs to
426 appear in sorted order, and merge the results together. When the query
427 planner chooses this latter approach, the EXPLAIN QUERY PLAN output
428 looks like this:
429
430 </p><div class="codeblock"><pre>sqlite&gt; EXPLAIN QUERY PLAN SELECT a FROM t1 EXCEPT SELECT d FROM t2 ORDER BY 1;
431 QUERY PLAN
432 `--MERGE (EXCEPT)
433 |--LEFT
434 | `--SCAN t1 USING COVERING INDEX i1
435 `--RIGHT
436 |--SCAN t2
437 `--USE TEMP B-TREE FOR ORDER BY
438 </pre></div>
439 <p align="center"><small><i>This page was last updated on 2025-05-31 13:08:22Z </small></i></p>
440