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: speed1.test,v 1.11 2009/04/09 01:23:49 drh Exp $
19 #sqlite3_config_scratch 29000 1
20 set old_lookaside [sqlite3_config_lookaside 1000 300]
21 #sqlite3_config_pagecache 1024 10000
22 set testdir [file dirname $argv0]
23 source $testdir/tester.tcl
25 speed_trial_init speed1
27 # Set a uniform random seed
30 set sqlout [open speed1.txt w]
36 # The number_name procedure below converts its argment (an integer)
37 # into a string which is the English-language name for that number.
41 # puts [number_name 123] -> "one hundred twenty three"
43 set ones {zero one two three four five six seven eight nine
44 ten eleven twelve thirteen fourteen fifteen sixteen seventeen
46 set tens {{} ten twenty thirty forty fifty sixty seventy eighty ninety}
47 proc number_name {n} {
49 set txt "[number_name [expr {$n/1000}]] thousand"
50 set n [expr {$n%1000}]
55 append txt " [lindex $::ones [expr {$n/100}]] hundred"
59 append txt " [lindex $::tens [expr {$n/10}]]"
63 append txt " [lindex $::ones $n]"
65 set txt [string trim $txt]
66 if {$txt==""} {set txt zero}
70 # Create a database schema.
74 PRAGMA page_size=1024;
75 PRAGMA cache_size=8192;
76 PRAGMA locking_mode=EXCLUSIVE;
77 CREATE TABLE t1(a INTEGER, b INTEGER, c TEXT);
78 CREATE TABLE t2(a INTEGER, b INTEGER, c TEXT);
79 CREATE INDEX i2a ON t2(a);
80 CREATE INDEX i2b ON t2(b);
83 SELECT name FROM sqlite_master ORDER BY 1;
88 # 50000 INSERTs on an unindexed table
91 for {set i 1} {$i<=50000} {incr i} {
92 set r [expr {int(rand()*500000)}]
93 append sql "INSERT INTO t1 VALUES($i,$r,'[number_name $r]');\n"
96 speed_trial speed1-insert1 50000 row $sql
99 # 50000 INSERTs on an indexed table
102 for {set i 1} {$i<=50000} {incr i} {
103 set r [expr {int(rand()*500000)}]
104 append sql "INSERT INTO t2 VALUES($i,$r,'[number_name $r]');\n"
107 speed_trial speed1-insert2 50000 row $sql
112 # 50 SELECTs on an integer comparison. There is no index so
113 # a full table scan is required.
116 for {set i 0} {$i<50} {incr i} {
117 set lwr [expr {$i*100}]
118 set upr [expr {($i+10)*100}]
119 append sql "SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr;"
122 speed_trial speed1-select1 [expr {50*50000}] row $sql
125 # 50 SELECTs on an LIKE comparison. There is no index so a full
126 # table scan is required.
129 for {set i 0} {$i<50} {incr i} {
131 "SELECT count(*), avg(b) FROM t1 WHERE c LIKE '%[number_name $i]%';"
134 speed_trial speed1-select2 [expr {50*50000}] row $sql
140 speed_trial speed1-createidx 150000 row {
141 CREATE INDEX i1a ON t1(a);
142 CREATE INDEX i1b ON t1(b);
143 CREATE INDEX i1c ON t1(c);
147 # 5000 SELECTs on an integer comparison where the integer is
151 for {set i 0} {$i<5000} {incr i} {
152 set lwr [expr {$i*100}]
153 set upr [expr {($i+10)*100}]
154 append sql "SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr;"
157 speed_trial speed1-select3 5000 stmt $sql
160 # 100000 random SELECTs against rowid.
163 for {set i 1} {$i<=100000} {incr i} {
164 set id [expr {int(rand()*50000)+1}]
165 append sql "SELECT c FROM t1 WHERE rowid=$id;"
168 speed_trial speed1-select4 100000 row $sql
171 # 100000 random SELECTs against a unique indexed column.
174 for {set i 1} {$i<=100000} {incr i} {
175 set id [expr {int(rand()*50000)+1}]
176 append sql "SELECT c FROM t1 WHERE a=$id;"
179 speed_trial speed1-select5 100000 row $sql
182 # 50000 random SELECTs against an indexed column text column
185 db eval {SELECT c FROM t1 ORDER BY random() LIMIT 50000} {
186 append sql "SELECT c FROM t1 WHERE c='$c';"
189 speed_trial speed1-select6 50000 row $sql
194 speed_trial speed1-vacuum 100000 row VACUUM
196 # 5000 updates of ranges where the field being compared is indexed.
199 for {set i 0} {$i<5000} {incr i} {
200 set lwr [expr {$i*2}]
201 set upr [expr {($i+1)*2}]
202 append sql "UPDATE t1 SET b=b*2 WHERE a>=$lwr AND a<$upr;"
205 speed_trial speed1-update1 5000 stmt $sql
208 # 50000 single-row updates. An index is used to find the row quickly.
211 for {set i 0} {$i<50000} {incr i} {
212 set r [expr {int(rand()*500000)}]
213 append sql "UPDATE t1 SET b=$r WHERE a=$i;"
216 speed_trial speed1-update2 50000 row $sql
219 # 1 big text update that touches every row in the table.
221 speed_trial speed1-update3 50000 row {
225 # Many individual text updates. Each row in the table is
226 # touched through an index.
229 for {set i 1} {$i<=50000} {incr i} {
230 set r [expr {int(rand()*500000)}]
231 append sql "UPDATE t1 SET c='[number_name $r]' WHERE a=$i;"
234 speed_trial speed1-update4 50000 row $sql
237 # Delete all content in a table.
239 speed_trial speed1-delete1 50000 row {DELETE FROM t1}
241 # Copy one table into another
243 speed_trial speed1-copy1 50000 row {INSERT INTO t1 SELECT * FROM t2}
245 # Delete all content in a table, one row at a time.
247 speed_trial speed1-delete2 50000 row {DELETE FROM t1 WHERE 1}
249 # Refill the table yet again
251 speed_trial speed1-copy2 50000 row {INSERT INTO t1 SELECT * FROM t2}
253 # Drop the table and recreate it without its indices.
256 speed_trial speed1-drop1 50000 row {
258 CREATE TABLE t1(a INTEGER, b INTEGER, c TEXT);
262 # Refill the table yet again. This copy should be faster because
263 # there are no indices to deal with.
265 speed_trial speed1-copy3 50000 row {INSERT INTO t1 SELECT * FROM t2}
267 # Select 20000 rows from the table at random.
269 speed_trial speed1-random1 50000 row {
270 SELECT rowid FROM t1 ORDER BY random() LIMIT 20000
273 # Delete 20000 random rows from the table.
275 speed_trial speed1-random-del1 20000 row {
276 DELETE FROM t1 WHERE rowid IN
277 (SELECT rowid FROM t1 ORDER BY random() LIMIT 20000)
280 db one {SELECT count(*) FROM t1}
284 # Delete 20000 more rows at random from the table.
286 speed_trial speed1-random-del2 20000 row {
287 DELETE FROM t1 WHERE rowid IN
288 (SELECT rowid FROM t1 ORDER BY random() LIMIT 20000)
291 db one {SELECT count(*) FROM t1}
293 speed_trial_summary speed1
297 eval sqlite3_config_lookaside $old_lookaside
299 autoinstall_test_functions