xref: /sqlite-3.40.0/test/speed1p.test (revision 2abcd58f)
145c236d8Sdrh# 2008 March 21
245c236d8Sdrh#
345c236d8Sdrh# The author disclaims copyright to this source code.  In place of
445c236d8Sdrh# a legal notice, here is a blessing:
545c236d8Sdrh#
645c236d8Sdrh#    May you do good and not evil.
745c236d8Sdrh#    May you find forgiveness for yourself and forgive others.
845c236d8Sdrh#    May you share freely, never taking more than you give.
945c236d8Sdrh#
1045c236d8Sdrh#*************************************************************************
1145c236d8Sdrh# This file implements regression tests for SQLite library.  The
1245c236d8Sdrh# focus of this script is measuring executing speed.
1345c236d8Sdrh#
1445c236d8Sdrh# This is a copy of speed1.test modified to user prepared statements.
1545c236d8Sdrh#
16*2abcd58fSdrh# $Id: speed1p.test,v 1.3 2008/07/24 23:34:07 drh Exp $
1745c236d8Sdrh#
1845c236d8Sdrh
19*2abcd58fSdrhsqlite3_shutdown
20*2abcd58fSdrhsqlite3_config_scratch 29000 1
2145c236d8Sdrhset testdir [file dirname $argv0]
2245c236d8Sdrhsource $testdir/tester.tcl
2345c236d8Sdrhspeed_trial_init speed1
2445c236d8Sdrh
2545c236d8Sdrh# Set a uniform random seed
2645c236d8Sdrhexpr srand(0)
2745c236d8Sdrh
2845c236d8Sdrh# The number_name procedure below converts its argment (an integer)
2945c236d8Sdrh# into a string which is the English-language name for that number.
3045c236d8Sdrh#
3145c236d8Sdrh# Example:
3245c236d8Sdrh#
3345c236d8Sdrh#     puts [number_name 123]   ->  "one hundred twenty three"
3445c236d8Sdrh#
3545c236d8Sdrhset ones {zero one two three four five six seven eight nine
3645c236d8Sdrh          ten eleven twelve thirteen fourteen fifteen sixteen seventeen
3745c236d8Sdrh          eighteen nineteen}
3845c236d8Sdrhset tens {{} ten twenty thirty forty fifty sixty seventy eighty ninety}
3945c236d8Sdrhproc number_name {n} {
4045c236d8Sdrh  if {$n>=1000} {
4145c236d8Sdrh    set txt "[number_name [expr {$n/1000}]] thousand"
4245c236d8Sdrh    set n [expr {$n%1000}]
4345c236d8Sdrh  } else {
4445c236d8Sdrh    set txt {}
4545c236d8Sdrh  }
4645c236d8Sdrh  if {$n>=100} {
4745c236d8Sdrh    append txt " [lindex $::ones [expr {$n/100}]] hundred"
4845c236d8Sdrh    set n [expr {$n%100}]
4945c236d8Sdrh  }
5045c236d8Sdrh  if {$n>=20} {
5145c236d8Sdrh    append txt " [lindex $::tens [expr {$n/10}]]"
5245c236d8Sdrh    set n [expr {$n%10}]
5345c236d8Sdrh  }
5445c236d8Sdrh  if {$n>0} {
5545c236d8Sdrh    append txt " [lindex $::ones $n]"
5645c236d8Sdrh  }
5745c236d8Sdrh  set txt [string trim $txt]
5845c236d8Sdrh  if {$txt==""} {set txt zero}
5945c236d8Sdrh  return $txt
6045c236d8Sdrh}
6145c236d8Sdrh
6245c236d8Sdrh# Create a database schema.
6345c236d8Sdrh#
6445c236d8Sdrhdo_test speed1p-1.0 {
6545c236d8Sdrh  execsql {
6645c236d8Sdrh    PRAGMA page_size=1024;
6745c236d8Sdrh    PRAGMA cache_size=8192;
6845c236d8Sdrh    PRAGMA locking_mode=EXCLUSIVE;
6945c236d8Sdrh    CREATE TABLE t1(a INTEGER, b INTEGER, c TEXT);
7045c236d8Sdrh    CREATE TABLE t2(a INTEGER, b INTEGER, c TEXT);
7145c236d8Sdrh    CREATE INDEX i2a ON t2(a);
7245c236d8Sdrh    CREATE INDEX i2b ON t2(b);
7345c236d8Sdrh  }
7445c236d8Sdrh  execsql {
7545c236d8Sdrh    SELECT name FROM sqlite_master ORDER BY 1;
7645c236d8Sdrh  }
7745c236d8Sdrh} {i2a i2b t1 t2}
7845c236d8Sdrh
7945c236d8Sdrh
8045c236d8Sdrh# 50000 INSERTs on an unindexed table
8145c236d8Sdrh#
8245c236d8Sdrhset list {}
8345c236d8Sdrhfor {set i 1} {$i<=50000} {incr i} {
8445c236d8Sdrh  set r [expr {int(rand()*500000)}]
8545c236d8Sdrh  set x [number_name $r]
8645c236d8Sdrh  lappend list $i $r $x
8745c236d8Sdrh}
8845c236d8Sdrhset script {
8945c236d8Sdrh  foreach {i r x} $::list {
9045c236d8Sdrh    db eval {INSERT INTO t1 VALUES($i,$r,$x)}
9145c236d8Sdrh  }
9245c236d8Sdrh}
9345c236d8Sdrhdb eval BEGIN
9445c236d8Sdrhspeed_trial_tcl speed1p-insert1 50000 row $script
9545c236d8Sdrhdb eval COMMIT
9645c236d8Sdrh
9745c236d8Sdrh# 50000 INSERTs on an indexed table
9845c236d8Sdrh#
9945c236d8Sdrhset list {}
10045c236d8Sdrhfor {set i 1} {$i<=50000} {incr i} {
10145c236d8Sdrh  set r [expr {int(rand()*500000)}]
10245c236d8Sdrh  set x [number_name $r]
10345c236d8Sdrh  lappend list $i $r $x
10445c236d8Sdrh}
10545c236d8Sdrhset script {
10645c236d8Sdrh  foreach {i r x} $::list {
10745c236d8Sdrh    db eval {INSERT INTO t2 VALUES($i,$r,$x)}
10845c236d8Sdrh  }
10945c236d8Sdrh}
11045c236d8Sdrhdb eval BEGIN
11145c236d8Sdrhspeed_trial_tcl speed1p-insert2 50000 row $script
11245c236d8Sdrhdb eval COMMIT
11345c236d8Sdrh
11445c236d8Sdrh
11545c236d8Sdrh
11645c236d8Sdrh# 50 SELECTs on an integer comparison.  There is no index so
11745c236d8Sdrh# a full table scan is required.
11845c236d8Sdrh#
11945c236d8Sdrhset list {}
12045c236d8Sdrhfor {set i 0} {$i<50} {incr i} {
12145c236d8Sdrh  set lwr [expr {$i*100}]
12245c236d8Sdrh  set upr [expr {($i+10)*100}]
12345c236d8Sdrh  lappend list $lwr $upr
12445c236d8Sdrh}
12545c236d8Sdrhset script {
12645c236d8Sdrh  foreach {lwr upr} $::list {
12745c236d8Sdrh    db eval  {SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr}
12845c236d8Sdrh  }
12945c236d8Sdrh}
13045c236d8Sdrhdb eval BEGIN
13145c236d8Sdrhspeed_trial_tcl speed1p-select1 [expr {50*50000}] row $script
13245c236d8Sdrhdb eval COMMIT
13345c236d8Sdrh
13445c236d8Sdrh# 50 SELECTs on an LIKE comparison.  There is no index so a full
13545c236d8Sdrh# table scan is required.
13645c236d8Sdrh#
13745c236d8Sdrhset list {}
13845c236d8Sdrhfor {set i 0} {$i<50} {incr i} {
13945c236d8Sdrh  lappend list "%[number_name $i]%"
14045c236d8Sdrh}
14145c236d8Sdrhset script {
14245c236d8Sdrh  foreach pattern $::list {
14345c236d8Sdrh    db eval {SELECT count(*), avg(b) FROM t1 WHERE c LIKE $pattern}
14445c236d8Sdrh  }
14545c236d8Sdrh}
14645c236d8Sdrhdb eval BEGIN
14745c236d8Sdrhspeed_trial_tcl speed1p-select2 [expr {50*50000}] row $script
14845c236d8Sdrhdb eval COMMIT
14945c236d8Sdrh
15045c236d8Sdrh# Create indices
15145c236d8Sdrh#
15245c236d8Sdrhdb eval BEGIN
15345c236d8Sdrhspeed_trial speed1p-createidx 150000 row {
15445c236d8Sdrh  CREATE INDEX i1a ON t1(a);
15545c236d8Sdrh  CREATE INDEX i1b ON t1(b);
15645c236d8Sdrh  CREATE INDEX i1c ON t1(c);
15745c236d8Sdrh}
15845c236d8Sdrhdb eval COMMIT
15945c236d8Sdrh
16045c236d8Sdrh# 5000 SELECTs on an integer comparison where the integer is
16145c236d8Sdrh# indexed.
16245c236d8Sdrh#
16345c236d8Sdrhset list {}
16445c236d8Sdrhfor {set i 0} {$i<5000} {incr i} {
16545c236d8Sdrh  set lwr [expr {$i*100}]
16645c236d8Sdrh  set upr [expr {($i+10)*100}]
16745c236d8Sdrh  lappend list $lwr $upr
16845c236d8Sdrh}
16945c236d8Sdrhset script {
17045c236d8Sdrh  foreach {lwr upr} $::list {
17145c236d8Sdrh    db eval {SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr}
17245c236d8Sdrh  }
17345c236d8Sdrh}
17445c236d8Sdrhdb eval BEGIN
17545c236d8Sdrhspeed_trial_tcl speed1p-select3 5000 stmt $script
17645c236d8Sdrhdb eval COMMIT
17745c236d8Sdrh
17845c236d8Sdrh# 100000 random SELECTs against rowid.
17945c236d8Sdrh#
18045c236d8Sdrhset list {}
18145c236d8Sdrhfor {set i 1} {$i<=100000} {incr i} {
18245c236d8Sdrh  set id [expr {int(rand()*50000)+1}]
18345c236d8Sdrh  lappend list $id
18445c236d8Sdrh}
18545c236d8Sdrhset script {
18645c236d8Sdrh  foreach id $::list {
18745c236d8Sdrh    db eval {SELECT c FROM t1 WHERE rowid=$id}
18845c236d8Sdrh  }
18945c236d8Sdrh}
19045c236d8Sdrhdb eval BEGIN
19145c236d8Sdrhspeed_trial_tcl speed1p-select4 100000 row $script
19245c236d8Sdrhdb eval COMMIT
19345c236d8Sdrh
19445c236d8Sdrh# 100000 random SELECTs against a unique indexed column.
19545c236d8Sdrh#
19645c236d8Sdrhset list {}
19745c236d8Sdrhfor {set i 1} {$i<=100000} {incr i} {
19845c236d8Sdrh  set id [expr {int(rand()*50000)+1}]
19945c236d8Sdrh  lappend list $id
20045c236d8Sdrh}
20145c236d8Sdrhset script {
20245c236d8Sdrh  foreach id $::list {
20345c236d8Sdrh    db eval {SELECT c FROM t1 WHERE a=$id}
20445c236d8Sdrh  }
20545c236d8Sdrh}
20645c236d8Sdrhdb eval BEGIN
20745c236d8Sdrhspeed_trial_tcl speed1p-select5 100000 row $script
20845c236d8Sdrhdb eval COMMIT
20945c236d8Sdrh
21045c236d8Sdrh# 50000 random SELECTs against an indexed column text column
21145c236d8Sdrh#
21245c236d8Sdrhset list [db eval {SELECT c FROM t1 ORDER BY random() LIMIT 50000}]
21345c236d8Sdrhset script {
21445c236d8Sdrh  foreach c $::list {
21545c236d8Sdrh    db eval {SELECT c FROM t1 WHERE c=$c}
21645c236d8Sdrh  }
21745c236d8Sdrh}
21845c236d8Sdrhdb eval BEGIN
21945c236d8Sdrhspeed_trial_tcl speed1p-select6 50000 row $script
22045c236d8Sdrhdb eval COMMIT
22145c236d8Sdrh
22245c236d8Sdrh
22345c236d8Sdrh# Vacuum
22445c236d8Sdrhspeed_trial speed1p-vacuum 100000 row VACUUM
22545c236d8Sdrh
22645c236d8Sdrh# 5000 updates of ranges where the field being compared is indexed.
22745c236d8Sdrh#
22845c236d8Sdrhset list {}
22945c236d8Sdrhfor {set i 0} {$i<5000} {incr i} {
23045c236d8Sdrh  set lwr [expr {$i*2}]
23145c236d8Sdrh  set upr [expr {($i+1)*2}]
23245c236d8Sdrh  lappend list $lwr $upr
23345c236d8Sdrh}
23445c236d8Sdrhset script {
23545c236d8Sdrh  foreach {lwr upr} $::list {
23645c236d8Sdrh    db eval {UPDATE t1 SET b=b*2 WHERE a>=$lwr AND a<$upr}
23745c236d8Sdrh  }
23845c236d8Sdrh}
23945c236d8Sdrhdb eval BEGIN
24045c236d8Sdrhspeed_trial_tcl speed1p-update1 5000 stmt $script
24145c236d8Sdrhdb eval COMMIT
24245c236d8Sdrh
24345c236d8Sdrh# 50000 single-row updates.  An index is used to find the row quickly.
24445c236d8Sdrh#
24545c236d8Sdrhset list {}
24645c236d8Sdrhfor {set i 0} {$i<50000} {incr i} {
24745c236d8Sdrh  set r [expr {int(rand()*500000)}]
24845c236d8Sdrh  lappend list $i $r
24945c236d8Sdrh}
25045c236d8Sdrhset script {
25145c236d8Sdrh  foreach {i r} $::list {
25245c236d8Sdrh    db eval {UPDATE t1 SET b=$r WHERE a=$i}
25345c236d8Sdrh  }
25445c236d8Sdrh}
25545c236d8Sdrhdb eval BEGIN
25645c236d8Sdrhspeed_trial_tcl speed1p-update2 50000 row $script
25745c236d8Sdrhdb eval COMMIT
25845c236d8Sdrh
25945c236d8Sdrh# 1 big text update that touches every row in the table.
26045c236d8Sdrh#
26145c236d8Sdrhspeed_trial speed1p-update3 50000 row {
26245c236d8Sdrh  UPDATE t1 SET c=a;
26345c236d8Sdrh}
26445c236d8Sdrh
26545c236d8Sdrh# Many individual text updates.  Each row in the table is
26645c236d8Sdrh# touched through an index.
26745c236d8Sdrh#
26845c236d8Sdrhset list {}
26945c236d8Sdrhfor {set i 1} {$i<=50000} {incr i} {
27045c236d8Sdrh  set r [expr {int(rand()*500000)}]
27145c236d8Sdrh  lappend list $i [number_name $r]
27245c236d8Sdrh}
27345c236d8Sdrhset script {
27445c236d8Sdrh  foreach {i x} $::list {
27545c236d8Sdrh    db eval {UPDATE t1 SET c=$x WHERE a=$i}
27645c236d8Sdrh  }
27745c236d8Sdrh}
27845c236d8Sdrhdb eval BEGIN
27945c236d8Sdrhspeed_trial_tcl speed1p-update4 50000 row $script
28045c236d8Sdrhdb eval COMMIT
28145c236d8Sdrh
28245c236d8Sdrh# Delete all content in a table.
28345c236d8Sdrh#
28445c236d8Sdrhspeed_trial speed1p-delete1 50000 row {DELETE FROM t1}
28545c236d8Sdrh
28645c236d8Sdrh# Copy one table into another
28745c236d8Sdrh#
28845c236d8Sdrhspeed_trial speed1p-copy1 50000 row {INSERT INTO t1 SELECT * FROM t2}
28945c236d8Sdrh
29045c236d8Sdrh# Delete all content in a table, one row at a time.
29145c236d8Sdrh#
29245c236d8Sdrhspeed_trial speed1p-delete2 50000 row {DELETE FROM t1 WHERE 1}
29345c236d8Sdrh
29445c236d8Sdrh# Refill the table yet again
29545c236d8Sdrh#
29645c236d8Sdrhspeed_trial speed1p-copy2 50000 row {INSERT INTO t1 SELECT * FROM t2}
29745c236d8Sdrh
29845c236d8Sdrh# Drop the table and recreate it without its indices.
29945c236d8Sdrh#
30045c236d8Sdrhdb eval BEGIN
30145c236d8Sdrhspeed_trial speed1p-drop1 50000 row {
30245c236d8Sdrh   DROP TABLE t1;
30345c236d8Sdrh   CREATE TABLE t1(a INTEGER, b INTEGER, c TEXT);
30445c236d8Sdrh}
30545c236d8Sdrhdb eval COMMIT
30645c236d8Sdrh
30745c236d8Sdrh# Refill the table yet again.  This copy should be faster because
30845c236d8Sdrh# there are no indices to deal with.
30945c236d8Sdrh#
31045c236d8Sdrhspeed_trial speed1p-copy3 50000 row {INSERT INTO t1 SELECT * FROM t2}
31145c236d8Sdrh
31245c236d8Sdrh# Select 20000 rows from the table at random.
31345c236d8Sdrh#
31445c236d8Sdrhspeed_trial speed1p-random1 50000 row {
31545c236d8Sdrh  SELECT rowid FROM t1 ORDER BY random() LIMIT 20000
31645c236d8Sdrh}
31745c236d8Sdrh
31845c236d8Sdrh# Delete 20000 random rows from the table.
31945c236d8Sdrh#
32045c236d8Sdrhspeed_trial speed1p-random-del1 20000 row {
32145c236d8Sdrh  DELETE FROM t1 WHERE rowid IN
32245c236d8Sdrh    (SELECT rowid FROM t1 ORDER BY random() LIMIT 20000)
32345c236d8Sdrh}
32445c236d8Sdrhdo_test speed1p-1.1 {
32545c236d8Sdrh  db one {SELECT count(*) FROM t1}
32645c236d8Sdrh} 30000
32745c236d8Sdrh
32845c236d8Sdrh
32945c236d8Sdrh# Delete 20000 more rows at random from the table.
33045c236d8Sdrh#
33145c236d8Sdrhspeed_trial speed1p-random-del2 20000 row {
33245c236d8Sdrh  DELETE FROM t1 WHERE rowid IN
33345c236d8Sdrh    (SELECT rowid FROM t1 ORDER BY random() LIMIT 20000)
33445c236d8Sdrh}
33545c236d8Sdrhdo_test speed1p-1.2 {
33645c236d8Sdrh  db one {SELECT count(*) FROM t1}
33745c236d8Sdrh} 10000
33845c236d8Sdrhspeed_trial_summary speed1
33945c236d8Sdrh
34045c236d8Sdrhfinish_test
341