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 #***********************************************************************
11 # Tests for functionality related to ANALYZE.
14 set testdir [file dirname $argv0]
15 source $testdir/tester.tcl
21 set testprefix analyzeG
23 proc do_scan_order_test {tn sql expect} {
24 uplevel [list do_test $tn [subst -nocommands {
26 db eval "explain query plan $sql" {
27 lappend res [set detail]
33 #-------------------------------------------------------------------------
34 # Test cases 1.* seek to verify that even if an index is not used, its
35 # stat4 data may be used by the planner to estimate the number of
36 # rows that match an unindexed constraint on the same column.
39 PRAGMA automatic_index = 0;
40 CREATE TABLE t1(a, x);
41 CREATE TABLE t2(b, y);
43 SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<100
45 INSERT INTO t1 SELECT (i%50), NULL FROM s;
47 SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<100
49 INSERT INTO t2 SELECT (CASE WHEN i<95 THEN 44 ELSE i END), NULL FROM s;
52 # Join tables t1 and t2. Both contain 100 rows. (a=44) matches 2 rows
53 # in "t1", (b=44) matches 95 rows in table "t2". But the planner doesn't
54 # know this, so it has no preference as to which order the tables are
55 # scanned in. In practice this means that tables are scanned in the order
56 # they are specified in in the FROM clause.
57 do_scan_order_test 1.1.1 {
58 SELECT * FROM t1, t2 WHERE a=44 AND b=44;
60 {SCAN TABLE t1} {SCAN TABLE t2}
62 do_scan_order_test 1.1.2 {
63 SELECT * FROM t2, t1 WHERE a=44 AND b=44
65 {SCAN TABLE t2} {SCAN TABLE t1}
69 CREATE INDEX t2b ON t2(b);
73 # Now, with the ANALYZE data, the planner knows that (b=44) matches a
74 # large number of rows. So it elects to scan table "t1" first, regardless
75 # of the order in which the tables are specified in the FROM clause.
76 do_scan_order_test 1.3.1 {
77 SELECT * FROM t1, t2 WHERE a=44 AND b=44;
79 {SCAN TABLE t1} {SCAN TABLE t2}
81 do_scan_order_test 1.3.2 {
82 SELECT * FROM t2, t1 WHERE a=44 AND b=44
84 {SCAN TABLE t1} {SCAN TABLE t2}