3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
13 set testdir [file dirname $argv0]
14 source $testdir/tester.tcl
15 set testprefix scanstatus
17 ifcapable !scanstatus {
23 CREATE TABLE t1(a, b);
24 CREATE TABLE t2(x, y);
25 INSERT INTO t1 VALUES(1, 2);
26 INSERT INTO t1 VALUES(3, 4);
27 INSERT INTO t2 VALUES('a', 'b');
28 INSERT INTO t2 VALUES('c', 'd');
29 INSERT INTO t2 VALUES('e', 'f');
32 proc do_scanstatus_test {tn res} {
33 set stmt [db version -last-stmt-ptr]
37 set r [sqlite3_stmt_scanstatus $stmt $idx]
38 if {[llength $r]==0} break
39 foreach v {nLoop nVisit nEst zName zExplain} {
40 lappend ret $v [dict get $r $v]
45 uplevel [list do_test $tn [list set {} $ret] [list {*}$res]]
48 do_execsql_test 1.1 { SELECT count(*) FROM t1, t2; } 6
49 do_scanstatus_test 1.2 {
50 nLoop 1 nVisit 2 nEst 1048576.0 zName t1 zExplain {SCAN t1}
51 nLoop 2 nVisit 6 nEst 1048576.0 zName t2 zExplain {SCAN t2}
56 SELECT count(*) FROM t1, t2;
58 do_scanstatus_test 1.4 {
59 nLoop 1 nVisit 2 nEst 2.0 zName t1 zExplain {SCAN t1}
60 nLoop 2 nVisit 6 nEst 3.0 zName t2 zExplain {SCAN t2}
63 do_execsql_test 1.5 { ANALYZE }
65 SELECT count(*) FROM t1, t2 WHERE t2.rowid>1;
67 do_scanstatus_test 1.7 {
68 nLoop 1 nVisit 2 nEst 2.0 zName t2 zExplain
69 {SEARCH t2 USING INTEGER PRIMARY KEY (rowid>?)}
70 nLoop 2 nVisit 4 nEst 2.0 zName t1 zExplain {SCAN t1}
74 SELECT count(*) FROM t1, t2 WHERE t2.rowid>1;
77 do_scanstatus_test 1.9 {
78 nLoop 2 nVisit 4 nEst 2.0 zName t2 zExplain
79 {SEARCH t2 USING INTEGER PRIMARY KEY (rowid>?)}
80 nLoop 4 nVisit 8 nEst 2.0 zName t1 zExplain {SCAN t1}
84 sqlite3_stmt_scanstatus_reset [db version -last-stmt-ptr]
87 do_scanstatus_test 1.10 {
88 nLoop 0 nVisit 0 nEst 2.0 zName t2 zExplain
89 {SEARCH t2 USING INTEGER PRIMARY KEY (rowid>?)}
90 nLoop 0 nVisit 0 nEst 2.0 zName t1 zExplain {SCAN t1}
93 #-------------------------------------------------------------------------
94 # Try a few different types of scans.
98 CREATE TABLE x1(i INTEGER PRIMARY KEY, j);
99 INSERT INTO x1 VALUES(1, 'one');
100 INSERT INTO x1 VALUES(2, 'two');
101 INSERT INTO x1 VALUES(3, 'three');
102 INSERT INTO x1 VALUES(4, 'four');
103 CREATE INDEX x1j ON x1(j);
105 SELECT * FROM x1 WHERE i=2;
108 do_scanstatus_test 2.2 {
109 nLoop 1 nVisit 1 nEst 1.0 zName x1
110 zExplain {SEARCH x1 USING INTEGER PRIMARY KEY (rowid=?)}
113 do_execsql_test 2.3.1 {
114 SELECT * FROM x1 WHERE j='two'
116 do_scanstatus_test 2.3.2 {
117 nLoop 1 nVisit 1 nEst 10.0 zName x1j
118 zExplain {SEARCH x1 USING COVERING INDEX x1j (j=?)}
121 do_execsql_test 2.4.1 {
122 SELECT * FROM x1 WHERE j<'two'
123 } {4 four 1 one 3 three}
124 do_scanstatus_test 2.4.2 {
125 nLoop 1 nVisit 3 nEst 262144.0 zName x1j
126 zExplain {SEARCH x1 USING COVERING INDEX x1j (j<?)}
129 do_execsql_test 2.5.1 {
130 SELECT * FROM x1 WHERE j>='two'
132 do_scanstatus_test 2.5.2 {
133 nLoop 1 nVisit 1 nEst 262144.0 zName x1j
134 zExplain {SEARCH x1 USING COVERING INDEX x1j (j>?)}
137 do_execsql_test 2.6.1 {
138 SELECT * FROM x1 WHERE j BETWEEN 'three' AND 'two'
140 do_scanstatus_test 2.6.2 {
141 nLoop 1 nVisit 2 nEst 16384.0 zName x1j
142 zExplain {SEARCH x1 USING COVERING INDEX x1j (j>? AND j<?)}
145 do_execsql_test 2.7.1 {
146 CREATE TABLE x2(i INTEGER, j, k);
147 INSERT INTO x2 SELECT i, j, i || ' ' || j FROM x1;
148 CREATE INDEX x2j ON x2(j);
149 CREATE INDEX x2ij ON x2(i, j);
150 SELECT * FROM x2 WHERE j BETWEEN 'three' AND 'two'
151 } {3 three {3 three} 2 two {2 two}}
153 do_scanstatus_test 2.7.2 {
154 nLoop 1 nVisit 2 nEst 16384.0 zName x2j
155 zExplain {SEARCH x2 USING INDEX x2j (j>? AND j<?)}
158 do_execsql_test 2.8.1 {
159 SELECT * FROM x2 WHERE i=1 AND j='two'
161 do_scanstatus_test 2.8.2 {
162 nLoop 1 nVisit 0 nEst 8.0 zName x2ij
163 zExplain {SEARCH x2 USING INDEX x2ij (i=? AND j=?)}
166 do_execsql_test 2.9.1 {
167 SELECT * FROM x2 WHERE i=5 AND j='two'
169 do_scanstatus_test 2.9.2 {
170 nLoop 1 nVisit 0 nEst 8.0 zName x2ij
171 zExplain {SEARCH x2 USING INDEX x2ij (i=? AND j=?)}
174 do_execsql_test 2.10.1 {
175 SELECT * FROM x2 WHERE i=3 AND j='three'
176 } {3 three {3 three}}
177 do_scanstatus_test 2.10.2 {
178 nLoop 1 nVisit 1 nEst 8.0 zName x2ij
179 zExplain {SEARCH x2 USING INDEX x2ij (i=? AND j=?)}
182 #-------------------------------------------------------------------------
183 # Try with queries that use the OR optimization.
185 do_execsql_test 3.1 {
186 CREATE TABLE a1(a, b, c, d);
187 CREATE INDEX a1a ON a1(a);
188 CREATE INDEX a1bc ON a1(b, c);
190 WITH d(x) AS (SELECT 1 UNION ALL SELECT x+1 AS n FROM d WHERE n<=100)
191 INSERT INTO a1 SELECT x, x, x, x FROM d;
194 do_execsql_test 3.2.1 {
195 SELECT d FROM a1 WHERE (a=4 OR b=13)
197 do_scanstatus_test 3.2.2 {
198 nLoop 1 nVisit 1 nEst 10.0 zName a1a
199 zExplain {SEARCH a1 USING INDEX a1a (a=?)}
200 nLoop 1 nVisit 1 nEst 10.0 zName a1bc
201 zExplain {SEARCH a1 USING INDEX a1bc (b=?)}
204 do_execsql_test 3.2.1 {
205 SELECT count(*) FROM a1 WHERE (a BETWEEN 4 AND 12) OR (b BETWEEN 40 AND 60)
207 do_scanstatus_test 3.2.2 {
208 nLoop 1 nVisit 9 nEst 16384.0 zName a1a
209 zExplain {SEARCH a1 USING INDEX a1a (a>? AND a<?)}
210 nLoop 1 nVisit 21 nEst 16384.0 zName a1bc
211 zExplain {SEARCH a1 USING INDEX a1bc (b>? AND b<?)}
214 do_execsql_test 3.3.1 {
215 SELECT count(*) FROM a1 AS x, a1 AS y
216 WHERE (x.a BETWEEN 4 AND 12) AND (y.b BETWEEN 1 AND 10)
218 do_scanstatus_test 3.2.2 {
219 nLoop 1 nVisit 10 nEst 16384.0 zName a1bc
220 zExplain {SEARCH y USING COVERING INDEX a1bc (b>? AND b<?)}
221 nLoop 10 nVisit 90 nEst 16384.0 zName a1a
222 zExplain {SEARCH x USING COVERING INDEX a1a (a>? AND a<?)}
225 do_execsql_test 3.4.1 {
226 SELECT count(*) FROM a1 WHERE a IN (1, 5, 10, 15);
228 do_scanstatus_test 3.4.2 {
229 nLoop 1 nVisit 4 nEst 40.0 zName a1a
230 zExplain {SEARCH a1 USING COVERING INDEX a1a (a=?)}
233 do_execsql_test 3.4.1 {
234 SELECT count(*) FROM a1 WHERE rowid IN (1, 5, 10, 15);
236 do_scanstatus_test 3.4.2 {
237 nLoop 1 nVisit 4 nEst 4.0 zName a1
238 zExplain {SEARCH a1 USING INTEGER PRIMARY KEY (rowid=?)}
241 #-------------------------------------------------------------------------
242 # Test that scanstatus() data is not available for searches performed
245 # It is available for searches performed as part of FK processing, but
246 # not FK action processing.
248 do_execsql_test 4.0 {
249 CREATE TABLE t1(a, b, c);
250 CREATE TABLE t2(x PRIMARY KEY, y, z);
251 CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN
252 SELECT * FROM t2 WHERE x BETWEEN 20 AND 40;
254 WITH d(x) AS (SELECT 1 UNION ALL SELECT x+1 AS n FROM d WHERE n<=100)
255 INSERT INTO t2 SELECT x, x*2, x*3 FROM d;
258 do_execsql_test 4.1.1 { INSERT INTO t1 VALUES(1, 2, 3); }
259 do_scanstatus_test 4.1.2 {}
261 do_execsql_test 4.2 {
262 CREATE TABLE p1(x PRIMARY KEY);
263 INSERT INTO p1 VALUES(1), (2), (3), (4);
264 CREATE TABLE c1(y REFERENCES p1);
265 INSERT INTO c1 VALUES(1), (2), (3);
266 PRAGMA foreign_keys=on;
268 do_execsql_test 4.2.1 { DELETE FROM p1 WHERE x=4 }
269 do_scanstatus_test 4.2.2 {
270 nLoop 1 nVisit 1 nEst 1.0 zName sqlite_autoindex_p1_1
271 zExplain {SEARCH p1 USING INDEX sqlite_autoindex_p1_1 (x=?)}
273 nLoop 1 nVisit 3 nEst 262144.0 zName c1 zExplain {SCAN c1}
276 #-------------------------------------------------------------------------
277 # Further tests of different scan types.
281 set alphabet {a b c d e f g h i j k l m n o p q r s t u v w x y z}
282 return [lindex $alphabet [expr $i % [llength $alphabet]]]
284 db func tochar tochar
285 do_execsql_test 5.0 {
286 CREATE TABLE t1(a PRIMARY KEY, b, c);
287 INSERT INTO t1 VALUES(0, 1, 'a');
288 INSERT INTO t1 VALUES(1, 0, 'b');
289 INSERT INTO t1 VALUES(2, 1, 'c');
290 INSERT INTO t1 VALUES(3, 0, 'd');
291 INSERT INTO t1 VALUES(4, 1, 'e');
292 INSERT INTO t1 VALUES(5, 0, 'a');
293 INSERT INTO t1 VALUES(6, 1, 'b');
294 INSERT INTO t1 VALUES(7, 0, 'c');
295 INSERT INTO t1 VALUES(8, 1, 'd');
296 INSERT INTO t1 VALUES(9, 0, 'e');
297 CREATE INDEX t1bc ON t1(b, c);
299 CREATE TABLE t2(x, y);
300 CREATE INDEX t2xy ON t2(x, y);
301 WITH data(i, x, y) AS (
302 SELECT 0, 0, tochar(0)
304 SELECT i+1, (i+1)%2, tochar(i+1) FROM data WHERE i<500
305 ) INSERT INTO t2 SELECT x, y FROM data;
307 CREATE TABLE t3(x, y);
308 INSERT INTO t3 SELECT * FROM t2;
313 do_execsql_test 5.1.1 {
314 SELECT count(*) FROM t1 WHERE a IN (SELECT b FROM t1 AS ii)
317 do_scanstatus_test 5.1.2 {
318 nLoop 1 nVisit 10 nEst 10.0 zName t1
320 nLoop 1 nVisit 2 nEst 8.0 zName sqlite_autoindex_t1_1
321 zExplain {SEARCH t1 USING COVERING INDEX sqlite_autoindex_t1_1 (a=?)}
325 do_execsql_test 5.2.1 {
326 SELECT count(*) FROM t1 WHERE a IN (0, 1)
328 do_scanstatus_test 5.2.2 {
329 nLoop 1 nVisit 2 nEst 2.0 zName sqlite_autoindex_t1_1
330 zExplain {SEARCH t1 USING COVERING INDEX sqlite_autoindex_t1_1 (a=?)}
334 SELECT count(*) FROM t2 WHERE y = 'j';
335 } {SEARCH t2 USING COVERING INDEX t2xy (ANY(x) AND y=?)}
336 do_execsql_test 5.3.2 {
337 SELECT count(*) FROM t2 WHERE y = 'j';
339 do_scanstatus_test 5.3.3 {
340 nLoop 1 nVisit 19 nEst 56.0 zName t2xy zExplain
341 {SEARCH t2 USING COVERING INDEX t2xy (ANY(x) AND y=?)}
346 SELECT count(*) FROM t1, t2 WHERE y = c;
350 `--SEARCH t2 USING COVERING INDEX t2xy (ANY(x) AND y=?)
352 do_execsql_test 5.4.2 {
353 SELECT count(*) FROM t1, t2 WHERE y = c;
355 do_scanstatus_test 5.4.3 {
356 nLoop 1 nVisit 10 nEst 10.0 zName t1
358 nLoop 10 nVisit 200 nEst 56.0 zName t2xy
359 zExplain {SEARCH t2 USING COVERING INDEX t2xy (ANY(x) AND y=?)}
364 SELECT count(*) FROM t1, t3 WHERE y = c;
368 `--SEARCH t1 USING AUTOMATIC COVERING INDEX (c=?)
370 do_execsql_test 5.5.2 {
371 SELECT count(*) FROM t1, t3 WHERE y = c;
373 do_scanstatus_test 5.5.3 {
374 nLoop 1 nVisit 501 nEst 480.0 zName t3 zExplain {SCAN t3}
375 nLoop 501 nVisit 200 nEst 20.0 zName auto-index zExplain
376 {SEARCH t1 USING AUTOMATIC COVERING INDEX (c=?)}
379 #-------------------------------------------------------------------------
380 # Virtual table scans
383 do_execsql_test 6.0 {
384 CREATE VIRTUAL TABLE ft1 USING fts4;
385 INSERT INTO ft1 VALUES('a d c f g h e i f c');
386 INSERT INTO ft1 VALUES('g c h b g b f f f g');
387 INSERT INTO ft1 VALUES('h h c c h f a e d d');
388 INSERT INTO ft1 VALUES('e j i j i e b c f g');
389 INSERT INTO ft1 VALUES('g f b g j c h a d f');
390 INSERT INTO ft1 VALUES('j i a e g f a i a c');
391 INSERT INTO ft1 VALUES('f d g g j j c a h g');
392 INSERT INTO ft1 VALUES('b d h a d j j j b i');
393 INSERT INTO ft1 VALUES('j e a b j e c b c i');
394 INSERT INTO ft1 VALUES('a d e f b j j c g d');
396 do_execsql_test 6.1.1 {
397 SELECT count(*) FROM ft1 WHERE ft1 MATCH 'd'
399 do_scanstatus_test 6.1.2 {
400 nLoop 1 nVisit 6 nEst 24.0 zName ft1 zExplain
401 {SCAN ft1 VIRTUAL TABLE INDEX 3:}