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
23 #-------------------------------------------------------------------------
25 # eqp-1.*: Assorted tests.
26 # eqp-2.*: Tests for single select statements.
27 # eqp-3.*: Select statements that execute sub-selects.
28 # eqp-4.*: Compound select statements.
30 # eqp-7.*: "SELECT count(*) FROM tbl" statements (VDBE code OP_Count).
33 proc det {args} { uplevel do_eqp_test $args }
36 CREATE TABLE t1(a INT, b INT, ex TEXT);
37 CREATE INDEX i1 ON t1(a);
38 CREATE INDEX i2 ON t1(b);
39 CREATE TABLE t2(a INT, b INT, ex TEXT);
40 CREATE TABLE t3(a INT, b INT, ex TEXT);
44 SELECT * FROM t2, t1 WHERE t1.a=1 OR t1.b=2;
46 0 0 1 {SEARCH TABLE t1 USING INDEX i1 (a=?)}
47 0 0 1 {SEARCH TABLE t1 USING INDEX i2 (b=?)}
51 SELECT * FROM t2 CROSS JOIN t1 WHERE t1.a=1 OR t1.b=2;
54 0 1 1 {SEARCH TABLE t1 USING INDEX i1 (a=?)}
55 0 1 1 {SEARCH TABLE t1 USING INDEX i2 (b=?)}
58 SELECT a FROM t1 ORDER BY a
60 0 0 0 {SCAN TABLE t1 USING COVERING INDEX i1}
63 SELECT a FROM t1 ORDER BY +a
65 0 0 0 {SCAN TABLE t1 USING COVERING INDEX i1}
66 0 0 0 {USE TEMP B-TREE FOR ORDER BY}
69 SELECT a FROM t1 WHERE a=4
71 0 0 0 {SEARCH TABLE t1 USING COVERING INDEX i1 (a=?)}
74 SELECT DISTINCT count(*) FROM t3 GROUP BY a;
77 0 0 0 {USE TEMP B-TREE FOR GROUP BY}
78 0 0 0 {USE TEMP B-TREE FOR DISTINCT}
82 SELECT * FROM t3 JOIN (SELECT 1)
84 0 0 1 {SCAN SUBQUERY 1}
88 SELECT * FROM t3 JOIN (SELECT 1 UNION SELECT 2)
90 1 0 0 {COMPOUND SUBQUERIES 2 AND 3 USING TEMP B-TREE (UNION)}
91 0 0 1 {SCAN SUBQUERY 1}
95 SELECT * FROM t3 JOIN (SELECT 1 EXCEPT SELECT a FROM t3 LIMIT 17)
98 1 0 0 {COMPOUND SUBQUERIES 2 AND 3 USING TEMP B-TREE (EXCEPT)}
99 0 0 1 {SCAN SUBQUERY 1}
100 0 1 0 {SCAN TABLE t3}
103 SELECT * FROM t3 JOIN (SELECT 1 INTERSECT SELECT a FROM t3 LIMIT 17)
105 3 0 0 {SCAN TABLE t3}
106 1 0 0 {COMPOUND SUBQUERIES 2 AND 3 USING TEMP B-TREE (INTERSECT)}
107 0 0 1 {SCAN SUBQUERY 1}
108 0 1 0 {SCAN TABLE t3}
112 SELECT * FROM t3 JOIN (SELECT 1 UNION ALL SELECT a FROM t3 LIMIT 17)
114 3 0 0 {SCAN TABLE t3}
115 1 0 0 {COMPOUND SUBQUERIES 2 AND 3 (UNION ALL)}
116 0 0 1 {SCAN SUBQUERY 1}
117 0 1 0 {SCAN TABLE t3}
120 #-------------------------------------------------------------------------
121 # Test cases eqp-2.* - tests for single select statements.
124 do_execsql_test 2.1 {
125 CREATE TABLE t1(x INT, y INT, ex TEXT);
127 CREATE TABLE t2(x INT, y INT, ex TEXT);
128 CREATE INDEX t2i1 ON t2(x);
131 det 2.2.1 "SELECT DISTINCT min(x), max(x) FROM t1 GROUP BY x ORDER BY 1" {
132 0 0 0 {SCAN TABLE t1}
133 0 0 0 {USE TEMP B-TREE FOR GROUP BY}
134 0 0 0 {USE TEMP B-TREE FOR DISTINCT}
135 0 0 0 {USE TEMP B-TREE FOR ORDER BY}
137 det 2.2.2 "SELECT DISTINCT min(x), max(x) FROM t2 GROUP BY x ORDER BY 1" {
138 0 0 0 {SCAN TABLE t2 USING COVERING INDEX t2i1}
139 0 0 0 {USE TEMP B-TREE FOR DISTINCT}
140 0 0 0 {USE TEMP B-TREE FOR ORDER BY}
142 det 2.2.3 "SELECT DISTINCT * FROM t1" {
143 0 0 0 {SCAN TABLE t1}
144 0 0 0 {USE TEMP B-TREE FOR DISTINCT}
146 det 2.2.4 "SELECT DISTINCT * FROM t1, t2" {
147 0 0 0 {SCAN TABLE t1}
148 0 1 1 {SCAN TABLE t2}
149 0 0 0 {USE TEMP B-TREE FOR DISTINCT}
151 det 2.2.5 "SELECT DISTINCT * FROM t1, t2 ORDER BY t1.x" {
152 0 0 0 {SCAN TABLE t1}
153 0 1 1 {SCAN TABLE t2}
154 0 0 0 {USE TEMP B-TREE FOR DISTINCT}
155 0 0 0 {USE TEMP B-TREE FOR ORDER BY}
157 det 2.2.6 "SELECT DISTINCT t2.x FROM t1, t2 ORDER BY t2.x" {
158 0 0 1 {SCAN TABLE t2 USING COVERING INDEX t2i1}
159 0 1 0 {SCAN TABLE t1}
162 det 2.3.1 "SELECT max(x) FROM t2" {
163 0 0 0 {SEARCH TABLE t2 USING COVERING INDEX t2i1}
165 det 2.3.2 "SELECT min(x) FROM t2" {
166 0 0 0 {SEARCH TABLE t2 USING COVERING INDEX t2i1}
168 det 2.3.3 "SELECT min(x), max(x) FROM t2" {
169 0 0 0 {SCAN TABLE t2 USING COVERING INDEX t2i1}
172 det 2.4.1 "SELECT * FROM t1 WHERE rowid=?" {
173 0 0 0 {SEARCH TABLE t1 USING INTEGER PRIMARY KEY (rowid=?)}
178 #-------------------------------------------------------------------------
179 # Test cases eqp-3.* - tests for select statements that use sub-selects.
182 SELECT (SELECT x FROM t1 AS sub) FROM t1;
184 0 0 0 {SCAN TABLE t1}
185 0 0 0 {EXECUTE SCALAR SUBQUERY 1}
186 1 0 0 {SCAN TABLE t1 AS sub}
189 SELECT * FROM t1 WHERE (SELECT x FROM t1 AS sub);
191 0 0 0 {SCAN TABLE t1}
192 0 0 0 {EXECUTE SCALAR SUBQUERY 1}
193 1 0 0 {SCAN TABLE t1 AS sub}
196 SELECT * FROM t1 WHERE (SELECT x FROM t1 AS sub ORDER BY y);
198 0 0 0 {SCAN TABLE t1}
199 0 0 0 {EXECUTE SCALAR SUBQUERY 1}
200 1 0 0 {SCAN TABLE t1 AS sub}
201 1 0 0 {USE TEMP B-TREE FOR ORDER BY}
204 SELECT * FROM t1 WHERE (SELECT x FROM t2 ORDER BY x);
206 0 0 0 {SCAN TABLE t1}
207 0 0 0 {EXECUTE SCALAR SUBQUERY 1}
208 1 0 0 {SCAN TABLE t2 USING COVERING INDEX t2i1}
212 SELECT * FROM (SELECT * FROM t1 ORDER BY x LIMIT 10) ORDER BY y LIMIT 5
214 1 0 0 {SCAN TABLE t1}
215 1 0 0 {USE TEMP B-TREE FOR ORDER BY}
216 0 0 0 {SCAN SUBQUERY 1}
217 0 0 0 {USE TEMP B-TREE FOR ORDER BY}
221 (SELECT * FROM t1 ORDER BY x LIMIT 10) AS x1,
222 (SELECT * FROM t2 ORDER BY x LIMIT 10) AS x2
223 ORDER BY x2.y LIMIT 5
225 1 0 0 {SCAN TABLE t1}
226 1 0 0 {USE TEMP B-TREE FOR ORDER BY}
227 2 0 0 {SCAN TABLE t2 USING INDEX t2i1}
228 0 0 0 {SCAN SUBQUERY 1 AS x1}
229 0 1 1 {SCAN SUBQUERY 2 AS x2}
230 0 0 0 {USE TEMP B-TREE FOR ORDER BY}
234 SELECT * FROM t1 WHERE y IN (SELECT y FROM t2)
236 0 0 0 {SCAN TABLE t1}
237 0 0 0 {EXECUTE LIST SUBQUERY 1}
238 1 0 0 {SCAN TABLE t2}
241 SELECT * FROM t1 WHERE y IN (SELECT y FROM t2 WHERE t1.x!=t2.x)
243 0 0 0 {SCAN TABLE t1}
244 0 0 0 {EXECUTE CORRELATED LIST SUBQUERY 1}
245 1 0 0 {SCAN TABLE t2}
248 SELECT * FROM t1 WHERE EXISTS (SELECT y FROM t2 WHERE t1.x!=t2.x)
250 0 0 0 {SCAN TABLE t1}
251 0 0 0 {EXECUTE CORRELATED SCALAR SUBQUERY 1}
252 1 0 0 {SCAN TABLE t2}
255 #-------------------------------------------------------------------------
256 # Test cases eqp-4.* - tests for composite select statements.
259 SELECT * FROM t1 UNION ALL SELECT * FROM t2
261 1 0 0 {SCAN TABLE t1}
262 2 0 0 {SCAN TABLE t2}
263 0 0 0 {COMPOUND SUBQUERIES 1 AND 2 (UNION ALL)}
266 SELECT * FROM t1 UNION ALL SELECT * FROM t2 ORDER BY 2
268 1 0 0 {SCAN TABLE t1}
269 1 0 0 {USE TEMP B-TREE FOR ORDER BY}
270 2 0 0 {SCAN TABLE t2}
271 2 0 0 {USE TEMP B-TREE FOR ORDER BY}
272 0 0 0 {COMPOUND SUBQUERIES 1 AND 2 (UNION ALL)}
275 SELECT * FROM t1 UNION SELECT * FROM t2 ORDER BY 2
277 1 0 0 {SCAN TABLE t1}
278 1 0 0 {USE TEMP B-TREE FOR ORDER BY}
279 2 0 0 {SCAN TABLE t2}
280 2 0 0 {USE TEMP B-TREE FOR ORDER BY}
281 0 0 0 {COMPOUND SUBQUERIES 1 AND 2 (UNION)}
284 SELECT * FROM t1 INTERSECT SELECT * FROM t2 ORDER BY 2
286 1 0 0 {SCAN TABLE t1}
287 1 0 0 {USE TEMP B-TREE FOR ORDER BY}
288 2 0 0 {SCAN TABLE t2}
289 2 0 0 {USE TEMP B-TREE FOR ORDER BY}
290 0 0 0 {COMPOUND SUBQUERIES 1 AND 2 (INTERSECT)}
293 SELECT * FROM t1 EXCEPT SELECT * FROM t2 ORDER BY 2
295 1 0 0 {SCAN TABLE t1}
296 1 0 0 {USE TEMP B-TREE FOR ORDER BY}
297 2 0 0 {SCAN TABLE t2}
298 2 0 0 {USE TEMP B-TREE FOR ORDER BY}
299 0 0 0 {COMPOUND SUBQUERIES 1 AND 2 (EXCEPT)}
303 SELECT * FROM t1 UNION ALL SELECT * FROM t2 ORDER BY 1
305 1 0 0 {SCAN TABLE t1}
306 1 0 0 {USE TEMP B-TREE FOR ORDER BY}
307 2 0 0 {SCAN TABLE t2 USING INDEX t2i1}
308 0 0 0 {COMPOUND SUBQUERIES 1 AND 2 (UNION ALL)}
311 SELECT * FROM t1 UNION SELECT * FROM t2 ORDER BY 1
313 1 0 0 {SCAN TABLE t1}
314 1 0 0 {USE TEMP B-TREE FOR ORDER BY}
315 2 0 0 {SCAN TABLE t2 USING INDEX t2i1}
316 2 0 0 {USE TEMP B-TREE FOR RIGHT PART OF ORDER BY}
317 0 0 0 {COMPOUND SUBQUERIES 1 AND 2 (UNION)}
320 SELECT * FROM t1 INTERSECT SELECT * FROM t2 ORDER BY 1
322 1 0 0 {SCAN TABLE t1}
323 1 0 0 {USE TEMP B-TREE FOR ORDER BY}
324 2 0 0 {SCAN TABLE t2 USING INDEX t2i1}
325 2 0 0 {USE TEMP B-TREE FOR RIGHT PART OF ORDER BY}
326 0 0 0 {COMPOUND SUBQUERIES 1 AND 2 (INTERSECT)}
329 SELECT * FROM t1 EXCEPT SELECT * FROM t2 ORDER BY 1
331 1 0 0 {SCAN TABLE t1}
332 1 0 0 {USE TEMP B-TREE FOR ORDER BY}
333 2 0 0 {SCAN TABLE t2 USING INDEX t2i1}
334 2 0 0 {USE TEMP B-TREE FOR RIGHT PART OF ORDER BY}
335 0 0 0 {COMPOUND SUBQUERIES 1 AND 2 (EXCEPT)}
339 SELECT x FROM t1 UNION SELECT x FROM t2
341 1 0 0 {SCAN TABLE t1}
342 2 0 0 {SCAN TABLE t2 USING COVERING INDEX t2i1}
343 0 0 0 {COMPOUND SUBQUERIES 1 AND 2 USING TEMP B-TREE (UNION)}
347 SELECT x FROM t1 UNION SELECT x FROM t2 UNION SELECT x FROM t1
349 2 0 0 {SCAN TABLE t1}
350 3 0 0 {SCAN TABLE t2 USING COVERING INDEX t2i1}
351 1 0 0 {COMPOUND SUBQUERIES 2 AND 3 USING TEMP B-TREE (UNION)}
352 4 0 0 {SCAN TABLE t1}
353 0 0 0 {COMPOUND SUBQUERIES 1 AND 4 USING TEMP B-TREE (UNION)}
356 SELECT x FROM t1 UNION SELECT x FROM t2 UNION SELECT x FROM t1 ORDER BY 1
358 2 0 0 {SCAN TABLE t1}
359 2 0 0 {USE TEMP B-TREE FOR ORDER BY}
360 3 0 0 {SCAN TABLE t2 USING COVERING INDEX t2i1}
361 1 0 0 {COMPOUND SUBQUERIES 2 AND 3 (UNION)}
362 4 0 0 {SCAN TABLE t1}
363 4 0 0 {USE TEMP B-TREE FOR ORDER BY}
364 0 0 0 {COMPOUND SUBQUERIES 1 AND 4 (UNION)}
367 #-------------------------------------------------------------------------
368 # This next block of tests verifies that the examples on the
369 # lang_explain.html page are correct.
373 # EVIDENCE-OF: R-47779-47605 sqlite> EXPLAIN QUERY PLAN SELECT a, b
375 # 0|0|0|SCAN TABLE t1
377 do_execsql_test 5.1.0 { CREATE TABLE t1(a INT, b INT, ex TEXT) }
378 det 5.1.1 "SELECT a, b FROM t1 WHERE a=1" {
379 0 0 0 {SCAN TABLE t1}
382 # EVIDENCE-OF: R-55852-17599 sqlite> CREATE INDEX i1 ON t1(a);
383 # sqlite> EXPLAIN QUERY PLAN SELECT a, b FROM t1 WHERE a=1;
384 # 0|0|0|SEARCH TABLE t1 USING INDEX i1
386 do_execsql_test 5.2.0 { CREATE INDEX i1 ON t1(a) }
387 det 5.2.1 "SELECT a, b FROM t1 WHERE a=1" {
388 0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a=?)}
391 # EVIDENCE-OF: R-21179-11011 sqlite> CREATE INDEX i2 ON t1(a, b);
392 # sqlite> EXPLAIN QUERY PLAN SELECT a, b FROM t1 WHERE a=1;
393 # 0|0|0|SEARCH TABLE t1 USING COVERING INDEX i2 (a=?)
395 do_execsql_test 5.3.0 { CREATE INDEX i2 ON t1(a, b) }
396 det 5.3.1 "SELECT a, b FROM t1 WHERE a=1" {
397 0 0 0 {SEARCH TABLE t1 USING COVERING INDEX i2 (a=?)}
400 # EVIDENCE-OF: R-09991-48941 sqlite> EXPLAIN QUERY PLAN
401 # SELECT t1.*, t2.* FROM t1, t2 WHERE t1.a=1 AND t1.b>2;
402 # 0|0|0|SEARCH TABLE t1 USING COVERING INDEX i2 (a=? AND b>?)
403 # 0|1|1|SCAN TABLE t2
405 do_execsql_test 5.4.0 {CREATE TABLE t2(c INT, d INT, ex TEXT)}
406 det 5.4.1 "SELECT t1.a, t2.c FROM t1, t2 WHERE t1.a=1 AND t1.b>2" {
407 0 0 0 {SEARCH TABLE t1 USING COVERING INDEX i2 (a=? AND b>?)}
408 0 1 1 {SCAN TABLE t2}
411 # EVIDENCE-OF: R-33626-61085 sqlite> EXPLAIN QUERY PLAN
412 # SELECT t1.*, t2.* FROM t2, t1 WHERE t1.a=1 AND t1.b>2;
413 # 0|0|1|SEARCH TABLE t1 USING COVERING INDEX i2 (a=? AND b>?)
414 # 0|1|0|SCAN TABLE t2
416 det 5.5 "SELECT t1.a, t2.c FROM t2, t1 WHERE t1.a=1 AND t1.b>2" {
417 0 0 1 {SEARCH TABLE t1 USING COVERING INDEX i2 (a=? AND b>?)}
418 0 1 0 {SCAN TABLE t2}
421 # EVIDENCE-OF: R-04002-25654 sqlite> CREATE INDEX i3 ON t1(b);
422 # sqlite> EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=1 OR b=2;
423 # 0|0|0|SEARCH TABLE t1 USING COVERING INDEX i2 (a=?)
424 # 0|0|0|SEARCH TABLE t1 USING INDEX i3 (b=?)
426 do_execsql_test 5.5.0 {CREATE INDEX i3 ON t1(b)}
427 det 5.6.1 "SELECT a, b FROM t1 WHERE a=1 OR b=2" {
428 0 0 0 {SEARCH TABLE t1 USING COVERING INDEX i2 (a=?)}
429 0 0 0 {SEARCH TABLE t1 USING INDEX i3 (b=?)}
432 # EVIDENCE-OF: R-24577-38891 sqlite> EXPLAIN QUERY PLAN
433 # SELECT c, d FROM t2 ORDER BY c;
434 # 0|0|0|SCAN TABLE t2
435 # 0|0|0|USE TEMP B-TREE FOR ORDER BY
437 det 5.7 "SELECT c, d FROM t2 ORDER BY c" {
438 0 0 0 {SCAN TABLE t2}
439 0 0 0 {USE TEMP B-TREE FOR ORDER BY}
442 # EVIDENCE-OF: R-58157-12355 sqlite> CREATE INDEX i4 ON t2(c);
443 # sqlite> EXPLAIN QUERY PLAN SELECT c, d FROM t2 ORDER BY c;
444 # 0|0|0|SCAN TABLE t2 USING INDEX i4
446 do_execsql_test 5.8.0 {CREATE INDEX i4 ON t2(c)}
447 det 5.8.1 "SELECT c, d FROM t2 ORDER BY c" {
448 0 0 0 {SCAN TABLE t2 USING INDEX i4}
451 # EVIDENCE-OF: R-13931-10421 sqlite> EXPLAIN QUERY PLAN SELECT
452 # (SELECT b FROM t1 WHERE a=0), (SELECT a FROM t1 WHERE b=t2.c) FROM t2;
453 # 0|0|0|SCAN TABLE t2
454 # 0|0|0|EXECUTE SCALAR SUBQUERY 1
455 # 1|0|0|SEARCH TABLE t1 USING COVERING INDEX i2 (a=?)
456 # 0|0|0|EXECUTE CORRELATED SCALAR SUBQUERY 2
457 # 2|0|0|SEARCH TABLE t1 USING INDEX i3 (b=?)
460 SELECT (SELECT b FROM t1 WHERE a=0), (SELECT a FROM t1 WHERE b=t2.c) FROM t2
462 0 0 0 {SCAN TABLE t2 USING COVERING INDEX i4}
463 0 0 0 {EXECUTE SCALAR SUBQUERY 1}
464 1 0 0 {SEARCH TABLE t1 USING COVERING INDEX i2 (a=?)}
465 0 0 0 {EXECUTE CORRELATED SCALAR SUBQUERY 2}
466 2 0 0 {SEARCH TABLE t1 USING INDEX i3 (b=?)}
469 # EVIDENCE-OF: R-50892-45943 sqlite> EXPLAIN QUERY PLAN
470 # SELECT count(*) FROM (SELECT max(b) AS x FROM t1 GROUP BY a) GROUP BY x;
471 # 1|0|0|SCAN TABLE t1 USING COVERING INDEX i2
472 # 0|0|0|SCAN SUBQUERY 1
473 # 0|0|0|USE TEMP B-TREE FOR GROUP BY
476 SELECT count(*) FROM (SELECT max(b) AS x FROM t1 GROUP BY a) GROUP BY x
478 1 0 0 {SCAN TABLE t1 USING COVERING INDEX i2}
479 0 0 0 {SCAN SUBQUERY 1}
480 0 0 0 {USE TEMP B-TREE FOR GROUP BY}
483 # EVIDENCE-OF: R-46219-33846 sqlite> EXPLAIN QUERY PLAN
484 # SELECT * FROM (SELECT * FROM t2 WHERE c=1), t1;
485 # 0|0|0|SEARCH TABLE t2 USING INDEX i4 (c=?)
486 # 0|1|1|SCAN TABLE t1
488 det 5.11 "SELECT a, b FROM (SELECT * FROM t2 WHERE c=1), t1" {
489 0 0 0 {SEARCH TABLE t2 USING INDEX i4 (c=?)}
490 0 1 1 {SCAN TABLE t1 USING COVERING INDEX i2}
493 # EVIDENCE-OF: R-37879-39987 sqlite> EXPLAIN QUERY PLAN
494 # SELECT a FROM t1 UNION SELECT c FROM t2;
495 # 1|0|0|SCAN TABLE t1
496 # 2|0|0|SCAN TABLE t2
497 # 0|0|0|COMPOUND SUBQUERIES 1 AND 2 USING TEMP B-TREE (UNION)
499 det 5.12 "SELECT a,b FROM t1 UNION SELECT c, 99 FROM t2" {
500 1 0 0 {SCAN TABLE t1 USING COVERING INDEX i2}
501 2 0 0 {SCAN TABLE t2 USING COVERING INDEX i4}
502 0 0 0 {COMPOUND SUBQUERIES 1 AND 2 USING TEMP B-TREE (UNION)}
505 # EVIDENCE-OF: R-44864-63011 sqlite> EXPLAIN QUERY PLAN
506 # SELECT a FROM t1 EXCEPT SELECT d FROM t2 ORDER BY 1;
507 # 1|0|0|SCAN TABLE t1 USING COVERING INDEX i2
508 # 2|0|0|SCAN TABLE t2 2|0|0|USE TEMP B-TREE FOR ORDER BY
509 # 0|0|0|COMPOUND SUBQUERIES 1 AND 2 (EXCEPT)
511 det 5.13 "SELECT a FROM t1 EXCEPT SELECT d FROM t2 ORDER BY 1" {
512 1 0 0 {SCAN TABLE t1 USING COVERING INDEX i1}
513 2 0 0 {SCAN TABLE t2}
514 2 0 0 {USE TEMP B-TREE FOR ORDER BY}
515 0 0 0 {COMPOUND SUBQUERIES 1 AND 2 (EXCEPT)}
519 if {![nonzero_reserved_bytes]} {
520 #-------------------------------------------------------------------------
521 # The following tests - eqp-6.* - test that the example C code on
522 # documentation page eqp.html works. The C code is duplicated in test1.c
523 # and wrapped in Tcl command [print_explain_query_plan]
526 proc explain_query_plan {db sql} {
527 set stmt [sqlite3_prepare_v2 db $sql -1 DUMMY]
528 print_explain_query_plan $stmt
529 sqlite3_finalize $stmt
532 explain_query_plan db {%SQL%}
537 # Do a "Print Explain Query Plan" test.
538 proc do_peqp_test {tn sql res} {
539 set fd [open script.tcl w]
540 puts $fd [string map [list %SQL% $sql] $::boilerplate]
543 uplevel do_test $tn [list {
544 set fd [open "|[info nameofexec] script.tcl"]
552 SELECT a, b FROM t1 EXCEPT SELECT d, 99 FROM t2 ORDER BY 1
554 1 0 0 SCAN TABLE t1 USING COVERING INDEX i2
556 2 0 0 USE TEMP B-TREE FOR ORDER BY
557 0 0 0 COMPOUND SUBQUERIES 1 AND 2 (EXCEPT)
561 #-------------------------------------------------------------------------
562 # The following tests - eqp-7.* - test that queries that use the OP_Count
563 # optimization return something sensible with EQP.
567 do_execsql_test 7.0 {
568 CREATE TABLE t1(a INT, b INT, ex CHAR(100));
569 CREATE TABLE t2(a INT, b INT, ex CHAR(100));
570 CREATE INDEX i1 ON t2(a);
573 det 7.1 "SELECT count(*) FROM t1" {
574 0 0 0 {SCAN TABLE t1}
577 det 7.2 "SELECT count(*) FROM t2" {
578 0 0 0 {SCAN TABLE t2 USING COVERING INDEX i1}
581 do_execsql_test 7.3 {
582 INSERT INTO t1(a,b) VALUES(1, 2);
583 INSERT INTO t1(a,b) VALUES(3, 4);
585 INSERT INTO t2(a,b) VALUES(1, 2);
586 INSERT INTO t2(a,b) VALUES(3, 4);
587 INSERT INTO t2(a,b) VALUES(5, 6);
595 det 7.4 "SELECT count(*) FROM t1" {
596 0 0 0 {SCAN TABLE t1}
599 det 7.5 "SELECT count(*) FROM t2" {
600 0 0 0 {SCAN TABLE t2 USING COVERING INDEX i1}
603 #-------------------------------------------------------------------------
604 # The following tests - eqp-8.* - test that queries that use the OP_Count
605 # optimization return something sensible with EQP.
609 do_execsql_test 8.0 {
610 CREATE TABLE t1(a, b, c, PRIMARY KEY(b, c)) WITHOUT ROWID;
611 CREATE TABLE t2(a, b, c);
614 det 8.1.1 "SELECT * FROM t2" {
615 0 0 0 {SCAN TABLE t2}
618 det 8.1.2 "SELECT * FROM t2 WHERE rowid=?" {
619 0 0 0 {SEARCH TABLE t2 USING INTEGER PRIMARY KEY (rowid=?)}
622 det 8.1.3 "SELECT count(*) FROM t2" {
623 0 0 0 {SCAN TABLE t2}
626 det 8.2.1 "SELECT * FROM t1" {
627 0 0 0 {SCAN TABLE t1}
630 det 8.2.2 "SELECT * FROM t1 WHERE b=?" {
631 0 0 0 {SEARCH TABLE t1 USING PRIMARY KEY (b=?)}
634 det 8.2.3 "SELECT * FROM t1 WHERE b=? AND c=?" {
635 0 0 0 {SEARCH TABLE t1 USING PRIMARY KEY (b=? AND c=?)}
638 det 8.2.4 "SELECT count(*) FROM t1" {
639 0 0 0 {SCAN TABLE t1}