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 scanstatus2
17 ifcapable !scanstatus {
22 sqlite3_db_config db STMT_SCANSTATUS 1
25 CREATE TABLE t1(a, b);
26 CREATE TABLE t2(x, y);
27 INSERT INTO t1 VALUES(1, 2);
28 INSERT INTO t1 VALUES(3, 4);
29 INSERT INTO t2 VALUES('a', 'b');
30 INSERT INTO t2 VALUES('c', 'd');
31 INSERT INTO t2 VALUES('e', 'f');
34 proc do_zexplain_test {v2 tn sql res} {
36 set stmt [db version -last-stmt-ptr]
40 set cmd sqlite3_stmt_scanstatus
42 if {$v2} { lappend f complex }
45 set r [sqlite3_stmt_scanstatus -flags $f $stmt $idx]
46 if {[llength $r]==0} break
47 lappend ret [dict get $r zExplain]
50 uplevel [list do_test $tn [list set {} $ret] [list {*}$res]]
53 proc get_cycles {stmt} {
54 set r [sqlite3_stmt_scanstatus $stmt -1]
58 proc foreach_scan {varname stmt body} {
61 for {set ii 0} {1} {incr ii} {
62 set r [sqlite3_stmt_scanstatus -flags complex $stmt $ii]
63 if {[llength $r]==0} break
69 proc get_eqp_graph {stmt iPar nIndent} {
71 foreach_scan A $stmt {
72 if {$A(iParentId)==$iPar} {
75 append txt " (nCycle=$A(nCycle))"
77 append res "[string repeat - $nIndent]$txt\n"
78 append res [get_eqp_graph $stmt $A(iSelectId) [expr $nIndent+2]]
84 proc get_graph {stmt} {
85 set nCycle [get_cycles $stmt]
86 set res "QUERY (nCycle=$nCycle)\n"
87 append res [get_eqp_graph $stmt 0 2]
90 proc do_graph_test {tn sql res} {
92 set stmt [db version -last-stmt-ptr]
93 set graph [string trim [get_graph $stmt]]
95 set graph [regsub -all {nCycle=[0-9]+} $graph nCycle=nnn]
96 uplevel [list do_test $tn [list set {} $graph] [string trim $res]]
99 proc puts_graph {sql} {
101 set stmt [db version -last-stmt-ptr]
102 puts [string trim [get_graph $stmt]]
106 do_zexplain_test 0 1.1 {
107 SELECT (SELECT a FROM t1 WHERE b=x) FROM t2 WHERE y=2
112 do_zexplain_test 1 1.2 {
113 SELECT (SELECT a FROM t1 WHERE b=x) FROM t2 WHERE y=2
116 {CORRELATED SCALAR SUBQUERY 1}
121 SELECT (SELECT a FROM t1 WHERE b=x) FROM t2 WHERE y=2
124 --SCAN t2 (nCycle=nnn)
125 --CORRELATED SCALAR SUBQUERY 1 (nCycle=nnn)
126 ----SCAN t1 (nCycle=nnn)
130 WITH v2(x,y) AS MATERIALIZED (
133 SELECT * FROM t1, v2 ORDER BY y;
136 --MATERIALIZE v2 (nCycle=nnn)
137 ----SCAN t2 (nCycle=nnn)
138 --SCAN v2 (nCycle=nnn)
139 --SCAN t1 (nCycle=nnn)
140 --USE TEMP B-TREE FOR ORDER BY (nCycle=nnn)
143 #-------------------------------------------------------------------------
146 sqlite3_db_config db STMT_SCANSTATUS 1
147 do_execsql_test 2.0 {
148 CREATE VIRTUAL TABLE ft USING fts5(a);
149 INSERT INTO ft VALUES('abc');
150 INSERT INTO ft VALUES('def');
151 INSERT INTO ft VALUES('ghi');
155 SELECT * FROM ft('def')
158 --SCAN ft VIRTUAL TABLE INDEX 0:M1 (nCycle=nnn)
162 #-------------------------------------------------------------------------
164 sqlite3_db_config db STMT_SCANSTATUS 1
165 do_execsql_test 3.0 {
166 CREATE TABLE x1(a, b);
167 CREATE TABLE x2(c, d);
169 WITH s(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<1000)
170 INSERT INTO x1 SELECT i, i FROM s;
171 INSERT INTO x2 SELECT a, b FROM x1;
175 SELECT * FROM x1, x2 WHERE c=+a;
178 --SCAN x1 (nCycle=nnn)
179 --CREATE AUTOMATIC INDEX ON x2(c, d) (nCycle=nnn)
180 --BLOOM FILTER ON x2 (c=?)
181 --SEARCH x2 USING AUTOMATIC COVERING INDEX (c=?) (nCycle=nnn)
184 #-------------------------------------------------------------------------
186 sqlite3_db_config db STMT_SCANSTATUS 1
187 do_execsql_test 4.0 {
188 CREATE TABLE rt1 (id INTEGER PRIMARY KEY, x1, x2);
189 CREATE TABLE rt2 (id, x1, x2);
193 SELECT * FROM rt1, rt2 WHERE rt1.id%2 AND rt2.x1=rt1.x1;
196 --SCAN rt1 (nCycle=nnn)
197 --CREATE AUTOMATIC INDEX ON rt2(x1, id, x2) (nCycle=nnn)
198 --BLOOM FILTER ON rt2 (x1=?)
199 --SEARCH rt2 USING AUTOMATIC COVERING INDEX (x1=?) (nCycle=nnn)
203 SELECT rt2.id FROM rt1, rt2 WHERE rt1.id%2 AND rt2.x1=rt1.x1;
206 --SCAN rt1 (nCycle=nnn)
207 --CREATE AUTOMATIC INDEX ON rt2(x1, id) (nCycle=nnn)
208 --BLOOM FILTER ON rt2 (x1=?)
209 --SEARCH rt2 USING AUTOMATIC COVERING INDEX (x1=?) (nCycle=nnn)
213 SELECT rt2.id FROM rt1, rt2 WHERE rt1.id%2 AND (rt2.x1+1)=(rt1.x1+1);
216 --SCAN rt1 (nCycle=nnn)
217 --SCAN rt2 (nCycle=nnn)
221 SELECT rt2.id FROM rt1, rt2 WHERE rt1.id%2 AND rt2.x1=(rt1.x1+1) AND rt2.id>5;
224 --SCAN rt1 (nCycle=nnn)
225 --CREATE AUTOMATIC INDEX ON rt2(x1, id) WHERE <expr> (nCycle=nnn)
226 --BLOOM FILTER ON rt2 (x1=?)
227 --SEARCH rt2 USING AUTOMATIC PARTIAL COVERING INDEX (x1=?) (nCycle=nnn)
231 SELECT v1.cnt FROM rt1, (
232 SELECT count(*) AS cnt, rt2.x1 AS x1 FROM rt2 GROUP BY x1
233 ) AS v1 WHERE rt1.x1=v1.x1
237 ----SCAN rt2 (nCycle=nnn)
238 ----USE TEMP B-TREE FOR GROUP BY
239 --SCAN rt1 (nCycle=nnn)
240 --CREATE AUTOMATIC INDEX ON v1(x1, cnt) (nCycle=nnn)
241 --BLOOM FILTER ON v1 (x1=?)
242 --SEARCH v1 USING AUTOMATIC COVERING INDEX (x1=?) (nCycle=nnn)
245 #-------------------------------------------------------------------------
249 do_execsql_test 5.0 {
250 CREATE TABLE t1(x, y);
251 CREATE TRIGGER tr1 AFTER DELETE ON t1 BEGIN
254 INSERT INTO t1 VALUES(1, 2);
257 proc trace {stmt sql} {
258 array set A [sqlite3_stmt_scanstatus -flags complex [format %x $stmt] 0]
259 lappend ::trace_explain $A(zExplain)
263 set ::trace_explain [list]
264 do_execsql_test 5.1 {
265 DELETE FROM t1 WHERE x=1;
270 } {{SCAN t1} {SCAN t1} {SCAN t1}}