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 #-------------------------------------------------------------------------
24 # Test cases 1.* seek to verify that even if an index is not used, its
25 # stat4 data may be used by the planner to estimate the number of
26 # rows that match an unindexed constraint on the same column.
29 PRAGMA automatic_index = 0;
30 CREATE TABLE t1(a, x);
31 CREATE TABLE t2(b, y);
33 SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<100
35 INSERT INTO t1 SELECT (i%50), NULL FROM s;
37 SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<100
39 INSERT INTO t2 SELECT (CASE WHEN i<95 THEN 44 ELSE i END), NULL FROM s;
42 # Join tables t1 and t2. Both contain 100 rows. (a=44) matches 2 rows
43 # in "t1", (b=44) matches 95 rows in table "t2". But the planner doesn't
44 # know this, so it has no preference as to which order the tables are
45 # scanned in. In practice this means that tables are scanned in the order
46 # they are specified in in the FROM clause.
48 SELECT * FROM t1, t2 WHERE a=44 AND b=44;
53 SELECT * FROM t2, t1 WHERE a=44 AND b=44
61 CREATE INDEX t2b ON t2(b);
65 # Now, with the ANALYZE data, the planner knows that (b=44) matches a
66 # large number of rows. So it elects to scan table "t1" first, regardless
67 # of the order in which the tables are specified in the FROM clause.
69 SELECT * FROM t1, t2 WHERE a=44 AND b=44;
76 SELECT * FROM t2, t1 WHERE a=44 AND b=44