OLD | NEW |
1 # 2008 October 4 | 1 # 2008-10-04 |
2 # | 2 # |
3 # The author disclaims copyright to this source code. In place of | 3 # The author disclaims copyright to this source code. In place of |
4 # a legal notice, here is a blessing: | 4 # a legal notice, here is a blessing: |
5 # | 5 # |
6 # May you do good and not evil. | 6 # May you do good and not evil. |
7 # May you find forgiveness for yourself and forgive others. | 7 # May you find forgiveness for yourself and forgive others. |
8 # May you share freely, never taking more than you give. | 8 # May you share freely, never taking more than you give. |
9 # | 9 # |
10 #*********************************************************************** | 10 #*********************************************************************** |
11 # | 11 # |
12 # $Id: indexedby.test,v 1.5 2009/03/22 20:36:19 drh Exp $ | |
13 | 12 |
14 set testdir [file dirname $argv0] | 13 set testdir [file dirname $argv0] |
15 source $testdir/tester.tcl | 14 source $testdir/tester.tcl |
16 set ::testprefix indexedby | 15 set ::testprefix indexedby |
17 | 16 |
18 # Create a schema with some indexes. | 17 # Create a schema with some indexes. |
19 # | 18 # |
20 do_test indexedby-1.1 { | 19 do_test indexedby-1.1 { |
21 execsql { | 20 execsql { |
22 CREATE TABLE t1(a, b); | 21 CREATE TABLE t1(a, b); |
(...skipping 28 matching lines...) Expand all Loading... |
51 EXPLAIN QUERY PLAN select * from t1, t2 WHERE c = 10; | 50 EXPLAIN QUERY PLAN select * from t1, t2 WHERE c = 10; |
52 } { | 51 } { |
53 0 0 1 {SEARCH TABLE t2 USING INDEX i3 (c=?)} | 52 0 0 1 {SEARCH TABLE t2 USING INDEX i3 (c=?)} |
54 0 1 0 {SCAN TABLE t1} | 53 0 1 0 {SCAN TABLE t1} |
55 } | 54 } |
56 | 55 |
57 # Parser tests. Test that an INDEXED BY or NOT INDEX clause can be | 56 # Parser tests. Test that an INDEXED BY or NOT INDEX clause can be |
58 # attached to a table in the FROM clause, but not to a sub-select or | 57 # attached to a table in the FROM clause, but not to a sub-select or |
59 # SQL view. Also test that specifying an index that does not exist or | 58 # SQL view. Also test that specifying an index that does not exist or |
60 # is attached to a different table is detected as an error. | 59 # is attached to a different table is detected as an error. |
| 60 # |
| 61 # EVIDENCE-OF: R-07004-11522 -- syntax diagram qualified-table-name |
61 # | 62 # |
| 63 # EVIDENCE-OF: R-58230-57098 The "INDEXED BY index-name" phrase |
| 64 # specifies that the named index must be used in order to look up values |
| 65 # on the preceding table. |
| 66 # |
62 do_test indexedby-2.1 { | 67 do_test indexedby-2.1 { |
63 execsql { SELECT * FROM t1 NOT INDEXED WHERE a = 'one' AND b = 'two'} | 68 execsql { SELECT * FROM t1 NOT INDEXED WHERE a = 'one' AND b = 'two'} |
64 } {} | 69 } {} |
| 70 do_test indexedby-2.1b { |
| 71 execsql { SELECT * FROM main.t1 NOT INDEXED WHERE a = 'one' AND b = 'two'} |
| 72 } {} |
65 do_test indexedby-2.2 { | 73 do_test indexedby-2.2 { |
66 execsql { SELECT * FROM t1 INDEXED BY i1 WHERE a = 'one' AND b = 'two'} | 74 execsql { SELECT * FROM t1 INDEXED BY i1 WHERE a = 'one' AND b = 'two'} |
67 } {} | 75 } {} |
| 76 do_test indexedby-2.2b { |
| 77 execsql { SELECT * FROM main.t1 INDEXED BY i1 WHERE a = 'one' AND b = 'two'} |
| 78 } {} |
68 do_test indexedby-2.3 { | 79 do_test indexedby-2.3 { |
69 execsql { SELECT * FROM t1 INDEXED BY i2 WHERE a = 'one' AND b = 'two'} | 80 execsql { SELECT * FROM t1 INDEXED BY i2 WHERE a = 'one' AND b = 'two'} |
70 } {} | 81 } {} |
71 | 82 # EVIDENCE-OF: R-44699-55558 The INDEXED BY clause does not give the |
| 83 # optimizer hints about which index to use; it gives the optimizer a |
| 84 # requirement of which index to use. |
| 85 # EVIDENCE-OF: R-15800-25719 If index-name does not exist or cannot be |
| 86 # used for the query, then the preparation of the SQL statement fails. |
| 87 # |
72 do_test indexedby-2.4 { | 88 do_test indexedby-2.4 { |
73 catchsql { SELECT * FROM t1 INDEXED BY i3 WHERE a = 'one' AND b = 'two'} | 89 catchsql { SELECT * FROM t1 INDEXED BY i3 WHERE a = 'one' AND b = 'two'} |
74 } {1 {no such index: i3}} | 90 } {1 {no such index: i3}} |
| 91 |
| 92 # EVIDENCE-OF: R-62112-42456 If the query optimizer is unable to use the |
| 93 # index specified by the INDEX BY clause, then the query will fail with |
| 94 # an error. |
| 95 do_test indexedby-2.4.1 { |
| 96 catchsql { SELECT b FROM t1 INDEXED BY i1 WHERE b = 'two' } |
| 97 } {1 {no query solution}} |
| 98 |
75 do_test indexedby-2.5 { | 99 do_test indexedby-2.5 { |
76 catchsql { SELECT * FROM t1 INDEXED BY i5 WHERE a = 'one' AND b = 'two'} | 100 catchsql { SELECT * FROM t1 INDEXED BY i5 WHERE a = 'one' AND b = 'two'} |
77 } {1 {no such index: i5}} | 101 } {1 {no such index: i5}} |
78 do_test indexedby-2.6 { | 102 do_test indexedby-2.6 { |
79 catchsql { SELECT * FROM t1 INDEXED BY WHERE a = 'one' AND b = 'two'} | 103 catchsql { SELECT * FROM t1 INDEXED BY WHERE a = 'one' AND b = 'two'} |
80 } {1 {near "WHERE": syntax error}} | 104 } {1 {near "WHERE": syntax error}} |
81 do_test indexedby-2.7 { | 105 do_test indexedby-2.7 { |
82 catchsql { SELECT * FROM v1 INDEXED BY i1 WHERE a = 'one' } | 106 catchsql { SELECT * FROM v1 INDEXED BY i1 WHERE a = 'one' } |
83 } {1 {no such index: i1}} | 107 } {1 {no such index: i1}} |
84 | 108 |
| 109 |
85 # Tests for single table cases. | 110 # Tests for single table cases. |
86 # | 111 # |
| 112 # EVIDENCE-OF: R-37002-28871 The "NOT INDEXED" clause specifies that no |
| 113 # index shall be used when accessing the preceding table, including |
| 114 # implied indices create by UNIQUE and PRIMARY KEY constraints. However, |
| 115 # the rowid can still be used to look up entries even when "NOT INDEXED" |
| 116 # is specified. |
| 117 # |
87 do_execsql_test indexedby-3.1 { | 118 do_execsql_test indexedby-3.1 { |
| 119 EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a = 'one' AND b = 'two' |
| 120 } {/SEARCH TABLE t1 USING INDEX/} |
| 121 do_execsql_test indexedby-3.1.1 { |
88 EXPLAIN QUERY PLAN SELECT * FROM t1 NOT INDEXED WHERE a = 'one' AND b = 'two' | 122 EXPLAIN QUERY PLAN SELECT * FROM t1 NOT INDEXED WHERE a = 'one' AND b = 'two' |
89 } {0 0 0 {SCAN TABLE t1}} | 123 } {0 0 0 {SCAN TABLE t1}} |
| 124 do_execsql_test indexedby-3.1.2 { |
| 125 EXPLAIN QUERY PLAN SELECT * FROM t1 NOT INDEXED WHERE rowid=1 |
| 126 } {/SEARCH TABLE t1 USING INTEGER PRIMARY KEY .rowid=/} |
| 127 |
| 128 |
90 do_execsql_test indexedby-3.2 { | 129 do_execsql_test indexedby-3.2 { |
91 EXPLAIN QUERY PLAN | 130 EXPLAIN QUERY PLAN |
92 SELECT * FROM t1 INDEXED BY i1 WHERE a = 'one' AND b = 'two' | 131 SELECT * FROM t1 INDEXED BY i1 WHERE a = 'one' AND b = 'two' |
93 } {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a=?)}} | 132 } {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a=?)}} |
94 do_execsql_test indexedby-3.3 { | 133 do_execsql_test indexedby-3.3 { |
95 EXPLAIN QUERY PLAN | 134 EXPLAIN QUERY PLAN |
96 SELECT * FROM t1 INDEXED BY i2 WHERE a = 'one' AND b = 'two' | 135 SELECT * FROM t1 INDEXED BY i2 WHERE a = 'one' AND b = 'two' |
97 } {0 0 0 {SEARCH TABLE t1 USING INDEX i2 (b=?)}} | 136 } {0 0 0 {SEARCH TABLE t1 USING INDEX i2 (b=?)}} |
98 do_test indexedby-3.4 { | 137 do_test indexedby-3.4 { |
99 catchsql { SELECT * FROM t1 INDEXED BY i2 WHERE a = 'one' } | 138 catchsql { SELECT * FROM t1 INDEXED BY i2 WHERE a = 'one' } |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 | 216 |
178 # Test that "NOT INDEXED" may use the rowid index, but not others. | 217 # Test that "NOT INDEXED" may use the rowid index, but not others. |
179 # | 218 # |
180 do_execsql_test indexedby-6.1 { | 219 do_execsql_test indexedby-6.1 { |
181 EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b = 10 ORDER BY rowid | 220 EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b = 10 ORDER BY rowid |
182 } {0 0 0 {SEARCH TABLE t1 USING INDEX i2 (b=?)}} | 221 } {0 0 0 {SEARCH TABLE t1 USING INDEX i2 (b=?)}} |
183 do_execsql_test indexedby-6.2 { | 222 do_execsql_test indexedby-6.2 { |
184 EXPLAIN QUERY PLAN SELECT * FROM t1 NOT INDEXED WHERE b = 10 ORDER BY rowid | 223 EXPLAIN QUERY PLAN SELECT * FROM t1 NOT INDEXED WHERE b = 10 ORDER BY rowid |
185 } {0 0 0 {SCAN TABLE t1}} | 224 } {0 0 0 {SCAN TABLE t1}} |
186 | 225 |
| 226 # EVIDENCE-OF: R-40297-14464 The INDEXED BY phrase forces the SQLite |
| 227 # query planner to use a particular named index on a DELETE, SELECT, or |
| 228 # UPDATE statement. |
| 229 # |
187 # Test that "INDEXED BY" can be used in a DELETE statement. | 230 # Test that "INDEXED BY" can be used in a DELETE statement. |
188 # | 231 # |
189 do_execsql_test indexedby-7.1 { | 232 do_execsql_test indexedby-7.1 { |
190 EXPLAIN QUERY PLAN DELETE FROM t1 WHERE a = 5 | 233 EXPLAIN QUERY PLAN DELETE FROM t1 WHERE a = 5 |
191 } {0 0 0 {SEARCH TABLE t1 USING COVERING INDEX i1 (a=?)}} | 234 } {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a=?)}} |
192 do_execsql_test indexedby-7.2 { | 235 do_execsql_test indexedby-7.2 { |
193 EXPLAIN QUERY PLAN DELETE FROM t1 NOT INDEXED WHERE a = 5 | 236 EXPLAIN QUERY PLAN DELETE FROM t1 NOT INDEXED WHERE a = 5 |
194 } {0 0 0 {SCAN TABLE t1}} | 237 } {0 0 0 {SCAN TABLE t1}} |
195 do_execsql_test indexedby-7.3 { | 238 do_execsql_test indexedby-7.3 { |
196 EXPLAIN QUERY PLAN DELETE FROM t1 INDEXED BY i1 WHERE a = 5 | 239 EXPLAIN QUERY PLAN DELETE FROM t1 INDEXED BY i1 WHERE a = 5 |
197 } {0 0 0 {SEARCH TABLE t1 USING COVERING INDEX i1 (a=?)}} | 240 } {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a=?)}} |
198 do_execsql_test indexedby-7.4 { | 241 do_execsql_test indexedby-7.4 { |
199 EXPLAIN QUERY PLAN DELETE FROM t1 INDEXED BY i1 WHERE a = 5 AND b = 10 | 242 EXPLAIN QUERY PLAN DELETE FROM t1 INDEXED BY i1 WHERE a = 5 AND b = 10 |
200 } {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a=?)}} | 243 } {0 0 0 {SEARCH TABLE t1 USING INDEX i1 (a=?)}} |
201 do_execsql_test indexedby-7.5 { | 244 do_execsql_test indexedby-7.5 { |
202 EXPLAIN QUERY PLAN DELETE FROM t1 INDEXED BY i2 WHERE a = 5 AND b = 10 | 245 EXPLAIN QUERY PLAN DELETE FROM t1 INDEXED BY i2 WHERE a = 5 AND b = 10 |
203 } {0 0 0 {SEARCH TABLE t1 USING INDEX i2 (b=?)}} | 246 } {0 0 0 {SEARCH TABLE t1 USING INDEX i2 (b=?)}} |
204 do_test indexedby-7.6 { | 247 do_test indexedby-7.6 { |
205 catchsql { DELETE FROM t1 INDEXED BY i2 WHERE a = 5} | 248 catchsql { DELETE FROM t1 INDEXED BY i2 WHERE a = 5} |
206 } {1 {no query solution}} | 249 } {1 {no query solution}} |
207 | 250 |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 SELECT a,b,c FROM x2 INDEXED BY x2i WHERE a=1 AND b=1 AND c='3'; | 358 SELECT a,b,c FROM x2 INDEXED BY x2i WHERE a=1 AND b=1 AND c='3'; |
316 } {1 1 3} | 359 } {1 1 3} |
317 do_execsql_test 11.9 { | 360 do_execsql_test 11.9 { |
318 SELECT a,b,c FROM x2 INDEXED BY x2i WHERE a=1 AND b=1 AND c='3.0'; | 361 SELECT a,b,c FROM x2 INDEXED BY x2i WHERE a=1 AND b=1 AND c='3.0'; |
319 } {1 1 3} | 362 } {1 1 3} |
320 do_eqp_test 11.10 { | 363 do_eqp_test 11.10 { |
321 SELECT a,b,c FROM x2 INDEXED BY x2i WHERE a=1 AND b=1 AND c='3.0'; | 364 SELECT a,b,c FROM x2 INDEXED BY x2i WHERE a=1 AND b=1 AND c='3.0'; |
322 } {0 0 0 {SEARCH TABLE x2 USING COVERING INDEX x2i (a=? AND b=? AND rowid=?)}} | 365 } {0 0 0 {SEARCH TABLE x2 USING COVERING INDEX x2i (a=? AND b=? AND rowid=?)}} |
323 | 366 |
324 finish_test | 367 finish_test |
OLD | NEW |