sqlite/test/scanstatus.test

235 lines
6.3 KiB
Plaintext
Raw Normal View History

# 2014 November 1
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
#
set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix scanstatus
do_execsql_test 1.0 {
CREATE TABLE t1(a, b);
CREATE TABLE t2(x, y);
INSERT INTO t1 VALUES(1, 2);
INSERT INTO t1 VALUES(3, 4);
INSERT INTO t2 VALUES('a', 'b');
INSERT INTO t2 VALUES('c', 'd');
INSERT INTO t2 VALUES('e', 'f');
}
proc do_scanstatus_test {tn res} {
set stmt [db_last_stmt_ptr db]
set idx 0
set ret [list]
while {1} {
set r [sqlite3_stmt_scanstatus $stmt $idx]
if {[llength $r]==0} break
lappend ret {*}$r
incr idx
}
uplevel [list do_test $tn [list set {} $ret] [list {*}$res]]
}
do_execsql_test 1.1 { SELECT count(*) FROM t1, t2; } 6
do_scanstatus_test 1.2 {
nLoop 1 nVisit 2 nEst 1048576 zName t1 zExplain {SCAN TABLE t1}
nLoop 2 nVisit 6 nEst 1048576 zName t2 zExplain {SCAN TABLE t2}
}
do_execsql_test 1.3 {
ANALYZE;
SELECT count(*) FROM t1, t2;
} 6
do_scanstatus_test 1.4 {
nLoop 1 nVisit 2 nEst 2 zName t1 zExplain {SCAN TABLE t1}
nLoop 2 nVisit 6 nEst 3 zName t2 zExplain {SCAN TABLE t2}
}
do_execsql_test 1.5 { ANALYZE }
do_execsql_test 1.6 {
SELECT count(*) FROM t1, t2 WHERE t2.rowid>1;
} 4
do_scanstatus_test 1.7 {
nLoop 1 nVisit 2 nEst 2 zName t2 zExplain
{SEARCH TABLE t2 USING INTEGER PRIMARY KEY (rowid>?)}
nLoop 2 nVisit 4 nEst 2 zName t1 zExplain {SCAN TABLE t1}
}
do_execsql_test 1.8 {
SELECT count(*) FROM t1, t2 WHERE t2.rowid>1;
} 4
do_scanstatus_test 1.9 {
nLoop 2 nVisit 4 nEst 2 zName t2 zExplain
{SEARCH TABLE t2 USING INTEGER PRIMARY KEY (rowid>?)}
nLoop 4 nVisit 8 nEst 2 zName t1 zExplain {SCAN TABLE t1}
}
do_test 1.9 {
sqlite3_stmt_scanstatus_reset [db_last_stmt_ptr db]
} {}
do_scanstatus_test 1.10 {
nLoop 0 nVisit 0 nEst 2 zName t2 zExplain
{SEARCH TABLE t2 USING INTEGER PRIMARY KEY (rowid>?)}
nLoop 0 nVisit 0 nEst 2 zName t1 zExplain {SCAN TABLE t1}
}
#-------------------------------------------------------------------------
# Try a few different types of scans.
#
reset_db
do_execsql_test 2.1 {
CREATE TABLE x1(i INTEGER PRIMARY KEY, j);
INSERT INTO x1 VALUES(1, 'one');
INSERT INTO x1 VALUES(2, 'two');
INSERT INTO x1 VALUES(3, 'three');
INSERT INTO x1 VALUES(4, 'four');
CREATE INDEX x1j ON x1(j);
SELECT * FROM x1 WHERE i=2;
} {2 two}
do_scanstatus_test 2.2 {
nLoop 1 nVisit 1 nEst 1 zName x1
zExplain {SEARCH TABLE x1 USING INTEGER PRIMARY KEY (rowid=?)}
}
do_execsql_test 2.3.1 {
SELECT * FROM x1 WHERE j='two'
} {2 two}
do_scanstatus_test 2.3.2 {
nLoop 1 nVisit 1 nEst 10 zName x1j
zExplain {SEARCH TABLE x1 USING COVERING INDEX x1j (j=?)}
}
do_execsql_test 2.4.1 {
SELECT * FROM x1 WHERE j<'two'
} {4 four 1 one 3 three}
do_scanstatus_test 2.4.2 {
nLoop 1 nVisit 3 nEst 262144 zName x1j
zExplain {SEARCH TABLE x1 USING COVERING INDEX x1j (j<?)}
}
do_execsql_test 2.5.1 {
SELECT * FROM x1 WHERE j>='two'
} {2 two}
do_scanstatus_test 2.5.2 {
nLoop 1 nVisit 1 nEst 262144 zName x1j
zExplain {SEARCH TABLE x1 USING COVERING INDEX x1j (j>?)}
}
do_execsql_test 2.6.1 {
SELECT * FROM x1 WHERE j BETWEEN 'three' AND 'two'
} {3 three 2 two}
do_scanstatus_test 2.6.2 {
nLoop 1 nVisit 2 nEst 16384 zName x1j
zExplain {SEARCH TABLE x1 USING COVERING INDEX x1j (j>? AND j<?)}
}
do_execsql_test 2.7.1 {
CREATE TABLE x2(i INTEGER, j, k);
INSERT INTO x2 SELECT i, j, i || ' ' || j FROM x1;
CREATE INDEX x2j ON x2(j);
CREATE INDEX x2ij ON x2(i, j);
SELECT * FROM x2 WHERE j BETWEEN 'three' AND 'two'
} {3 three {3 three} 2 two {2 two}}
do_scanstatus_test 2.7.2 {
nLoop 1 nVisit 2 nEst 16384 zName x2j
zExplain {SEARCH TABLE x2 USING INDEX x2j (j>? AND j<?)}
}
do_execsql_test 2.8.1 {
SELECT * FROM x2 WHERE i=1 AND j='two'
}
do_scanstatus_test 2.8.2 {
nLoop 1 nVisit 0 nEst 8 zName x2ij
zExplain {SEARCH TABLE x2 USING INDEX x2ij (i=? AND j=?)}
}
do_execsql_test 2.9.1 {
SELECT * FROM x2 WHERE i=5 AND j='two'
}
do_scanstatus_test 2.9.2 {
nLoop 1 nVisit 0 nEst 8 zName x2ij
zExplain {SEARCH TABLE x2 USING INDEX x2ij (i=? AND j=?)}
}
do_execsql_test 2.10.1 {
SELECT * FROM x2 WHERE i=3 AND j='three'
} {3 three {3 three}}
do_scanstatus_test 2.10.2 {
nLoop 1 nVisit 1 nEst 8 zName x2ij
zExplain {SEARCH TABLE x2 USING INDEX x2ij (i=? AND j=?)}
}
#-------------------------------------------------------------------------
# Try with queries that use the OR optimization.
#
do_execsql_test 3.1 {
CREATE TABLE a1(a, b, c, d);
CREATE INDEX a1a ON a1(a);
CREATE INDEX a1bc ON a1(b, c);
WITH d(x) AS (SELECT 1 UNION ALL SELECT x+1 AS n FROM d WHERE n<=100)
INSERT INTO a1 SELECT x, x, x, x FROM d;
}
do_execsql_test 3.2.1 {
SELECT d FROM a1 WHERE (a=4 OR b=13)
} {4 13}
do_scanstatus_test 3.2.2 {
nLoop 1 nVisit 1 nEst 10 zName a1a
zExplain {SEARCH TABLE a1 USING INDEX a1a (a=?)}
nLoop 1 nVisit 1 nEst 10 zName a1bc
zExplain {SEARCH TABLE a1 USING INDEX a1bc (b=?)}
}
do_execsql_test 3.2.1 {
SELECT count(*) FROM a1 WHERE (a BETWEEN 4 AND 12) OR (b BETWEEN 40 AND 60)
} {30}
do_scanstatus_test 3.2.2 {
nLoop 1 nVisit 9 nEst 16384 zName a1a
zExplain {SEARCH TABLE a1 USING INDEX a1a (a>? AND a<?)}
nLoop 1 nVisit 21 nEst 16384 zName a1bc
zExplain {SEARCH TABLE a1 USING INDEX a1bc (b>? AND b<?)}
}
do_execsql_test 3.3.1 {
SELECT count(*) FROM a1 AS x, a1 AS y
WHERE (x.a BETWEEN 4 AND 12) AND (y.b BETWEEN 1 AND 10)
} {90}
do_scanstatus_test 3.2.2 {
nLoop 1 nVisit 10 nEst 16384 zName a1bc
zExplain {SEARCH TABLE a1 AS y USING COVERING INDEX a1bc (b>? AND b<?)}
nLoop 10 nVisit 90 nEst 16384 zName a1a
zExplain {SEARCH TABLE a1 AS x USING COVERING INDEX a1a (a>? AND a<?)}
}
do_execsql_test 3.4.1 {
SELECT count(*) FROM a1 WHERE a IN (1, 5, 10, 15);
} {4}
do_scanstatus_test 3.4.2 {
nLoop 1 nVisit 4 nEst 40 zName a1a
zExplain {SEARCH TABLE a1 USING COVERING INDEX a1a (a=?)}
}
do_execsql_test 3.4.1 {
SELECT count(*) FROM a1 WHERE rowid IN (1, 5, 10, 15);
} {4}
do_scanstatus_test 3.4.2 {
nLoop 1 nVisit 4 nEst 4 zName a1
zExplain {SEARCH TABLE a1 USING INTEGER PRIMARY KEY (rowid=?)}
}
finish_test