sqlite
[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.
Brought by drakestapleton
sqlite / test / scanstatus.test
| 1 | # 2014 November 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 | # |
| 12 | |
| 13 | set testdir [file dirname $argv0] |
| 14 | source $testdir/tester.tcl |
| 15 | set testprefix scanstatus |
| 16 | |
| 17 | ifcapable !scanstatus { |
| 18 | finish_test |
| 19 | return |
| 20 | } |
| 21 | |
| 22 | do_execsql_test 1.0 { |
| 23 | CREATE TABLE t1(a, b); |
| 24 | CREATE TABLE t2(x, y); |
| 25 | INSERT INTO t1 VALUES(1, 2); |
| 26 | INSERT INTO t1 VALUES(3, 4); |
| 27 | INSERT INTO t2 VALUES('a', 'b'); |
| 28 | INSERT INTO t2 VALUES('c', 'd'); |
| 29 | INSERT INTO t2 VALUES('e', 'f'); |
| 30 | } |
| 31 | |
| 32 | proc do_scanstatus_test {tn res} { |
| 33 | set stmt [db version -last-stmt-ptr] |
| 34 | set idx 0 |
| 35 | set ret [list] |
| 36 | while {1} { |
| 37 | set r [sqlite3_stmt_scanstatus $stmt $idx] |
| 38 | if {[llength $r]==0} break |
| 39 | foreach v {nLoop nVisit nEst zName zExplain} { |
| 40 | lappend ret $v [dict get $r $v] |
| 41 | } |
| 42 | incr idx |
| 43 | } |
| 44 | |
| 45 | uplevel [list do_test $tn [list set {} $ret] [list {*}$res]] |
| 46 | } |
| 47 | |
| 48 | do_execsql_test 1.1a { SELECT count(*) FROM t1, t2; } 6 |
| 49 | do_scanstatus_test 1.1b { |
| 50 | nLoop 1 nVisit 2 nEst 1048576.0 zName t1 zExplain {SCAN t1} |
| 51 | nLoop 2 nVisit 6 nEst 1048576.0 zName t2 zExplain {SCAN t2} |
| 52 | } |
| 53 | |
| 54 | sqlite3_db_config db STMT_SCANSTATUS 0 |
| 55 | |
| 56 | do_execsql_test 1.2a { SELECT count(*) FROM t1, t2; } 6 |
| 57 | do_scanstatus_test 1.2b { |
| 58 | } |
| 59 | |
| 60 | sqlite3_db_config db STMT_SCANSTATUS 1 |
| 61 | |
| 62 | do_execsql_test 1.3 { |
| 63 | ANALYZE; |
| 64 | SELECT count(*) FROM t1, t2; |
| 65 | } 6 |
| 66 | do_scanstatus_test 1.4 { |
| 67 | nLoop 1 nVisit 2 nEst 2.0 zName t1 zExplain {SCAN t1} |
| 68 | nLoop 2 nVisit 6 nEst 3.0 zName t2 zExplain {SCAN t2} |
| 69 | } |
| 70 | |
| 71 | do_execsql_test 1.5 { ANALYZE } |
| 72 | do_execsql_test 1.6 { |
| 73 | SELECT count(*) FROM t1, t2 WHERE t2.rowid>1; |
| 74 | } 4 |
| 75 | do_scanstatus_test 1.7 { |
| 76 | nLoop 1 nVisit 2 nEst 2.0 zName t2 zExplain |
| 77 | {SEARCH t2 USING INTEGER PRIMARY KEY (rowid>?)} |
| 78 | nLoop 2 nVisit 4 nEst 2.0 zName t1 zExplain {SCAN t1} |
| 79 | } |
| 80 | |
| 81 | do_execsql_test 1.8 { |
| 82 | SELECT count(*) FROM t1, t2 WHERE t2.rowid>1; |
| 83 | } 4 |
| 84 | |
| 85 | do_scanstatus_test 1.9 { |
| 86 | nLoop 2 nVisit 4 nEst 2.0 zName t2 zExplain |
| 87 | {SEARCH t2 USING INTEGER PRIMARY KEY (rowid>?)} |
| 88 | nLoop 4 nVisit 8 nEst 2.0 zName t1 zExplain {SCAN t1} |
| 89 | } |
| 90 | |
| 91 | do_test 1.9 { |
| 92 | sqlite3_stmt_scanstatus_reset [db version -last-stmt-ptr] |
| 93 | } {} |
| 94 | |
| 95 | do_scanstatus_test 1.10 { |
| 96 | nLoop 0 nVisit 0 nEst 2.0 zName t2 zExplain |
| 97 | {SEARCH t2 USING INTEGER PRIMARY KEY (rowid>?)} |
| 98 | nLoop 0 nVisit 0 nEst 2.0 zName t1 zExplain {SCAN t1} |
| 99 | } |
| 100 | |
| 101 | #------------------------------------------------------------------------- |
| 102 | # Try a few different types of scans. |
| 103 | # |
| 104 | reset_db |
| 105 | sqlite3_db_config db STMT_SCANSTATUS 1 |
| 106 | do_execsql_test 2.1 { |
| 107 | CREATE TABLE x1(i INTEGER PRIMARY KEY, j); |
| 108 | INSERT INTO x1 VALUES(1, 'one'); |
| 109 | INSERT INTO x1 VALUES(2, 'two'); |
| 110 | INSERT INTO x1 VALUES(3, 'three'); |
| 111 | INSERT INTO x1 VALUES(4, 'four'); |
| 112 | CREATE INDEX x1j ON x1(j); |
| 113 | |
| 114 | SELECT * FROM x1 WHERE i=2; |
| 115 | } {2 two} |
| 116 | |
| 117 | do_scanstatus_test 2.2 { |
| 118 | nLoop 1 nVisit 1 nEst 1.0 zName x1 |
| 119 | zExplain {SEARCH x1 USING INTEGER PRIMARY KEY (rowid=?)} |
| 120 | } |
| 121 | |
| 122 | do_execsql_test 2.3.1 { |
| 123 | SELECT * FROM x1 WHERE j='two' |
| 124 | } {2 two} |
| 125 | do_scanstatus_test 2.3.2 { |
| 126 | nLoop 1 nVisit 1 nEst 10.0 zName x1j |
| 127 | zExplain {SEARCH x1 USING COVERING INDEX x1j (j=?)} |
| 128 | } |
| 129 | |
| 130 | do_execsql_test 2.4.1 { |
| 131 | SELECT * FROM x1 WHERE j<'two' |
| 132 | } {4 four 1 one 3 three} |
| 133 | do_scanstatus_test 2.4.2 { |
| 134 | nLoop 1 nVisit 3 nEst 262144.0 zName x1j |
| 135 | zExplain {SEARCH x1 USING COVERING INDEX x1j (j<?)} |
| 136 | } |
| 137 | |
| 138 | do_execsql_test 2.5.1 { |
| 139 | SELECT * FROM x1 WHERE j>='two' |
| 140 | } {2 two} |
| 141 | do_scanstatus_test 2.5.2 { |
| 142 | nLoop 1 nVisit 1 nEst 262144.0 zName x1j |
| 143 | zExplain {SEARCH x1 USING COVERING INDEX x1j (j>?)} |
| 144 | } |
| 145 | |
| 146 | do_execsql_test 2.6.1 { |
| 147 | SELECT * FROM x1 WHERE j BETWEEN 'three' AND 'two' |
| 148 | } {3 three 2 two} |
| 149 | do_scanstatus_test 2.6.2 { |
| 150 | nLoop 1 nVisit 2 nEst 16384.0 zName x1j |
| 151 | zExplain {SEARCH x1 USING COVERING INDEX x1j (j>? AND j<?)} |
| 152 | } |
| 153 | |
| 154 | do_execsql_test 2.7.1 { |
| 155 | CREATE TABLE x2(i INTEGER, j, k); |
| 156 | INSERT INTO x2 SELECT i, j, i || ' ' || j FROM x1; |
| 157 | CREATE INDEX x2j ON x2(j); |
| 158 | CREATE INDEX x2ij ON x2(i, j); |
| 159 | SELECT * FROM x2 WHERE j BETWEEN 'three' AND 'two' |
| 160 | } {3 three {3 three} 2 two {2 two}} |
| 161 | |
| 162 | do_scanstatus_test 2.7.2 { |
| 163 | nLoop 1 nVisit 2 nEst 16384.0 zName x2j |
| 164 | zExplain {SEARCH x2 USING INDEX x2j (j>? AND j<?)} |
| 165 | } |
| 166 | |
| 167 | do_execsql_test 2.8.1 { |
| 168 | SELECT * FROM x2 WHERE i=1 AND j='two' |
| 169 | } |
| 170 | do_scanstatus_test 2.8.2 { |
| 171 | nLoop 1 nVisit 0 nEst 9.0 zName x2ij |
| 172 | zExplain {SEARCH x2 USING INDEX x2ij (i=? AND j=?)} |
| 173 | } |
| 174 | |
| 175 | do_execsql_test 2.9.1 { |
| 176 | SELECT * FROM x2 WHERE i=5 AND j='two' |
| 177 | } |
| 178 | do_scanstatus_test 2.9.2 { |
| 179 | nLoop 1 nVisit 0 nEst 9.0 zName x2ij |
| 180 | zExplain {SEARCH x2 USING INDEX x2ij (i=? AND j=?)} |
| 181 | } |
| 182 | |
| 183 | do_execsql_test 2.10.1 { |
| 184 | SELECT * FROM x2 WHERE i=3 AND j='three' |
| 185 | } {3 three {3 three}} |
| 186 | do_scanstatus_test 2.10.2 { |
| 187 | nLoop 1 nVisit 1 nEst 9.0 zName x2ij |
| 188 | zExplain {SEARCH x2 USING INDEX x2ij (i=? AND j=?)} |
| 189 | } |
| 190 | |
| 191 | #------------------------------------------------------------------------- |
| 192 | # Try with queries that use the OR optimization. |
| 193 | # |
| 194 | do_execsql_test 3.1 { |
| 195 | CREATE TABLE a1(a, b, c, d); |
| 196 | CREATE INDEX a1a ON a1(a); |
| 197 | CREATE INDEX a1bc ON a1(b, c); |
| 198 | |
| 199 | WITH d(x) AS (SELECT 1 UNION ALL SELECT x+1 AS n FROM d WHERE n<=100) |
| 200 | INSERT INTO a1 SELECT x, x, x, x FROM d; |
| 201 | } |
| 202 | |
| 203 | do_execsql_test 3.2.1 { |
| 204 | SELECT d FROM a1 WHERE (a=4 OR b=13) |
| 205 | } {4 13} |
| 206 | do_scanstatus_test 3.2.2 { |
| 207 | nLoop 1 nVisit 1 nEst 10.0 zName a1a |
| 208 | zExplain {SEARCH a1 USING INDEX a1a (a=?)} |
| 209 | nLoop 1 nVisit 1 nEst 10.0 zName a1bc |
| 210 | zExplain {SEARCH a1 USING INDEX a1bc (b=?)} |
| 211 | } |
| 212 | |
| 213 | do_execsql_test 3.2.1 { |
| 214 | SELECT count(*) FROM a1 WHERE (a BETWEEN 4 AND 12) OR (b BETWEEN 40 AND 60) |
| 215 | } {30} |
| 216 | do_scanstatus_test 3.2.2 { |
| 217 | nLoop 1 nVisit 9 nEst 16384.0 zName a1a |
| 218 | zExplain {SEARCH a1 USING INDEX a1a (a>? AND a<?)} |
| 219 | nLoop 1 nVisit 21 nEst 16384.0 zName a1bc |
| 220 | zExplain {SEARCH a1 USING INDEX a1bc (b>? AND b<?)} |
| 221 | } |
| 222 | |
| 223 | do_execsql_test 3.3.1 { |
| 224 | SELECT count(*) FROM a1 AS x, a1 AS y |
| 225 | WHERE (x.a BETWEEN 4 AND 12) AND (y.b BETWEEN 1 AND 10) |
| 226 | } {90} |
| 227 | do_scanstatus_test 3.2.2 { |
| 228 | nLoop 1 nVisit 10 nEst 16384.0 zName a1bc |
| 229 | zExplain {SEARCH y USING COVERING INDEX a1bc (b>? AND b<?)} |
| 230 | nLoop 10 nVisit 90 nEst 16384.0 zName a1a |
| 231 | zExplain {SEARCH x USING COVERING INDEX a1a (a>? AND a<?)} |
| 232 | } |
| 233 | |
| 234 | do_execsql_test 3.4.1 { |
| 235 | SELECT count(*) FROM a1 WHERE a IN (1, 5, 10, 15); |
| 236 | } {4} |
| 237 | do_scanstatus_test 3.4.2 { |
| 238 | nLoop 1 nVisit 4 nEst 40.0 zName a1a |
| 239 | zExplain {SEARCH a1 USING COVERING INDEX a1a (a=?)} |
| 240 | } |
| 241 | |
| 242 | do_execsql_test 3.4.1 { |
| 243 | SELECT count(*) FROM a1 WHERE rowid IN (1, 5, 10, 15); |
| 244 | } {4} |
| 245 | do_scanstatus_test 3.4.2 { |
| 246 | nLoop 1 nVisit 4 nEst 4.0 zName a1 |
| 247 | zExplain {SEARCH a1 USING INTEGER PRIMARY KEY (rowid=?)} |
| 248 | } |
| 249 | |
| 250 | #------------------------------------------------------------------------- |
| 251 | # Test that scanstatus() data is not available for searches performed |
| 252 | # by triggers. |
| 253 | # |
| 254 | # It is available for searches performed as part of FK processing, but |
| 255 | # not FK action processing. |
| 256 | # |
| 257 | do_execsql_test 4.0 { |
| 258 | CREATE TABLE t1(a, b, c); |
| 259 | CREATE TABLE t2(x PRIMARY KEY, y, z); |
| 260 | CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN |
| 261 | SELECT * FROM t2 WHERE x BETWEEN 20 AND 40; |
| 262 | END; |
| 263 | WITH d(x) AS (SELECT 1 UNION ALL SELECT x+1 AS n FROM d WHERE n<=100) |
| 264 | INSERT INTO t2 SELECT x, x*2, x*3 FROM d; |
| 265 | } |
| 266 | |
| 267 | do_execsql_test 4.1.1 { INSERT INTO t1 VALUES(1, 2, 3); } |
| 268 | do_scanstatus_test 4.1.2 {} |
| 269 | |
| 270 | do_execsql_test 4.2 { |
| 271 | CREATE TABLE p1(x PRIMARY KEY); |
| 272 | INSERT INTO p1 VALUES(1), (2), (3), (4); |
| 273 | CREATE TABLE c1(y REFERENCES p1); |
| 274 | INSERT INTO c1 VALUES(1), (2), (3); |
| 275 | PRAGMA foreign_keys=on; |
| 276 | } |
| 277 | do_execsql_test 4.2.1 { DELETE FROM p1 WHERE x=4 } |
| 278 | do_scanstatus_test 4.2.2 { |
| 279 | nLoop 1 nVisit 1 nEst 1.0 zName sqlite_autoindex_p1_1 |
| 280 | zExplain {SEARCH p1 USING INDEX sqlite_autoindex_p1_1 (x=?)} |
| 281 | |
| 282 | nLoop 1 nVisit 3 nEst 1048576.0 zName c1 zExplain {SCAN c1} |
| 283 | } |
| 284 | |
| 285 | #------------------------------------------------------------------------- |
| 286 | # Further tests of different scan types. |
| 287 | # |
| 288 | reset_db |
| 289 | sqlite3_db_config db STMT_SCANSTATUS 1 |
| 290 | proc tochar {i} { |
| 291 | set alphabet {a b c d e f g h i j k l m n o p q r s t u v w x y z} |
| 292 | return [lindex $alphabet [expr $i % [llength $alphabet]]] |
| 293 | } |
| 294 | db func tochar tochar |
| 295 | do_execsql_test 5.0 { |
| 296 | CREATE TABLE t1(a PRIMARY KEY, b, c); |
| 297 | INSERT INTO t1 VALUES(0, 1, 'a'); |
| 298 | INSERT INTO t1 VALUES(1, 0, 'b'); |
| 299 | INSERT INTO t1 VALUES(2, 1, 'c'); |
| 300 | INSERT INTO t1 VALUES(3, 0, 'd'); |
| 301 | INSERT INTO t1 VALUES(4, 1, 'e'); |
| 302 | INSERT INTO t1 VALUES(5, 0, 'a'); |
| 303 | INSERT INTO t1 VALUES(6, 1, 'b'); |
| 304 | INSERT INTO t1 VALUES(7, 0, 'c'); |
| 305 | INSERT INTO t1 VALUES(8, 1, 'd'); |
| 306 | INSERT INTO t1 VALUES(9, 0, 'e'); |
| 307 | CREATE INDEX t1bc ON t1(b, c); |
| 308 | |
| 309 | CREATE TABLE t2(x, y); |
| 310 | CREATE INDEX t2xy ON t2(x, y); |
| 311 | WITH data(i, x, y) AS ( |
| 312 | SELECT 0, 0, tochar(0) |
| 313 | UNION ALL |
| 314 | SELECT i+1, (i+1)%2, tochar(i+1) FROM data WHERE i<500 |
| 315 | ) INSERT INTO t2 SELECT x, y FROM data; |
| 316 | |
| 317 | CREATE TABLE t3(x, y); |
| 318 | INSERT INTO t3 SELECT * FROM t2; |
| 319 | |
| 320 | ANALYZE; |
| 321 | } |
| 322 | |
| 323 | do_execsql_test 5.1.1 { |
| 324 | SELECT count(*) FROM t1 WHERE a IN (SELECT b FROM t1 AS ii) |
| 325 | } {2} |
| 326 | ifcapable stat4 { |
| 327 | do_scanstatus_test 5.1.2 { |
| 328 | nLoop 1 nVisit 10 nEst 10.0 zName t1 |
| 329 | zExplain {SCAN ii} |
| 330 | nLoop 1 nVisit 2 nEst 8.0 zName sqlite_autoindex_t1_1 |
| 331 | zExplain {SEARCH t1 USING COVERING INDEX sqlite_autoindex_t1_1 (a=?)} |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | do_execsql_test 5.2.1 { |
| 336 | SELECT count(*) FROM t1 WHERE a IN (0, 1) |
| 337 | } {2} |
| 338 | do_scanstatus_test 5.2.2 { |
| 339 | nLoop 1 nVisit 2 nEst 2.0 zName sqlite_autoindex_t1_1 |
| 340 | zExplain {SEARCH t1 USING COVERING INDEX sqlite_autoindex_t1_1 (a=?)} |
| 341 | } |
| 342 | |
| 343 | do_eqp_test 5.3.1 { |
| 344 | SELECT count(*) FROM t2 WHERE y = 'j'; |
| 345 | } {SEARCH t2 USING COVERING INDEX t2xy (ANY(x) AND y=?)} |
| 346 | do_execsql_test 5.3.2 { |
| 347 | SELECT count(*) FROM t2 WHERE y = 'j'; |
| 348 | } {19} |
| 349 | do_scanstatus_test 5.3.3 { |
| 350 | nLoop 1 nVisit 19 nEst 56.0 zName t2xy zExplain |
| 351 | {SEARCH t2 USING COVERING INDEX t2xy (ANY(x) AND y=?)} |
| 352 | } |
| 353 | |
| 354 | ifcapable stat4 { |
| 355 | do_eqp_test 5.4.1 { |
| 356 | SELECT count(*) FROM t1, t2 WHERE y = c; |
| 357 | } { |
| 358 | QUERY PLAN |
| 359 | |--SCAN t1 |
| 360 | `--SEARCH t2 USING COVERING INDEX t2xy (ANY(x) AND y=?) |
| 361 | } |
| 362 | do_execsql_test 5.4.2 { |
| 363 | SELECT count(*) FROM t1, t2 WHERE y = c; |
| 364 | } {200} |
| 365 | do_scanstatus_test 5.4.3 { |
| 366 | nLoop 1 nVisit 10 nEst 10.0 zName t1 |
| 367 | zExplain {SCAN t1} |
| 368 | nLoop 10 nVisit 200 nEst 56.0 zName t2xy |
| 369 | zExplain {SEARCH t2 USING COVERING INDEX t2xy (ANY(x) AND y=?)} |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | do_eqp_test 5.5.1 { |
| 374 | SELECT count(*) FROM t1, t3 WHERE y = c; |
| 375 | } { |
| 376 | QUERY PLAN |
| 377 | |--SCAN t3 |
| 378 | |--BLOOM FILTER ON t1 (c=?) |
| 379 | `--SEARCH t1 USING AUTOMATIC COVERING INDEX (c=?) |
| 380 | } |
| 381 | do_execsql_test 5.5.2 { |
| 382 | SELECT count(*) FROM t1, t3 WHERE y = c; |
| 383 | } {200} |
| 384 | do_scanstatus_test 5.5.3 { |
| 385 | nLoop 1 nVisit 501 nEst 480.0 zName t3 zExplain {SCAN t3} |
| 386 | nLoop 501 nVisit 200 nEst 20.0 zName auto-index zExplain |
| 387 | {SEARCH t1 USING AUTOMATIC COVERING INDEX (c=?)} |
| 388 | } |
| 389 | |
| 390 | #------------------------------------------------------------------------- |
| 391 | # Virtual table scans |
| 392 | # |
| 393 | ifcapable fts3 { |
| 394 | do_execsql_test 6.0 { |
| 395 | CREATE VIRTUAL TABLE ft1 USING fts4; |
| 396 | INSERT INTO ft1 VALUES('a d c f g h e i f c'); |
| 397 | INSERT INTO ft1 VALUES('g c h b g b f f f g'); |
| 398 | INSERT INTO ft1 VALUES('h h c c h f a e d d'); |
| 399 | INSERT INTO ft1 VALUES('e j i j i e b c f g'); |
| 400 | INSERT INTO ft1 VALUES('g f b g j c h a d f'); |
| 401 | INSERT INTO ft1 VALUES('j i a e g f a i a c'); |
| 402 | INSERT INTO ft1 VALUES('f d g g j j c a h g'); |
| 403 | INSERT INTO ft1 VALUES('b d h a d j j j b i'); |
| 404 | INSERT INTO ft1 VALUES('j e a b j e c b c i'); |
| 405 | INSERT INTO ft1 VALUES('a d e f b j j c g d'); |
| 406 | } |
| 407 | do_execsql_test 6.1.1 { |
| 408 | SELECT count(*) FROM ft1 WHERE ft1 MATCH 'd' |
| 409 | } {6} |
| 410 | do_scanstatus_test 6.1.2 { |
| 411 | nLoop 1 nVisit 6 nEst 24.0 zName ft1 zExplain |
| 412 | {SCAN ft1 VIRTUAL TABLE INDEX 3:} |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | |
| 417 | finish_test |