3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #*************************************************************************
11 # This file implements regression tests for SQLite library. The
12 # focus of this script is measuring executing speed.
14 # $Id: speed2.test,v 1.7 2007/04/16 15:02:20 drh Exp $
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
19 speed_trial_init speed2
21 # Set a uniform random seed
24 set sqlout [open speed2.txt w]
30 # The number_name procedure below converts its argment (an integer)
31 # into a string which is the English-language name for that number.
35 # puts [number_name 123] -> "one hundred twenty three"
37 set ones {zero one two three four five six seven eight nine
38 ten eleven twelve thirteen fourteen fifteen sixteen seventeen
40 set tens {{} ten twenty thirty forty fifty sixty seventy eighty ninety}
41 proc number_name {n} {
43 set txt "[number_name [expr {$n/1000}]] thousand"
44 set n [expr {$n%1000}]
49 append txt " [lindex $::ones [expr {$n/100}]] hundred"
53 append txt " [lindex $::tens [expr {$n/10}]]"
57 append txt " [lindex $::ones $n]"
59 set txt [string trim $txt]
60 if {$txt==""} {set txt zero}
64 # Create a database schema.
68 PRAGMA page_size=1024;
69 PRAGMA cache_size=8192;
70 PRAGMA locking_mode=EXCLUSIVE;
71 CREATE TABLE t1(a INTEGER, b INTEGER, c TEXT);
72 CREATE TABLE t2(a INTEGER, b INTEGER, c TEXT);
73 CREATE INDEX i2a ON t2(a);
74 CREATE INDEX i2b ON t2(b);
77 SELECT name FROM sqlite_master ORDER BY 1;
82 # 50000 INSERTs on an unindexed table
85 for {set i 1} {$i<=50000} {incr i} {
86 set r [expr {int(rand()*500000)}]
87 append sql "INSERT INTO t1 VALUES($i,$r,'[number_name $r]');\n"
90 speed_trial speed2-insert1 50000 row $sql
93 # 50000 INSERTs on an indexed table
96 for {set i 1} {$i<=50000} {incr i} {
97 set r [expr {int(rand()*500000)}]
98 append sql "INSERT INTO t2 VALUES($i,$r,'[number_name $r]');\n"
101 speed_trial speed2-insert2 50000 row $sql
106 # 50 SELECTs on an integer comparison. There is no index so
107 # a full table scan is required.
110 for {set i 0} {$i<50} {incr i} {
111 set lwr [expr {$i*100}]
112 set upr [expr {($i+10)*100}]
113 append sql "SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr;"
115 speed_trial speed2-select1a [expr {50*50000}] row $sql
117 # 50 SELECTs on an LIKE comparison. There is no index so a full
118 # table scan is required.
121 for {set i 0} {$i<50} {incr i} {
123 "SELECT count(*), avg(b) FROM t1 WHERE c LIKE '%[number_name $i]%';"
125 speed_trial speed2-select2a [expr {50*50000}] row $sql
128 speed_trial speed2-vacuum1 100000 row VACUUM
130 # 50 SELECTs on an integer comparison. There is no index so
131 # a full table scan is required.
134 for {set i 0} {$i<50} {incr i} {
135 set lwr [expr {$i*100}]
136 set upr [expr {($i+10)*100}]
137 append sql "SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr;"
139 speed_trial speed2-select1b [expr {50*50000}] row $sql
141 # 50 SELECTs on an LIKE comparison. There is no index so a full
142 # table scan is required.
145 for {set i 0} {$i<50} {incr i} {
147 "SELECT count(*), avg(b) FROM t1 WHERE c LIKE '%[number_name $i]%';"
149 speed_trial speed2-select2b [expr {50*50000}] row $sql
154 speed_trial speed2-createidx 150000 row {
155 CREATE INDEX i1a ON t1(a);
156 CREATE INDEX i1b ON t1(b);
157 CREATE INDEX i1c ON t1(c);
161 # 5000 SELECTs on an integer comparison where the integer is
165 for {set i 0} {$i<5000} {incr i} {
166 set lwr [expr {$i*100}]
167 set upr [expr {($i+10)*100}]
168 append sql "SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr;"
170 speed_trial speed2-select3a 5000 stmt $sql
172 # 100000 random SELECTs against rowid.
175 for {set i 1} {$i<=100000} {incr i} {
176 set id [expr {int(rand()*50000)+1}]
177 append sql "SELECT c=='hi' FROM t1 WHERE rowid=$id;\n"
179 speed_trial speed2-select4a 100000 row $sql
181 # 100000 random SELECTs against a unique indexed column.
184 for {set i 1} {$i<=100000} {incr i} {
185 set id [expr {int(rand()*50000)+1}]
186 append sql "SELECT c FROM t1 WHERE a=$id;"
188 speed_trial speed2-select5a 100000 row $sql
190 # 50000 random SELECTs against an indexed column text column
193 db eval {SELECT c FROM t1 ORDER BY random() LIMIT 50000} {
194 append sql "SELECT c FROM t1 WHERE c='$c';"
196 speed_trial speed2-select6a 50000 row $sql
199 speed_trial speed2-vacuum2 100000 row VACUUM
202 # 5000 SELECTs on an integer comparison where the integer is
206 for {set i 0} {$i<5000} {incr i} {
207 set lwr [expr {$i*100}]
208 set upr [expr {($i+10)*100}]
209 append sql "SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr;"
211 speed_trial speed2-select3b 5000 stmt $sql
213 # 100000 random SELECTs against rowid.
216 for {set i 1} {$i<=100000} {incr i} {
217 set id [expr {int(rand()*50000)+1}]
218 append sql "SELECT c=='hi' FROM t1 WHERE rowid=$id;\n"
220 speed_trial speed2-select4b 100000 row $sql
222 # 100000 random SELECTs against a unique indexed column.
225 for {set i 1} {$i<=100000} {incr i} {
226 set id [expr {int(rand()*50000)+1}]
227 append sql "SELECT c FROM t1 WHERE a=$id;"
229 speed_trial speed2-select5b 100000 row $sql
231 # 50000 random SELECTs against an indexed column text column
234 db eval {SELECT c FROM t1 ORDER BY random() LIMIT 50000} {
235 append sql "SELECT c FROM t1 WHERE c='$c';"
237 speed_trial speed2-select6b 50000 row $sql
239 # 5000 updates of ranges where the field being compared is indexed.
242 for {set i 0} {$i<5000} {incr i} {
243 set lwr [expr {$i*2}]
244 set upr [expr {($i+1)*2}]
245 append sql "UPDATE t1 SET b=b*2 WHERE a>=$lwr AND a<$upr;"
248 speed_trial speed2-update1 5000 stmt $sql
251 # 50000 single-row updates. An index is used to find the row quickly.
254 for {set i 0} {$i<50000} {incr i} {
255 set r [expr {int(rand()*500000)}]
256 append sql "UPDATE t1 SET b=$r WHERE a=$i;"
259 speed_trial speed2-update2 50000 row $sql
262 # 1 big text update that touches every row in the table.
264 speed_trial speed2-update3 50000 row {
268 # Many individual text updates. Each row in the table is
269 # touched through an index.
272 for {set i 1} {$i<=50000} {incr i} {
273 set r [expr {int(rand()*500000)}]
274 append sql "UPDATE t1 SET c='[number_name $r]' WHERE a=$i;"
277 speed_trial speed2-update4 50000 row $sql
280 # Delete all content in a table.
282 speed_trial speed2-delete1 50000 row {DELETE FROM t1}
284 # Copy one table into another
286 speed_trial speed2-copy1 50000 row {INSERT INTO t1 SELECT * FROM t2}
288 # Delete all content in a table, one row at a time.
290 speed_trial speed2-delete2 50000 row {DELETE FROM t1 WHERE 1}
292 # Refill the table yet again
294 speed_trial speed2-copy2 50000 row {INSERT INTO t1 SELECT * FROM t2}
296 # Drop the table and recreate it without its indices.
299 speed_trial speed2-drop1 50000 row {
301 CREATE TABLE t1(a INTEGER, b INTEGER, c TEXT);
305 # Refill the table yet again. This copy should be faster because
306 # there are no indices to deal with.
308 speed_trial speed2-copy3 50000 row {INSERT INTO t1 SELECT * FROM t2}
310 # Select 20000 rows from the table at random.
312 speed_trial speed2-random1 50000 row {
313 SELECT rowid FROM t1 ORDER BY random() LIMIT 20000
316 # Delete 20000 random rows from the table.
318 speed_trial speed2-random-del1 20000 row {
319 DELETE FROM t1 WHERE rowid IN
320 (SELECT rowid FROM t1 ORDER BY random() LIMIT 20000)
323 db one {SELECT count(*) FROM t1}
327 # Delete 20000 more rows at random from the table.
329 speed_trial speed2-random-del2 20000 row {
330 DELETE FROM t1 WHERE rowid IN
331 (SELECT rowid FROM t1 ORDER BY random() LIMIT 20000)
334 db one {SELECT count(*) FROM t1}
336 speed_trial_summary speed2