drh | be28a9b | 2011-04-01 14:04:36 +0000 | [diff] [blame] | 1 | # 2011 April 1 |
| 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 | # This file implements regression tests for SQLite library. |
| 12 | # This file implements tests for the ANALYZE command when an idnex |
| 13 | # name is given as the argument. |
| 14 | # |
| 15 | |
| 16 | set testdir [file dirname $argv0] |
| 17 | source $testdir/tester.tcl |
| 18 | |
| 19 | # There is nothing to test if ANALYZE is disable for this build. |
| 20 | # |
dan | 82346d9 | 2011-04-02 09:25:14 +0000 | [diff] [blame] | 21 | ifcapable {!analyze||!vtab} { |
drh | be28a9b | 2011-04-01 14:04:36 +0000 | [diff] [blame] | 22 | finish_test |
| 23 | return |
| 24 | } |
| 25 | |
| 26 | # Generate some test data |
| 27 | # |
| 28 | do_test analyze7-1.0 { |
drh | 24b6422 | 2013-04-25 11:58:36 +0000 | [diff] [blame] | 29 | load_static_extension db wholenumber |
drh | be28a9b | 2011-04-01 14:04:36 +0000 | [diff] [blame] | 30 | execsql { |
drh | be28a9b | 2011-04-01 14:04:36 +0000 | [diff] [blame] | 31 | CREATE TABLE t1(a,b,c,d); |
| 32 | CREATE INDEX t1a ON t1(a); |
| 33 | CREATE INDEX t1b ON t1(b); |
| 34 | CREATE INDEX t1cd ON t1(c,d); |
drh | 70586be | 2011-04-01 23:49:44 +0000 | [diff] [blame] | 35 | CREATE VIRTUAL TABLE nums USING wholenumber; |
| 36 | INSERT INTO t1 SELECT value, value, value/100, value FROM nums |
| 37 | WHERE value BETWEEN 1 AND 256; |
drh | be28a9b | 2011-04-01 14:04:36 +0000 | [diff] [blame] | 38 | EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123; |
| 39 | } |
drh | 5822d6f | 2013-06-10 23:30:09 +0000 | [diff] [blame] | 40 | } {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}} |
drh | be28a9b | 2011-04-01 14:04:36 +0000 | [diff] [blame] | 41 | do_test analyze7-1.1 { |
| 42 | execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b=123;} |
drh | 5822d6f | 2013-06-10 23:30:09 +0000 | [diff] [blame] | 43 | } {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?)}} |
drh | be28a9b | 2011-04-01 14:04:36 +0000 | [diff] [blame] | 44 | do_test analyze7-1.2 { |
| 45 | execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=2;} |
drh | 5822d6f | 2013-06-10 23:30:09 +0000 | [diff] [blame] | 46 | } {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=?)}} |
drh | be28a9b | 2011-04-01 14:04:36 +0000 | [diff] [blame] | 47 | |
| 48 | # Run an analyze on one of the three indices. Verify that this |
| 49 | # effects the row-count estimate on the one query that uses that |
| 50 | # one index. |
| 51 | # |
| 52 | do_test analyze7-2.0 { |
| 53 | execsql {ANALYZE t1a;} |
| 54 | db cache flush |
| 55 | execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123;} |
drh | 5822d6f | 2013-06-10 23:30:09 +0000 | [diff] [blame] | 56 | } {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}} |
drh | be28a9b | 2011-04-01 14:04:36 +0000 | [diff] [blame] | 57 | do_test analyze7-2.1 { |
| 58 | execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b=123;} |
drh | 5822d6f | 2013-06-10 23:30:09 +0000 | [diff] [blame] | 59 | } {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?)}} |
drh | be28a9b | 2011-04-01 14:04:36 +0000 | [diff] [blame] | 60 | do_test analyze7-2.2 { |
| 61 | execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=2;} |
drh | 5822d6f | 2013-06-10 23:30:09 +0000 | [diff] [blame] | 62 | } {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=?)}} |
drh | be28a9b | 2011-04-01 14:04:36 +0000 | [diff] [blame] | 63 | |
| 64 | # Verify that since the query planner now things that t1a is more |
| 65 | # selective than t1b, it prefers to use t1a. |
| 66 | # |
| 67 | do_test analyze7-2.3 { |
| 68 | execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123 AND b=123} |
drh | 5822d6f | 2013-06-10 23:30:09 +0000 | [diff] [blame] | 69 | } {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}} |
drh | be28a9b | 2011-04-01 14:04:36 +0000 | [diff] [blame] | 70 | |
| 71 | # Run an analysis on another of the three indices. Verify that this |
| 72 | # new analysis works and does not disrupt the previous analysis. |
| 73 | # |
| 74 | do_test analyze7-3.0 { |
| 75 | execsql {ANALYZE t1cd;} |
| 76 | db cache flush; |
| 77 | execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123;} |
drh | 5822d6f | 2013-06-10 23:30:09 +0000 | [diff] [blame] | 78 | } {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}} |
drh | be28a9b | 2011-04-01 14:04:36 +0000 | [diff] [blame] | 79 | do_test analyze7-3.1 { |
| 80 | execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b=123;} |
drh | 5822d6f | 2013-06-10 23:30:09 +0000 | [diff] [blame] | 81 | } {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?)}} |
dan | e433235 | 2011-04-01 17:53:19 +0000 | [diff] [blame] | 82 | do_test analyze7-3.2.1 { |
| 83 | execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=?;} |
drh | 5822d6f | 2013-06-10 23:30:09 +0000 | [diff] [blame] | 84 | } {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=?)}} |
dan | 8ad169a | 2013-08-12 20:14:04 +0000 | [diff] [blame] | 85 | ifcapable stat4||stat3 { |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 86 | # If ENABLE_STAT4 is defined, SQLite comes up with a different estimated |
dan | e433235 | 2011-04-01 17:53:19 +0000 | [diff] [blame] | 87 | # row count for (c=2) than it does for (c=?). |
| 88 | do_test analyze7-3.2.2 { |
| 89 | execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=2;} |
drh | 5822d6f | 2013-06-10 23:30:09 +0000 | [diff] [blame] | 90 | } {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=?)}} |
dan | e433235 | 2011-04-01 17:53:19 +0000 | [diff] [blame] | 91 | } else { |
dan | f52bb8d | 2013-08-03 20:24:58 +0000 | [diff] [blame] | 92 | # If ENABLE_STAT4 is not defined, the expected row count for (c=2) is the |
dan | e433235 | 2011-04-01 17:53:19 +0000 | [diff] [blame] | 93 | # same as that for (c=?). |
| 94 | do_test analyze7-3.2.3 { |
| 95 | execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=2;} |
drh | 5822d6f | 2013-06-10 23:30:09 +0000 | [diff] [blame] | 96 | } {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=?)}} |
dan | e433235 | 2011-04-01 17:53:19 +0000 | [diff] [blame] | 97 | } |
drh | be28a9b | 2011-04-01 14:04:36 +0000 | [diff] [blame] | 98 | do_test analyze7-3.3 { |
| 99 | execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123 AND b=123} |
drh | 5822d6f | 2013-06-10 23:30:09 +0000 | [diff] [blame] | 100 | } {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}} |
dan | 8ad169a | 2013-08-12 20:14:04 +0000 | [diff] [blame] | 101 | |
| 102 | ifcapable {!stat4 && !stat3} { |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 103 | do_test analyze7-3.4 { |
| 104 | execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=123 AND b=123} |
drh | 5822d6f | 2013-06-10 23:30:09 +0000 | [diff] [blame] | 105 | } {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?)}} |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 106 | do_test analyze7-3.5 { |
| 107 | execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123 AND c=123} |
drh | 5822d6f | 2013-06-10 23:30:09 +0000 | [diff] [blame] | 108 | } {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?)}} |
drh | 4e50c5e | 2011-08-13 19:35:19 +0000 | [diff] [blame] | 109 | } |
drh | be28a9b | 2011-04-01 14:04:36 +0000 | [diff] [blame] | 110 | do_test analyze7-3.6 { |
| 111 | execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=123 AND d=123 AND b=123} |
drh | 5822d6f | 2013-06-10 23:30:09 +0000 | [diff] [blame] | 112 | } {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=? AND d=?)}} |
drh | be28a9b | 2011-04-01 14:04:36 +0000 | [diff] [blame] | 113 | |
| 114 | finish_test |