xref: /sqlite-3.40.0/test/analyze7.test (revision 8210233c)
1be28a9b0Sdrh# 2011 April 1
2be28a9b0Sdrh#
3be28a9b0Sdrh# The author disclaims copyright to this source code.  In place of
4be28a9b0Sdrh# a legal notice, here is a blessing:
5be28a9b0Sdrh#
6be28a9b0Sdrh#    May you do good and not evil.
7be28a9b0Sdrh#    May you find forgiveness for yourself and forgive others.
8be28a9b0Sdrh#    May you share freely, never taking more than you give.
9be28a9b0Sdrh#
10be28a9b0Sdrh#***********************************************************************
11be28a9b0Sdrh# This file implements regression tests for SQLite library.
12be28a9b0Sdrh# This file implements tests for the ANALYZE command when an idnex
13be28a9b0Sdrh# name is given as the argument.
14be28a9b0Sdrh#
15be28a9b0Sdrh
16be28a9b0Sdrhset testdir [file dirname $argv0]
17be28a9b0Sdrhsource $testdir/tester.tcl
18be28a9b0Sdrh
19be28a9b0Sdrh# There is nothing to test if ANALYZE is disable for this build.
20be28a9b0Sdrh#
2182346d98Sdanifcapable {!analyze||!vtab} {
22be28a9b0Sdrh  finish_test
23be28a9b0Sdrh  return
24be28a9b0Sdrh}
25be28a9b0Sdrh
26be28a9b0Sdrh# Generate some test data
27be28a9b0Sdrh#
28be28a9b0Sdrhdo_test analyze7-1.0 {
2924b6422dSdrh  load_static_extension db wholenumber
30be28a9b0Sdrh  execsql {
31be28a9b0Sdrh    CREATE TABLE t1(a,b,c,d);
32be28a9b0Sdrh    CREATE INDEX t1a ON t1(a);
33be28a9b0Sdrh    CREATE INDEX t1b ON t1(b);
34be28a9b0Sdrh    CREATE INDEX t1cd ON t1(c,d);
3570586bebSdrh    CREATE VIRTUAL TABLE nums USING wholenumber;
3670586bebSdrh    INSERT INTO t1 SELECT value, value, value/100, value FROM nums
3770586bebSdrh                    WHERE value BETWEEN 1 AND 256;
38be28a9b0Sdrh    EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123;
39be28a9b0Sdrh  }
40*8210233cSdrh} {/*SEARCH t1 USING INDEX t1a (a=?)*/}
41be28a9b0Sdrhdo_test analyze7-1.1 {
42be28a9b0Sdrh  execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b=123;}
43*8210233cSdrh} {/*SEARCH t1 USING INDEX t1b (b=?)*/}
44be28a9b0Sdrhdo_test analyze7-1.2 {
45be28a9b0Sdrh  execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=2;}
46*8210233cSdrh} {/*SEARCH t1 USING INDEX t1cd (c=?)*/}
47be28a9b0Sdrh
48be28a9b0Sdrh# Run an analyze on one of the three indices.  Verify that this
49be28a9b0Sdrh# effects the row-count estimate on the one query that uses that
50be28a9b0Sdrh# one index.
51be28a9b0Sdrh#
52be28a9b0Sdrhdo_test analyze7-2.0 {
53be28a9b0Sdrh  execsql {ANALYZE t1a;}
54be28a9b0Sdrh  db cache flush
55be28a9b0Sdrh  execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123;}
56*8210233cSdrh} {/*SEARCH t1 USING INDEX t1a (a=?)*/}
57be28a9b0Sdrhdo_test analyze7-2.1 {
58be28a9b0Sdrh  execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b=123;}
59*8210233cSdrh} {/*SEARCH t1 USING INDEX t1b (b=?)*/}
60be28a9b0Sdrhdo_test analyze7-2.2 {
61be28a9b0Sdrh  execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=2;}
62*8210233cSdrh} {/*SEARCH t1 USING INDEX t1cd (c=?)*/}
63be28a9b0Sdrh
64be28a9b0Sdrh# Verify that since the query planner now things that t1a is more
65be28a9b0Sdrh# selective than t1b, it prefers to use t1a.
66be28a9b0Sdrh#
67be28a9b0Sdrhdo_test analyze7-2.3 {
68be28a9b0Sdrh  execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123 AND b=123}
69*8210233cSdrh} {/*SEARCH t1 USING INDEX t1a (a=?)*/}
70be28a9b0Sdrh
71be28a9b0Sdrh# Run an analysis on another of the three indices.  Verify  that this
72be28a9b0Sdrh# new analysis works and does not disrupt the previous analysis.
73be28a9b0Sdrh#
74be28a9b0Sdrhdo_test analyze7-3.0 {
75be28a9b0Sdrh  execsql {ANALYZE t1cd;}
76be28a9b0Sdrh  db cache flush;
77be28a9b0Sdrh  execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123;}
78*8210233cSdrh} {/*SEARCH t1 USING INDEX t1a (a=?)*/}
79be28a9b0Sdrhdo_test analyze7-3.1 {
80be28a9b0Sdrh  execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b=123;}
81*8210233cSdrh} {/*SEARCH t1 USING INDEX t1b (b=?)*/}
82e433235eSdando_test analyze7-3.2.1 {
83e433235eSdan  execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=?;}
84*8210233cSdrh} {/*SEARCH t1 USING INDEX t1cd (c=?)*/}
85175b8f06Sdrhifcapable stat4 {
86f52bb8d3Sdan  # If ENABLE_STAT4 is defined, SQLite comes up with a different estimated
87e433235eSdan  # row count for (c=2) than it does for (c=?).
88e433235eSdan  do_test analyze7-3.2.2 {
89be28a9b0Sdrh    execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=2;}
90*8210233cSdrh  } {/*SEARCH t1 USING INDEX t1cd (c=?)*/}
91e433235eSdan} else {
92f52bb8d3Sdan  # If ENABLE_STAT4 is not defined, the expected row count for (c=2) is the
93e433235eSdan  # same as that for (c=?).
94e433235eSdan  do_test analyze7-3.2.3 {
95e433235eSdan    execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=2;}
96*8210233cSdrh  } {/*SEARCH t1 USING INDEX t1cd (c=?)*/}
97e433235eSdan}
98be28a9b0Sdrhdo_test analyze7-3.3 {
99be28a9b0Sdrh  execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123 AND b=123}
100*8210233cSdrh} {/*SEARCH t1 USING INDEX t1a (a=?)*/}
1018ad169abSdan
102175b8f06Sdrhifcapable {!stat4} {
103be28a9b0Sdrh  do_test analyze7-3.4 {
104be28a9b0Sdrh    execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=123 AND b=123}
105*8210233cSdrh  } {/*SEARCH t1 USING INDEX t1b (b=?)*/}
106be28a9b0Sdrh  do_test analyze7-3.5 {
107be28a9b0Sdrh    execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123 AND c=123}
108*8210233cSdrh  } {/*SEARCH t1 USING INDEX t1a (a=?)*/}
1094e50c5ecSdrh}
110be28a9b0Sdrhdo_test analyze7-3.6 {
111be28a9b0Sdrh  execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=123 AND d=123 AND b=123}
112*8210233cSdrh} {/*SEARCH t1 USING INDEX t1cd (c=? AND d=?)*/}
113be28a9b0Sdrh
114be28a9b0Sdrhfinish_test
115