sqlite/test/scanstatus.test
dan 89e71646df Minor fixes and documentation improvements for sqlite3_stmt_scanstatus().
FossilOrigin-Name: 8d8cc9608d30bb65fffcfe488e904411cbbc7f41
2014-11-01 18:08:04 +00:00

202 lines
5.2 KiB
Plaintext

# 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 4 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 1 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 2 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 2.4 {
nLoop 1 nVisit 2 nEst 10 zName a1a
zExplain {SEARCH TABLE a1 USING INDEX a1a (a=?)}
nLoop 1 nVisit 2 nEst 10 zName a1bc
zExplain {SEARCH TABLE a1 USING INDEX a1bc (b=?)}
}
finish_test