OLD | NEW |
(Empty) | |
| 1 # 2010 April 07 |
| 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. The |
| 12 # focus of this script is testing automatic index creation logic. |
| 13 # |
| 14 |
| 15 set testdir [file dirname $argv0] |
| 16 source $testdir/tester.tcl |
| 17 |
| 18 # If the library is not compiled with automatic index support then |
| 19 # skip all tests in this file. |
| 20 # |
| 21 ifcapable {!autoindex} { |
| 22 finish_test |
| 23 return |
| 24 } |
| 25 |
| 26 # With automatic index turned off, we do a full scan of the T2 table |
| 27 do_test autoindex1-100 { |
| 28 db eval { |
| 29 CREATE TABLE t1(a,b); |
| 30 INSERT INTO t1 VALUES(1,11); |
| 31 INSERT INTO t1 VALUES(2,22); |
| 32 INSERT INTO t1 SELECT a+2, b+22 FROM t1; |
| 33 INSERT INTO t1 SELECT a+4, b+44 FROM t1; |
| 34 CREATE TABLE t2(c,d); |
| 35 INSERT INTO t2 SELECT a, 900+b FROM t1; |
| 36 } |
| 37 db eval { |
| 38 PRAGMA automatic_index=OFF; |
| 39 SELECT b, d FROM t1 JOIN t2 ON a=c ORDER BY b; |
| 40 } |
| 41 } {11 911 22 922 33 933 44 944 55 955 66 966 77 977 88 988} |
| 42 do_test autoindex1-101 { |
| 43 db status step |
| 44 } {63} |
| 45 do_test autoindex1-102 { |
| 46 db status autoindex |
| 47 } {0} |
| 48 |
| 49 # With autoindex turned on, we build an index once and then use that index |
| 50 # to find T2 values. |
| 51 do_test autoindex1-110 { |
| 52 db eval { |
| 53 PRAGMA automatic_index=ON; |
| 54 SELECT b, d FROM t1 JOIN t2 ON a=c ORDER BY b; |
| 55 } |
| 56 } {11 911 22 922 33 933 44 944 55 955 66 966 77 977 88 988} |
| 57 do_test autoindex1-111 { |
| 58 db status step |
| 59 } {7} |
| 60 do_test autoindex1-112 { |
| 61 db status autoindex |
| 62 } {7} |
| 63 |
| 64 # The same test as above, but this time the T2 query is a subquery rather |
| 65 # than a join. |
| 66 do_test autoindex1-200 { |
| 67 db eval { |
| 68 PRAGMA automatic_index=OFF; |
| 69 SELECT b, (SELECT d FROM t2 WHERE c=a) FROM t1; |
| 70 } |
| 71 } {11 911 22 922 33 933 44 944 55 955 66 966 77 977 88 988} |
| 72 do_test autoindex1-201 { |
| 73 db status step |
| 74 } {35} |
| 75 do_test autoindex1-202 { |
| 76 db status autoindex |
| 77 } {0} |
| 78 do_test autoindex1-210 { |
| 79 db eval { |
| 80 PRAGMA automatic_index=ON; |
| 81 SELECT b, (SELECT d FROM t2 WHERE c=a) FROM t1; |
| 82 } |
| 83 } {11 911 22 922 33 933 44 944 55 955 66 966 77 977 88 988} |
| 84 do_test autoindex1-211 { |
| 85 db status step |
| 86 } {7} |
| 87 do_test autoindex1-212 { |
| 88 db status autoindex |
| 89 } {7} |
| 90 |
| 91 |
| 92 # Modify the second table of the join while the join is in progress |
| 93 # |
| 94 do_test autoindex1-300 { |
| 95 set r {} |
| 96 db eval {SELECT b, d FROM t1 JOIN t2 ON (c=a)} { |
| 97 lappend r $b $d |
| 98 db eval {UPDATE t2 SET d=d+1} |
| 99 } |
| 100 set r |
| 101 } {11 911 22 922 33 933 44 944 55 955 66 966 77 977 88 988} |
| 102 do_test autoindex1-310 { |
| 103 db eval {SELECT d FROM t2 ORDER BY d} |
| 104 } {919 930 941 952 963 974 985 996} |
| 105 |
| 106 # The next test does a 10-way join on unindexed tables. Without |
| 107 # automatic indices, the join will take a long time to complete. |
| 108 # With automatic indices, it should only take about a second. |
| 109 # |
| 110 do_test autoindex1-400 { |
| 111 db eval { |
| 112 CREATE TABLE t4(a, b); |
| 113 INSERT INTO t4 VALUES(1,2); |
| 114 INSERT INTO t4 VALUES(2,3); |
| 115 } |
| 116 for {set n 2} {$n<4096} {set n [expr {$n+$n}]} { |
| 117 db eval {INSERT INTO t4 SELECT a+$n, b+$n FROM t4} |
| 118 } |
| 119 db eval { |
| 120 SELECT count(*) FROM t4; |
| 121 } |
| 122 } {4096} |
| 123 do_test autoindex1-401 { |
| 124 db eval { |
| 125 SELECT count(*) |
| 126 FROM t4 AS x1 |
| 127 JOIN t4 AS x2 ON x2.a=x1.b |
| 128 JOIN t4 AS x3 ON x3.a=x2.b |
| 129 JOIN t4 AS x4 ON x4.a=x3.b |
| 130 JOIN t4 AS x5 ON x5.a=x4.b |
| 131 JOIN t4 AS x6 ON x6.a=x5.b |
| 132 JOIN t4 AS x7 ON x7.a=x6.b |
| 133 JOIN t4 AS x8 ON x8.a=x7.b |
| 134 JOIN t4 AS x9 ON x9.a=x8.b |
| 135 JOIN t4 AS x10 ON x10.a=x9.b; |
| 136 } |
| 137 } {4087} |
| 138 |
| 139 # Ticket [8011086c85c6c404014c947fcf3eb9f42b184a0d] from 2010-07-08 |
| 140 # Make sure automatic indices are not created for the RHS of an IN expression |
| 141 # that is not a correlated subquery. |
| 142 # |
| 143 do_test autoindex1-500 { |
| 144 db eval { |
| 145 CREATE TABLE t501(a INTEGER PRIMARY KEY, b); |
| 146 CREATE TABLE t502(x INTEGER PRIMARY KEY, y); |
| 147 EXPLAIN QUERY PLAN |
| 148 SELECT b FROM t501 |
| 149 WHERE t501.a IN (SELECT x FROM t502 WHERE y=?); |
| 150 } |
| 151 } {0 0 {TABLE t501 USING PRIMARY KEY} 0 0 {TABLE t502}} |
| 152 do_test autoindex1-501 { |
| 153 db eval { |
| 154 EXPLAIN QUERY PLAN |
| 155 SELECT b FROM t501 |
| 156 WHERE t501.a IN (SELECT x FROM t502 WHERE y=t501.b); |
| 157 } |
| 158 } {0 0 {TABLE t501} 0 0 {TABLE t502 WITH AUTOMATIC INDEX}} |
| 159 do_test autoindex1-502 { |
| 160 db eval { |
| 161 EXPLAIN QUERY PLAN |
| 162 SELECT b FROM t501 |
| 163 WHERE t501.a=123 |
| 164 AND t501.a IN (SELECT x FROM t502 WHERE y=t501.b); |
| 165 } |
| 166 } {0 0 {TABLE t501 USING PRIMARY KEY} 0 0 {TABLE t502}} |
| 167 |
| 168 |
| 169 finish_test |
OLD | NEW |