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