blob: caad70c5b45c158b8f60e654fb9f36525733a886 [file] [log] [blame]
dan04489b62014-10-31 20:11:32 +00001# 2014 November 1
2#
3# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
5#
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.
9#
10#***********************************************************************
11#
12
13set testdir [file dirname $argv0]
14source $testdir/tester.tcl
15set testprefix scanstatus
16
dane2f771b2014-11-03 15:33:17 +000017ifcapable !scanstatus {
18 finish_test
19 return
20}
21
dan04489b62014-10-31 20:11:32 +000022do_execsql_test 1.0 {
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');
30}
31
32proc do_scanstatus_test {tn res} {
drh1df64702017-10-13 15:56:26 +000033 set stmt [db version -last-stmt-ptr]
dan04489b62014-10-31 20:11:32 +000034 set idx 0
35 set ret [list]
36 while {1} {
37 set r [sqlite3_stmt_scanstatus $stmt $idx]
38 if {[llength $r]==0} break
dan231ff4b2022-12-02 20:32:22 +000039 foreach v {nLoop nVisit nEst zName zExplain} {
40 lappend ret $v [dict get $r $v]
41 }
dan04489b62014-10-31 20:11:32 +000042 incr idx
43 }
44
45 uplevel [list do_test $tn [list set {} $ret] [list {*}$res]]
46}
47
48do_execsql_test 1.1 { SELECT count(*) FROM t1, t2; } 6
49do_scanstatus_test 1.2 {
drh82102332021-03-20 15:11:29 +000050 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}
dan04489b62014-10-31 20:11:32 +000052}
53
54do_execsql_test 1.3 {
55 ANALYZE;
56 SELECT count(*) FROM t1, t2;
57} 6
58do_scanstatus_test 1.4 {
drh82102332021-03-20 15:11:29 +000059 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}
dan04489b62014-10-31 20:11:32 +000061}
62
dan89e71642014-11-01 18:08:04 +000063do_execsql_test 1.5 { ANALYZE }
64do_execsql_test 1.6 {
dan04489b62014-10-31 20:11:32 +000065 SELECT count(*) FROM t1, t2 WHERE t2.rowid>1;
66} 4
dan89e71642014-11-01 18:08:04 +000067do_scanstatus_test 1.7 {
drh518140e2014-11-06 03:55:10 +000068 nLoop 1 nVisit 2 nEst 2.0 zName t2 zExplain
drh82102332021-03-20 15:11:29 +000069 {SEARCH t2 USING INTEGER PRIMARY KEY (rowid>?)}
70 nLoop 2 nVisit 4 nEst 2.0 zName t1 zExplain {SCAN t1}
dan04489b62014-10-31 20:11:32 +000071}
72
dan89e71642014-11-01 18:08:04 +000073do_execsql_test 1.8 {
74 SELECT count(*) FROM t1, t2 WHERE t2.rowid>1;
75} 4
76
77do_scanstatus_test 1.9 {
drh518140e2014-11-06 03:55:10 +000078 nLoop 2 nVisit 4 nEst 2.0 zName t2 zExplain
drh82102332021-03-20 15:11:29 +000079 {SEARCH t2 USING INTEGER PRIMARY KEY (rowid>?)}
80 nLoop 4 nVisit 8 nEst 2.0 zName t1 zExplain {SCAN t1}
dan89e71642014-11-01 18:08:04 +000081}
82
83do_test 1.9 {
drh1df64702017-10-13 15:56:26 +000084 sqlite3_stmt_scanstatus_reset [db version -last-stmt-ptr]
dan89e71642014-11-01 18:08:04 +000085} {}
86
87do_scanstatus_test 1.10 {
drh518140e2014-11-06 03:55:10 +000088 nLoop 0 nVisit 0 nEst 2.0 zName t2 zExplain
drh82102332021-03-20 15:11:29 +000089 {SEARCH t2 USING INTEGER PRIMARY KEY (rowid>?)}
90 nLoop 0 nVisit 0 nEst 2.0 zName t1 zExplain {SCAN t1}
dan89e71642014-11-01 18:08:04 +000091}
92
93#-------------------------------------------------------------------------
94# Try a few different types of scans.
95#
96reset_db
97do_execsql_test 2.1 {
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);
104
105 SELECT * FROM x1 WHERE i=2;
106} {2 two}
107
108do_scanstatus_test 2.2 {
drh518140e2014-11-06 03:55:10 +0000109 nLoop 1 nVisit 1 nEst 1.0 zName x1
drh82102332021-03-20 15:11:29 +0000110 zExplain {SEARCH x1 USING INTEGER PRIMARY KEY (rowid=?)}
dan89e71642014-11-01 18:08:04 +0000111}
112
113do_execsql_test 2.3.1 {
114 SELECT * FROM x1 WHERE j='two'
115} {2 two}
116do_scanstatus_test 2.3.2 {
drh518140e2014-11-06 03:55:10 +0000117 nLoop 1 nVisit 1 nEst 10.0 zName x1j
drh82102332021-03-20 15:11:29 +0000118 zExplain {SEARCH x1 USING COVERING INDEX x1j (j=?)}
dan89e71642014-11-01 18:08:04 +0000119}
120
121do_execsql_test 2.4.1 {
122 SELECT * FROM x1 WHERE j<'two'
123} {4 four 1 one 3 three}
124do_scanstatus_test 2.4.2 {
drh518140e2014-11-06 03:55:10 +0000125 nLoop 1 nVisit 3 nEst 262144.0 zName x1j
drh82102332021-03-20 15:11:29 +0000126 zExplain {SEARCH x1 USING COVERING INDEX x1j (j<?)}
dan89e71642014-11-01 18:08:04 +0000127}
128
129do_execsql_test 2.5.1 {
130 SELECT * FROM x1 WHERE j>='two'
131} {2 two}
132do_scanstatus_test 2.5.2 {
drh518140e2014-11-06 03:55:10 +0000133 nLoop 1 nVisit 1 nEst 262144.0 zName x1j
drh82102332021-03-20 15:11:29 +0000134 zExplain {SEARCH x1 USING COVERING INDEX x1j (j>?)}
dan89e71642014-11-01 18:08:04 +0000135}
136
137do_execsql_test 2.6.1 {
138 SELECT * FROM x1 WHERE j BETWEEN 'three' AND 'two'
139} {3 three 2 two}
140do_scanstatus_test 2.6.2 {
drh518140e2014-11-06 03:55:10 +0000141 nLoop 1 nVisit 2 nEst 16384.0 zName x1j
drh82102332021-03-20 15:11:29 +0000142 zExplain {SEARCH x1 USING COVERING INDEX x1j (j>? AND j<?)}
dan89e71642014-11-01 18:08:04 +0000143}
144
145do_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}}
152
153do_scanstatus_test 2.7.2 {
drh518140e2014-11-06 03:55:10 +0000154 nLoop 1 nVisit 2 nEst 16384.0 zName x2j
drh82102332021-03-20 15:11:29 +0000155 zExplain {SEARCH x2 USING INDEX x2j (j>? AND j<?)}
dan89e71642014-11-01 18:08:04 +0000156}
157
158do_execsql_test 2.8.1 {
159 SELECT * FROM x2 WHERE i=1 AND j='two'
160}
161do_scanstatus_test 2.8.2 {
drh518140e2014-11-06 03:55:10 +0000162 nLoop 1 nVisit 0 nEst 8.0 zName x2ij
drh82102332021-03-20 15:11:29 +0000163 zExplain {SEARCH x2 USING INDEX x2ij (i=? AND j=?)}
dan89e71642014-11-01 18:08:04 +0000164}
165
166do_execsql_test 2.9.1 {
167 SELECT * FROM x2 WHERE i=5 AND j='two'
168}
169do_scanstatus_test 2.9.2 {
drh518140e2014-11-06 03:55:10 +0000170 nLoop 1 nVisit 0 nEst 8.0 zName x2ij
drh82102332021-03-20 15:11:29 +0000171 zExplain {SEARCH x2 USING INDEX x2ij (i=? AND j=?)}
dan89e71642014-11-01 18:08:04 +0000172}
173
174do_execsql_test 2.10.1 {
175 SELECT * FROM x2 WHERE i=3 AND j='three'
176} {3 three {3 three}}
177do_scanstatus_test 2.10.2 {
drh518140e2014-11-06 03:55:10 +0000178 nLoop 1 nVisit 1 nEst 8.0 zName x2ij
drh82102332021-03-20 15:11:29 +0000179 zExplain {SEARCH x2 USING INDEX x2ij (i=? AND j=?)}
dan89e71642014-11-01 18:08:04 +0000180}
181
182#-------------------------------------------------------------------------
183# Try with queries that use the OR optimization.
184#
185do_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);
189
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;
192}
193
194do_execsql_test 3.2.1 {
195 SELECT d FROM a1 WHERE (a=4 OR b=13)
196} {4 13}
dan6f9702e2014-11-01 20:38:06 +0000197do_scanstatus_test 3.2.2 {
drh518140e2014-11-06 03:55:10 +0000198 nLoop 1 nVisit 1 nEst 10.0 zName a1a
drh82102332021-03-20 15:11:29 +0000199 zExplain {SEARCH a1 USING INDEX a1a (a=?)}
drh518140e2014-11-06 03:55:10 +0000200 nLoop 1 nVisit 1 nEst 10.0 zName a1bc
drh82102332021-03-20 15:11:29 +0000201 zExplain {SEARCH a1 USING INDEX a1bc (b=?)}
dan89e71642014-11-01 18:08:04 +0000202}
dan04489b62014-10-31 20:11:32 +0000203
dan6f9702e2014-11-01 20:38:06 +0000204do_execsql_test 3.2.1 {
205 SELECT count(*) FROM a1 WHERE (a BETWEEN 4 AND 12) OR (b BETWEEN 40 AND 60)
206} {30}
207do_scanstatus_test 3.2.2 {
drh518140e2014-11-06 03:55:10 +0000208 nLoop 1 nVisit 9 nEst 16384.0 zName a1a
drh82102332021-03-20 15:11:29 +0000209 zExplain {SEARCH a1 USING INDEX a1a (a>? AND a<?)}
drh518140e2014-11-06 03:55:10 +0000210 nLoop 1 nVisit 21 nEst 16384.0 zName a1bc
drh82102332021-03-20 15:11:29 +0000211 zExplain {SEARCH a1 USING INDEX a1bc (b>? AND b<?)}
dan6f9702e2014-11-01 20:38:06 +0000212}
dan04489b62014-10-31 20:11:32 +0000213
dan6f9702e2014-11-01 20:38:06 +0000214do_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)
217} {90}
218do_scanstatus_test 3.2.2 {
drh518140e2014-11-06 03:55:10 +0000219 nLoop 1 nVisit 10 nEst 16384.0 zName a1bc
drh46bee2a2021-03-23 15:39:02 +0000220 zExplain {SEARCH y USING COVERING INDEX a1bc (b>? AND b<?)}
drh518140e2014-11-06 03:55:10 +0000221 nLoop 10 nVisit 90 nEst 16384.0 zName a1a
drh46bee2a2021-03-23 15:39:02 +0000222 zExplain {SEARCH x USING COVERING INDEX a1a (a>? AND a<?)}
dan6f9702e2014-11-01 20:38:06 +0000223}
224
225do_execsql_test 3.4.1 {
226 SELECT count(*) FROM a1 WHERE a IN (1, 5, 10, 15);
227} {4}
228do_scanstatus_test 3.4.2 {
drh518140e2014-11-06 03:55:10 +0000229 nLoop 1 nVisit 4 nEst 40.0 zName a1a
drh82102332021-03-20 15:11:29 +0000230 zExplain {SEARCH a1 USING COVERING INDEX a1a (a=?)}
dan6f9702e2014-11-01 20:38:06 +0000231}
232
233do_execsql_test 3.4.1 {
234 SELECT count(*) FROM a1 WHERE rowid IN (1, 5, 10, 15);
235} {4}
236do_scanstatus_test 3.4.2 {
drh518140e2014-11-06 03:55:10 +0000237 nLoop 1 nVisit 4 nEst 4.0 zName a1
drh82102332021-03-20 15:11:29 +0000238 zExplain {SEARCH a1 USING INTEGER PRIMARY KEY (rowid=?)}
dan6f9702e2014-11-01 20:38:06 +0000239}
dan04489b62014-10-31 20:11:32 +0000240
dan037b5322014-11-03 11:25:32 +0000241#-------------------------------------------------------------------------
242# Test that scanstatus() data is not available for searches performed
243# by triggers.
244#
245# It is available for searches performed as part of FK processing, but
246# not FK action processing.
247#
248do_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;
253 END;
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;
256}
257
258do_execsql_test 4.1.1 { INSERT INTO t1 VALUES(1, 2, 3); }
dan69e224f2019-02-26 19:16:49 +0000259do_scanstatus_test 4.1.2 {}
dan037b5322014-11-03 11:25:32 +0000260
261do_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;
267}
268do_execsql_test 4.2.1 { DELETE FROM p1 WHERE x=4 }
269do_scanstatus_test 4.2.2 {
drh518140e2014-11-06 03:55:10 +0000270 nLoop 1 nVisit 1 nEst 1.0 zName sqlite_autoindex_p1_1
drh82102332021-03-20 15:11:29 +0000271 zExplain {SEARCH p1 USING INDEX sqlite_autoindex_p1_1 (x=?)}
dan037b5322014-11-03 11:25:32 +0000272
drh82102332021-03-20 15:11:29 +0000273 nLoop 1 nVisit 3 nEst 262144.0 zName c1 zExplain {SCAN c1}
dan037b5322014-11-03 11:25:32 +0000274}
275
dane2f771b2014-11-03 15:33:17 +0000276#-------------------------------------------------------------------------
277# Further tests of different scan types.
278#
279reset_db
280proc tochar {i} {
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]]]
283}
284db func tochar tochar
285do_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);
298
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)
303 UNION ALL
304 SELECT i+1, (i+1)%2, tochar(i+1) FROM data WHERE i<500
305 ) INSERT INTO t2 SELECT x, y FROM data;
306
307 CREATE TABLE t3(x, y);
308 INSERT INTO t3 SELECT * FROM t2;
309
310 ANALYZE;
311}
312
313do_execsql_test 5.1.1 {
314 SELECT count(*) FROM t1 WHERE a IN (SELECT b FROM t1 AS ii)
315} {2}
316do_scanstatus_test 5.1.2 {
dan231ff4b2022-12-02 20:32:22 +0000317 nLoop 1 nVisit 10 nEst 10.0 zName t1
318 zExplain {SCAN ii}
drh518140e2014-11-06 03:55:10 +0000319 nLoop 1 nVisit 2 nEst 8.0 zName sqlite_autoindex_t1_1
drh82102332021-03-20 15:11:29 +0000320 zExplain {SEARCH t1 USING COVERING INDEX sqlite_autoindex_t1_1 (a=?)}
dane2f771b2014-11-03 15:33:17 +0000321}
322
323do_execsql_test 5.2.1 {
324 SELECT count(*) FROM t1 WHERE a IN (0, 1)
325} {2}
326do_scanstatus_test 5.2.2 {
drh518140e2014-11-06 03:55:10 +0000327 nLoop 1 nVisit 2 nEst 2.0 zName sqlite_autoindex_t1_1
drh82102332021-03-20 15:11:29 +0000328 zExplain {SEARCH t1 USING COVERING INDEX sqlite_autoindex_t1_1 (a=?)}
dane2f771b2014-11-03 15:33:17 +0000329}
330
331do_eqp_test 5.3.1 {
332 SELECT count(*) FROM t2 WHERE y = 'j';
drh82102332021-03-20 15:11:29 +0000333} {SEARCH t2 USING COVERING INDEX t2xy (ANY(x) AND y=?)}
dane2f771b2014-11-03 15:33:17 +0000334do_execsql_test 5.3.2 {
335 SELECT count(*) FROM t2 WHERE y = 'j';
336} {19}
337do_scanstatus_test 5.3.3 {
drh99bbcc82016-07-29 01:32:36 +0000338 nLoop 1 nVisit 19 nEst 56.0 zName t2xy zExplain
drh82102332021-03-20 15:11:29 +0000339 {SEARCH t2 USING COVERING INDEX t2xy (ANY(x) AND y=?)}
dane2f771b2014-11-03 15:33:17 +0000340}
341
342do_eqp_test 5.4.1 {
343 SELECT count(*) FROM t1, t2 WHERE y = c;
344} {
drhb3f02762018-05-02 18:00:17 +0000345 QUERY PLAN
dan231ff4b2022-12-02 20:32:22 +0000346 |--SCAN t1
drh82102332021-03-20 15:11:29 +0000347 `--SEARCH t2 USING COVERING INDEX t2xy (ANY(x) AND y=?)
dane2f771b2014-11-03 15:33:17 +0000348}
349do_execsql_test 5.4.2 {
350 SELECT count(*) FROM t1, t2 WHERE y = c;
351} {200}
352do_scanstatus_test 5.4.3 {
dan231ff4b2022-12-02 20:32:22 +0000353 nLoop 1 nVisit 10 nEst 10.0 zName t1
354 zExplain {SCAN t1}
drh99bbcc82016-07-29 01:32:36 +0000355 nLoop 10 nVisit 200 nEst 56.0 zName t2xy
drh82102332021-03-20 15:11:29 +0000356 zExplain {SEARCH t2 USING COVERING INDEX t2xy (ANY(x) AND y=?)}
dane2f771b2014-11-03 15:33:17 +0000357}
358
359do_eqp_test 5.5.1 {
360 SELECT count(*) FROM t1, t3 WHERE y = c;
361} {
drhb3f02762018-05-02 18:00:17 +0000362 QUERY PLAN
drh82102332021-03-20 15:11:29 +0000363 |--SCAN t3
364 `--SEARCH t1 USING AUTOMATIC COVERING INDEX (c=?)
dane2f771b2014-11-03 15:33:17 +0000365}
366do_execsql_test 5.5.2 {
367 SELECT count(*) FROM t1, t3 WHERE y = c;
368} {200}
369do_scanstatus_test 5.5.3 {
drh82102332021-03-20 15:11:29 +0000370 nLoop 1 nVisit 501 nEst 480.0 zName t3 zExplain {SCAN t3}
drh518140e2014-11-06 03:55:10 +0000371 nLoop 501 nVisit 200 nEst 20.0 zName auto-index zExplain
drh82102332021-03-20 15:11:29 +0000372 {SEARCH t1 USING AUTOMATIC COVERING INDEX (c=?)}
dane2f771b2014-11-03 15:33:17 +0000373}
374
375#-------------------------------------------------------------------------
376# Virtual table scans
377#
378ifcapable fts3 {
379 do_execsql_test 6.0 {
380 CREATE VIRTUAL TABLE ft1 USING fts4;
381 INSERT INTO ft1 VALUES('a d c f g h e i f c');
382 INSERT INTO ft1 VALUES('g c h b g b f f f g');
383 INSERT INTO ft1 VALUES('h h c c h f a e d d');
384 INSERT INTO ft1 VALUES('e j i j i e b c f g');
385 INSERT INTO ft1 VALUES('g f b g j c h a d f');
386 INSERT INTO ft1 VALUES('j i a e g f a i a c');
387 INSERT INTO ft1 VALUES('f d g g j j c a h g');
388 INSERT INTO ft1 VALUES('b d h a d j j j b i');
389 INSERT INTO ft1 VALUES('j e a b j e c b c i');
390 INSERT INTO ft1 VALUES('a d e f b j j c g d');
391 }
392 do_execsql_test 6.1.1 {
393 SELECT count(*) FROM ft1 WHERE ft1 MATCH 'd'
394 } {6}
395 do_scanstatus_test 6.1.2 {
drh518140e2014-11-06 03:55:10 +0000396 nLoop 1 nVisit 6 nEst 24.0 zName ft1 zExplain
drh82102332021-03-20 15:11:29 +0000397 {SCAN ft1 VIRTUAL TABLE INDEX 3:}
dane2f771b2014-11-03 15:33:17 +0000398 }
399}
400
401
dan04489b62014-10-31 20:11:32 +0000402finish_test