Debian Codegraph @

www/profile.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>Profiling SQL Queries</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 Profiling SQL Queries
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="#_overview_">1. Overview </a></div>
129 <div class="fancy-toc1"><a href="#simple_cases_rows_loops_and_cycles">2. Simple Cases - Rows, Loops and Cycles</a></div>
130 <div class="fancy-toc1"><a href="#complex_cases_rows_loops_and_cycles">3. Complex Cases - Rows, Loops and Cycles</a></div>
131 <div class="fancy-toc1"><a href="#planner_estimates">4. Planner Estimates</a></div>
132 <div class="fancy-toc1"><a href="#low_level_profiling_data">5. Low-Level Profiling Data</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 <h1 id="_overview_"><span>1. </span> Overview </h1>
154
155 <p>SQLite contains built-in support for profiling SQL queries, but it is not
156 enabled by default. In order to enable support for query profiling, SQLite must
157 be compiled with the <a href="compile.html#enable_stmt_scanstatus">following option</a>:
158
159 </p><div class="codeblock"><pre>-DSQLITE_ENABLE_STMT_SCANSTATUS
160 </pre></div>
161
162 <p>Building SQLite with this option enables the <a href="c3ref/stmt_scanstatus.html">sqlite3_stmt_scanstatus_v2()</a>
163 API, which provides access to the various profiling metrics. The remainder of
164 this page discusses the profiling reports generated by the SQLite
165 <a href="cli.html">command-line shell</a> using these metrics, rather than the API directly.
166
167 </p><p>The profiling reports generated by the shell are very similar to the query
168 plan reports generated by the <a href="eqp.html">EXPLAIN QUERY PLAN</a> command. This page assumes
169 that the reader is familiar with this format.
170
171 </p><p>Within a command-line shell compiled with the option above, query profiling
172 is enabled using the ".scanstats on" command:
173
174 </p><div class="codeblock"><pre>sqlite> .scanstats on
175 </pre></div>
176
177 <p>Once enabled, the shell automatically outputs a query profile after each
178 SQL query executed. Query profiling can be disabled using ".scanstats off".
179 For example:
180
181 </p><div class="codeblock"><pre>sqlite> .scanstats on
182 sqlite> SELECT a FROM t1, t2 WHERE a IN (1,2,3) AND a=d+e;
183 QUERY PLAN (cycles=255831538 &#91;100%&#93;)
184 |--SEARCH t1 USING INTEGER PRIMARY KEY (rowid=?) (cycles=60048488 &#91;23%&#93; loops=1 rows=3)
185 `--SCAN t2 (cycles=133558052 &#91;52%&#93; loops=3 rows=150000)
186 </pre></div>
187
188 <h1 id="simple_cases_rows_loops_and_cycles"><span>2. </span>Simple Cases - Rows, Loops and Cycles</h1>
189
190 <p>Consider a database with the following schema:
191
192 </p><div class="codeblock"><pre>CREATE VIRTUAL TABLE ft USING fts5(text);
193 CREATE TABLE t1(a, b);
194 CREATE TABLE t2(c INTEGER PRIMARY KEY, d);
195 </pre></div>
196
197 <p>Then, after first executing ".scanstats on":
198
199 </p><div class="codeblock"><pre>sqlite3> SELECT * FROM t1, t2 WHERE t2.c=t1.a;
200 &lt;...query results...&gt;
201 QUERY PLAN (cycles=1140768 &#91;100%&#93;)
202 |--SCAN t1 (cycles=455974 &#91;40%&#93; loops=1 rows=500)
203 `--SEARCH t2 USING INTEGER PRIMARY KEY (rowid=?) (cycles=619820 &#91;54%&#93; loops=500 rows=250)
204 </pre></div>
205
206 <p>The text in the example above following the snipped
207 "&lt;...query results...&gt;" is the profile report for the join query just
208 executed. The parts of the profile report that are similar to the
209 <a href="eqp.html">EXPLAIN QUERY PLAN</a> output indicate that the query is implemented by doing a
210 full table-scan of table "t1", with a lookup by INTEGER PRIMARY KEY on table
211 "t2" for each row visited.
212
213 </p><p>The "loops=1" notation on the "SCAN t1" line indicates that this loop - the
214 full-table scan of table "t1" - ran exactly once. "rows=500" indicates that
215 that single scan visited 500 rows.
216
217 </p><p>The "SEARCH t2 USING ..." line contains the annotation "loops=500" to indicate
218 that this "loop" (really a lookup by INTEGER PRIMARY KEY) ran 500 times.
219 Which makes sense - it ran once for each row visited by the full-table scan
220 of "t1". "rows=250" means that, altogether, those 500 loops visited 250 rows.
221 In other words, only half of the INTEGER PRIMARY KEY lookups on table t2 were
222 successful, for the other half of the lookups there was no row to find.
223
224 </p><p>The loop-count for a SEARCH or SCAN entry is not necessarily the same as the
225 number of rows output by the outer loop. For example, if the query above
226 were modified as follows:
227
228 </p><div class="codeblock"><pre>sqlite3> SELECT * FROM t1, t2 WHERE t1.b&lt;=100 AND t2.c=t1.a;
229 &lt;...query results...&gt;
230 QUERY PLAN (cycles=561002 &#91;100%&#93;)
231 |--SCAN t1 (cycles=345950 &#91;62%&#93; loops=1 rows=500)
232 `--SEARCH t2 USING INTEGER PRIMARY KEY (rowid=?) (cycles=128690 &#91;23%&#93; loops=100 rows=50)
233 </pre></div>
234
235 <p>This time, even though the "SCAN t1" loop still visits 500 rows, the
236 "SEARCH t2" lookup is only performed 100 times. This is because SQLite
237 was able to discard rows from t1 that did not match the "t1.b&lt;=100"
238 constraint.
239
240 </p><p>The "cycles" measurements are based on the
241 <a href="https://en.wikipedia.org/wiki/Time_Stamp_Counter">CPU time-stamp counter
242 </a>, and so are a good proxy for wall-clock time. For the query above, the
243 total number of cycles was 561002. For each of the two loops ("SCAN t1..." and
244 "SEARCH t2..."), the cycles count represents the time spent in operations that
245 can be directly attributed to that loop only. Specifically, this is the time
246 spent navigating and extracting data from the database b-trees for that loop.
247 These values never quite add up to the total cycles for the query, as there are
248 other internal operations performed by SQLite that are not directly
249 attributable to either loop.
250
251 </p><p>The cycles count for the "SCAN t1" loop was 345950 - 62% of the total for the
252 query. The 100 lookups peformed by the "SEARCH t1" loop took 128690 cycles, 23%
253 of the total.
254
255 </p><p>When a virtual table is used, the "rows" and "loops" metrics have the same
256 meaning as for loops on regular SQLite tables. The "cycles" meaurement is the
257 total cycles consumed within virtual table methods associated with the loop.
258 For example:
259
260 </p><div class="codeblock"><pre>sqlite3> SELECT * FROM ft('sqlite'), t2 WHERE t2.c=ft.rowid;
261 &lt;...query results...&gt;
262 QUERY PLAN (cycles=836434 &#91;100%&#93;
263 |--SCAN ft VIRTUAL TABLE INDEX 0:M1 (cycles=739602 &#91;88%&#93; loops=1 rows=48)
264 `--SEARCH t2 USING INTEGER PRIMARY KEY (rowid=?) (cycles=62866 &#91;8%&#93; loops=48 rows=25)
265 </pre></div>
266
267 <p>In this case the single query (loops=1) on fts5 table "ft" returned 48 rows
268 (rows=48) and consumed 739602 cycles (cycles=739602), which was roughly 88% of
269 the total query time.
270
271 </p><h1 id="complex_cases_rows_loops_and_cycles"><span>3. </span>Complex Cases - Rows, Loops and Cycles</h1>
272
273 <p>Using the same schema as in the previous section, consider this more
274 complicated example:
275
276 </p><div class="codeblock"><pre>sqlite3&gt; WITH cnt(i) AS (
277 SELECT 1 UNION SELECT i+1 FROM cnt WHERE i&lt;100
278 )
279 SELECT
280 *, (SELECT d FROM t2 WHERE c=ft.rowid)
281 FROM
282 (SELECT count(*), a FROM t1 GROUP BY a) AS v1 CROSS JOIN
283 ft('sqlite'),
284 cnt
285 WHERE cnt.i=ft.rowid AND v1.a=ft.rowid;
286 &lt;...query results...&gt;
287 QUERY PLAN (cycles=177665334 &#91;100%&#93;)
288 |--CO-ROUTINE v1 (cycles=4500444 &#91;3%&#93;)
289 | |--SCAN t1 (cycles=397052 &#91;0%&#93; loops=1 rows=500)
290 | `--USE TEMP B-TREE FOR GROUP BY
291 |--MATERIALIZE cnt (cycles=1275068 &#91;1%&#93;)
292 | |--SETUP
293 | | `--SCAN CONSTANT ROW
294 | `--RECURSIVE STEP
295 | `--SCAN cnt (cycles=129166 &#91;0%&#93; loops=100 rows=100)
296 |--SCAN v1 (loops=1 rows=500)
297 |--SCAN ft VIRTUAL TABLE INDEX 0:M1= (cycles=161874340 &#91;91%&#93; loops=500 rows=271)
298 |--SCAN cnt (cycles=7336350 &#91;4%&#93; loops=95 rows=9500)
299 `--CORRELATED SCALAR SUBQUERY 3 (cycles=168538 &#91;0%&#93; loops=37)
300 `--SEARCH t2 USING INTEGER PRIMARY KEY (rowid=?) (cycles=94724 &#91;0%&#93; loops=37 rows=21)
301 </pre></div>
302
303 <p>The most complicated part of the example above is understanding the query
304 plan - the portion of the report that would also be generated by an
305 <a href="eqp.html">EXPLAIN QUERY PLAN</a> command. Other points of interest are:
306
307 </p><ul>
308 <li><p> Sub-query "v1" is implemented as a <a href="optoverview.html#coroutines">co-routine</a>.
309 In this case the sub-query is reported on separately, and a "cycles"
310 count is available for the entire sub-query. There is also a "SCAN v1"
311 line - this represents the invocation of the sub-query co-routine
312 from the main query. This entry has no cycles associated with it, as
313 the entire cost of the sub-query is attributed to the co-routine.
314 It does have "loops" and "rows" values - the sub-query is scanned
315 once and returns 500 rows.
316
317 </p></li><li><p>Recursive sub-query "cnt" is materialized (cached in a temp
318 table) before the main query is run. The entire cost of the materialization
319 is attributed to the "MATERIALIZE cnt" element. There is also a
320 "SCAN cnt" item representing the scans of the materialized sub-query.
321 The cycles value associated with this item represents the time taken
322 to scan the temp-table containing the materialized sub-query, which is
323 separate from the cycles used to populate it.
324
325 </p></li><li><p>There are cycles and loops measurements for the scalar sub-query
326 as well. These represent the total cycles consumed while executing the
327 sub-query and the number of times it was executed, respectively.
328
329 </p></li><li><p>Where one item is a parent of another, as in "CORRELATED SCALAR
330 SUBQUERY 3" and "SEARCH t2 USING...", then the cycles value associated
331 with the parent includes those cycles associated with all child elements.
332 In all cases, the percentage values relate to the total cycles used by
333 the query, not the cycles used by the parent.
334 </p></li></ul>
335
336 <p>The following query uses an <a href="optoverview.html#autoindex">automatic index</a> and
337 an external sort:
338
339 </p><div class="codeblock"><pre>sqlite> SELECT * FROM
340 t2,
341 (SELECT count(*) AS cnt, d FROM t2 GROUP BY d) AS v2
342 WHERE v2.d=t2.d AND t2.d>100
343 ORDER BY v2.cnt;
344 &lt;...query results...&gt;
345 QUERY PLAN (cycles=6234376 &#91;100%&#93;)
346 |--MATERIALIZE v2 (cycles=2351916 &#91;38%&#93;)
347 | |--SCAN t2 (cycles=188428 &#91;3%&#93; loops=1 rows=250)
348 | `--USE TEMP B-TREE FOR GROUP BY
349 |--SCAN t2 (cycles=455736 &#91;7%&#93; loops=1 rows=250)
350 |--CREATE AUTOMATIC INDEX ON v2(d, cnt) (cycles=1668380 &#91;27%&#93; loops=1 rows=250)
351 |--SEARCH v2 USING AUTOMATIC COVERING INDEX (d=?) (cycles=932824 &#91;15%&#93; loops=200 rows=200)
352 `--USE TEMP B-TREE FOR ORDER BY (cycles=662456 &#91;11%&#93; loops=1 rows=200)
353 </pre></div>
354
355 <p>Points of interest are:
356
357 </p><ul>
358 <li><p> This query materializes the sub-query into a temp table, then creates
359 an automatic (i.e. transient) index on it, then uses that index to optimize
360 the join. All three of these steps - "MATERIALIZE v2", "CREATE AUTOMATIC
361 INDEX" and "SEARCH ... USING AUTOMATIC INDEX" have separate cycle counts.
362 The "rows" associated with the "CREATE AUTOMATIC INDEX" line represents the
363 total number of rows included in the index. The "loops" and "rows" associated
364 with the "SEARCH ... USING AUTOMATIC INDEX" line represent the number of
365 lookups the index was used for and the total number of rows found by those
366 lookups.
367
368 </p></li><li><p> The external sort "USE TEMP B-TREE FOR ORDER BY" is also accounted
369 for separately. The cycles count represents the extra cycles consumed by
370 sorting the returned rows, above those that would have been used if the rows
371 were returned in arbitrary order. The rows count represents the number of
372 rows sorted.
373 </p></li></ul>
374
375 <h1 id="planner_estimates"><span>4. </span>Planner Estimates</h1>
376
377 <p>As well as ".scanstats on" to enable profiling and ".scanstats off" to
378 disable it, the shell also accepts ".scanstats est":
379
380 </p><div class="codeblock"><pre>sqlite> .scanstats est
381 </pre></div>
382
383 <p>This enables a special kind of profiling report that includes two extra
384 values associated with each "SCAN..." and "SEARCH..." element of a query
385 profile:
386
387 </p><ul>
388 <li> <b>rpl</b> (rows per loop): This is the value of the "rows" metric
389 divided by that of the "loops" metric.
390 </li><li> <b>est</b> (estimate): This is the number of rows per loop that the
391 query planner estimated would be returned by the current query
392 element. If this value is radically different from the actual
393 rows per loop value and query performance is unsatisfactory,
394 the quality of this estimate may be improved by running ANALYZE.
395 </li></ul>
396
397 <div class="codeblock"><pre>sqlite> SELECT a FROM t1, t2 WHERE a IN (1,2,3) AND a=d+e ORDER BY a;
398 &lt;query results...&gt;
399 QUERY PLAN (cycles=264725190 &#91;100%&#93;
400 |--SEARCH t1 USING INTEGER PRIMARY KEY (rowid=?) (cycles=60511568 &#91;23%&#93; loops=1 rows=3 rpl=3.0 est=3.0)
401 `--SCAN t2 (cycles=139461608 &#91;53%&#93; loops=3 rows=150000 rpl=50000.0 est=1048576.0)
402 </pre></div>
403
404 <h1 id="low_level_profiling_data"><span>5. </span>Low-Level Profiling Data</h1>
405
406
407
408 <p> Also supported is the ".scanstats vm" command. This enables a lower-level
409 profiling report showing the number of times each VM instruction was executed
410 and the percentage of clock-cycles that passed while it was executing:
411
412 </p><div class="codeblock"><pre>sqlite> .scanstats vm
413 </pre></div>
414
415 <p>Then:
416
417 </p><div class="codeblock"><pre>sqlite> SELECT count(*) FROM t2 WHERE (d % 5) = 0;
418 &lt;query results...&gt;
419 addr cycles nexec opcode p1 p2 p3 p4 p5 comment
420 ---- ------ ------ ------------- ---- ---- ---- ------------- -- -------------
421 0 0.0% 1 Init 1 18 0 0 Start at 18
422 1 0.0% 1 Null 0 1 1 0 r&#91;1..1]=NULL
423 2 0.0% 1 OpenRead 0 2 0 2 0 root=2 iDb=0; t2
424 3 0.0% 1 ColumnsUsed 0 0 0 2 0
425 4 0.0% 1 Explain 4 0 0 SCAN t2 0
426 5 0.0% 1 CursorHint 0 0 0 EQ(REM(c1,5),0) 0
427 6 0.0% 1 Rewind 0 14 0 0
428 7 46.86% 150000 Column 0 1 3 0 r&#91;3]= cursor 0 column 1
429 8 18.94% 150000 Remainder 4 3 2 0 r&#91;2]=r&#91;3]%r&#91;4]
430 9 5.41% 150000 ReleaseReg 3 1 0 0 release r&#91;3] mask 0
431 10 12.09% 150000 Ne 5 13 2 80 if r&#91;2]!=r&#91;5] goto 13
432 11 1.02% 30000 ReleaseReg 2 1 0 0 release r&#91;2] mask 0
433 12 2.95% 30000 AggStep1 0 0 1 count(0) 0 accum=r&#91;1] step(r&#91;0])
434 13 12.72% 150000 Next 0 7 0 1
435 14 0.0% 1 AggFinal 1 0 0 count(0) 0 accum=r&#91;1] N=0
436 15 0.0% 1 Copy 1 6 0 0 r&#91;6]=r&#91;1]
437 16 0.0% 1 ResultRow 6 1 0 0 output=r&#91;6]
438 17 0.01% 1 Halt 0 0 0 0
439 18 0.0% 1 Transaction 0 0 1 0 1 usesStmtJournal=0
440 19 0.0% 1 Integer 5 4 0 0 r&#91;4]=5
441 20 0.0% 1 Integer 0 5 0 0 r&#91;5]=0
442 21 0.0% 1 Goto 0 1 0 0
443 </pre></div>
444
445
446
447
448
449
450