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