blob: 31ace8eda574e2cd3063f718b2768d85131a61c8 [file] [log] [blame]
drh92e4feb2011-03-04 00:56:58 +00001# 2011 March 3
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# This file implements tests for SQLite library. The focus of the tests
13# in this file a corner-case query planner optimization involving the
14# join order of two tables of different sizes.
15#
16
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19
dan8ad169a2013-08-12 20:14:04 +000020ifcapable !stat4&&!stat3 {
drh92e4feb2011-03-04 00:56:58 +000021 finish_test
22 return
23}
24
25set testprefix analyze6
26
27proc eqp {sql {db db}} {
28 uplevel execsql [list "EXPLAIN QUERY PLAN $sql"] $db
29}
30
31do_test analyze6-1.0 {
32 db eval {
drhd9e3cad2013-10-04 02:36:19 +000033 CREATE TABLE cat(x INT, yz TEXT);
drh92e4feb2011-03-04 00:56:58 +000034 CREATE UNIQUE INDEX catx ON cat(x);
35 /* Give cat 16 unique integers */
drhd9e3cad2013-10-04 02:36:19 +000036 INSERT INTO cat(x) VALUES(1);
37 INSERT INTO cat(x) VALUES(2);
38 INSERT INTO cat(x) SELECT x+2 FROM cat;
39 INSERT INTO cat(x) SELECT x+4 FROM cat;
40 INSERT INTO cat(x) SELECT x+8 FROM cat;
drh92e4feb2011-03-04 00:56:58 +000041
42 CREATE TABLE ev(y INT);
43 CREATE INDEX evy ON ev(y);
44 /* ev will hold 32 copies of 16 integers found in cat */
45 INSERT INTO ev SELECT x FROM cat;
46 INSERT INTO ev SELECT x FROM cat;
47 INSERT INTO ev SELECT y FROM ev;
48 INSERT INTO ev SELECT y FROM ev;
49 INSERT INTO ev SELECT y FROM ev;
50 INSERT INTO ev SELECT y FROM ev;
51 ANALYZE;
52 SELECT count(*) FROM cat;
53 SELECT count(*) FROM ev;
54 }
55} {16 512}
56
57# The lowest cost plan is to scan CAT and for each integer there, do a single
58# lookup of the first corresponding entry in EV then read off the equal values
59# in EV. (Prior to the 2011-03-04 enhancement to where.c, this query would
60# have used EV for the outer loop instead of CAT - which was about 3x slower.)
61#
62do_test analyze6-1.1 {
63 eqp {SELECT count(*) FROM ev, cat WHERE x=y}
drh5822d6f2013-06-10 23:30:09 +000064} {0 0 1 {SCAN TABLE cat USING COVERING INDEX catx} 0 1 0 {SEARCH TABLE ev USING COVERING INDEX evy (y=?)}}
drh92e4feb2011-03-04 00:56:58 +000065
66# The same plan is chosen regardless of the order of the tables in the
67# FROM clause.
68#
69do_test analyze6-1.2 {
70 eqp {SELECT count(*) FROM cat, ev WHERE x=y}
drh5822d6f2013-06-10 23:30:09 +000071} {0 0 0 {SCAN TABLE cat USING COVERING INDEX catx} 0 1 1 {SEARCH TABLE ev USING COVERING INDEX evy (y=?)}}
drh92e4feb2011-03-04 00:56:58 +000072
73
drhf6cf1ff2011-03-30 14:54:05 +000074# Ticket [83ea97620bd3101645138b7b0e71c12c5498fe3d] 2011-03-30
75# If ANALYZE is run on an empty table, make sure indices are used
76# on the table.
77#
78do_test analyze6-2.1 {
79 execsql {
80 CREATE TABLE t201(x INTEGER PRIMARY KEY, y UNIQUE, z);
81 CREATE INDEX t201z ON t201(z);
82 ANALYZE;
83 }
84 eqp {SELECT * FROM t201 WHERE z=5}
drh5822d6f2013-06-10 23:30:09 +000085} {0 0 0 {SEARCH TABLE t201 USING INDEX t201z (z=?)}}
drhf6cf1ff2011-03-30 14:54:05 +000086do_test analyze6-2.2 {
87 eqp {SELECT * FROM t201 WHERE y=5}
drh5822d6f2013-06-10 23:30:09 +000088} {0 0 0 {SEARCH TABLE t201 USING INDEX sqlite_autoindex_t201_1 (y=?)}}
drhf6cf1ff2011-03-30 14:54:05 +000089do_test analyze6-2.3 {
90 eqp {SELECT * FROM t201 WHERE x=5}
drh5822d6f2013-06-10 23:30:09 +000091} {0 0 0 {SEARCH TABLE t201 USING INTEGER PRIMARY KEY (rowid=?)}}
drhf6cf1ff2011-03-30 14:54:05 +000092do_test analyze6-2.4 {
93 execsql {
drh358406f2014-07-22 14:42:16 +000094 INSERT INTO t201 VALUES(1,2,3),(2,3,4),(3,4,5);
drhf6cf1ff2011-03-30 14:54:05 +000095 ANALYZE t201;
96 }
97 eqp {SELECT * FROM t201 WHERE z=5}
drh5822d6f2013-06-10 23:30:09 +000098} {0 0 0 {SEARCH TABLE t201 USING INDEX t201z (z=?)}}
drhf6cf1ff2011-03-30 14:54:05 +000099do_test analyze6-2.5 {
100 eqp {SELECT * FROM t201 WHERE y=5}
drh5822d6f2013-06-10 23:30:09 +0000101} {0 0 0 {SEARCH TABLE t201 USING INDEX sqlite_autoindex_t201_1 (y=?)}}
drhf6cf1ff2011-03-30 14:54:05 +0000102do_test analyze6-2.6 {
103 eqp {SELECT * FROM t201 WHERE x=5}
drh5822d6f2013-06-10 23:30:09 +0000104} {0 0 0 {SEARCH TABLE t201 USING INTEGER PRIMARY KEY (rowid=?)}}
drhf6cf1ff2011-03-30 14:54:05 +0000105do_test analyze6-2.7 {
106 execsql {
107 INSERT INTO t201 VALUES(4,5,7);
108 INSERT INTO t201 SELECT x+100, y+100, z+100 FROM t201;
109 INSERT INTO t201 SELECT x+200, y+200, z+200 FROM t201;
110 INSERT INTO t201 SELECT x+400, y+400, z+400 FROM t201;
111 ANALYZE t201;
112 }
113 eqp {SELECT * FROM t201 WHERE z=5}
drh5822d6f2013-06-10 23:30:09 +0000114} {0 0 0 {SEARCH TABLE t201 USING INDEX t201z (z=?)}}
drhf6cf1ff2011-03-30 14:54:05 +0000115do_test analyze6-2.8 {
116 eqp {SELECT * FROM t201 WHERE y=5}
drh5822d6f2013-06-10 23:30:09 +0000117} {0 0 0 {SEARCH TABLE t201 USING INDEX sqlite_autoindex_t201_1 (y=?)}}
drhf6cf1ff2011-03-30 14:54:05 +0000118do_test analyze6-2.9 {
119 eqp {SELECT * FROM t201 WHERE x=5}
drh5822d6f2013-06-10 23:30:09 +0000120} {0 0 0 {SEARCH TABLE t201 USING INTEGER PRIMARY KEY (rowid=?)}}
drhf6cf1ff2011-03-30 14:54:05 +0000121
drh92e4feb2011-03-04 00:56:58 +0000122finish_test