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 # This is a copy of speed1.test modified to user prepared statements.
16 # $Id: speed1p.explain,v 1.1 2008/04/16 12:57:48 drh Exp $
19 set testdir [file dirname $argv0]
20 source $testdir/tester.tcl
21 speed_trial_init speed1
23 # Set a uniform random seed
26 set sqlout [open speed1.txt w]
32 # The number_name procedure below converts its argment (an integer)
33 # into a string which is the English-language name for that number.
37 # puts [number_name 123] -> "one hundred twenty three"
39 set ones {zero one two three four five six seven eight nine
40 ten eleven twelve thirteen fourteen fifteen sixteen seventeen
42 set tens {{} ten twenty thirty forty fifty sixty seventy eighty ninety}
43 proc number_name {n} {
45 set txt "[number_name [expr {$n/1000}]] thousand"
46 set n [expr {$n%1000}]
51 append txt " [lindex $::ones [expr {$n/100}]] hundred"
55 append txt " [lindex $::tens [expr {$n/10}]]"
59 append txt " [lindex $::ones $n]"
61 set txt [string trim $txt]
62 if {$txt==""} {set txt zero}
66 # Create a database schema.
70 PRAGMA page_size=1024;
71 PRAGMA cache_size=8192;
72 PRAGMA locking_mode=EXCLUSIVE;
73 CREATE TABLE t1(a INTEGER, b INTEGER, c TEXT);
74 CREATE TABLE t2(a INTEGER, b INTEGER, c TEXT);
75 CREATE INDEX i2a ON t2(a);
76 CREATE INDEX i2b ON t2(b);
79 SELECT name FROM sqlite_master ORDER BY 1;
84 # 50000 INSERTs on an unindexed table
87 for {set i 1} {$i<=50000} {incr i} {
88 set r [expr {int(rand()*500000)}]
89 set x [number_name $r]
93 foreach {i r x} $::list {
94 db eval {INSERT INTO t1 VALUES($i,$r,$x)}
97 explain {INSERT INTO t1 VALUES($i,$r,$x)}
99 speed_trial_tcl speed1p-insert1 50000 row $script
102 # 50000 INSERTs on an indexed table
105 for {set i 1} {$i<=50000} {incr i} {
106 set r [expr {int(rand()*500000)}]
107 set x [number_name $r]
108 lappend list $i $r $x
111 foreach {i r x} $::list {
112 db eval {INSERT INTO t2 VALUES($i,$r,$x)}
115 explain {INSERT INTO t2 VALUES($i,$r,$x)}
117 speed_trial_tcl speed1p-insert2 50000 row $script
122 # 50 SELECTs on an integer comparison. There is no index so
123 # a full table scan is required.
126 for {set i 0} {$i<50} {incr i} {
127 set lwr [expr {$i*100}]
128 set upr [expr {($i+10)*100}]
129 lappend list $lwr $upr
132 foreach {lwr upr} $::list {
133 db eval {SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr}
136 explain {SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr}
138 speed_trial_tcl speed1p-select1 [expr {50*50000}] row $script
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} {
146 lappend list "%[number_name $i]%"
149 foreach pattern $::list {
150 db eval {SELECT count(*), avg(b) FROM t1 WHERE c LIKE $pattern}
153 explain {SELECT count(*), avg(b) FROM t1 WHERE c LIKE $pattern}
155 speed_trial_tcl speed1p-select2 [expr {50*50000}] row $script
160 explain {CREATE INDEX i1a ON t1(a)}
161 explain {CREATE INDEX i1b ON t1(b)}
163 speed_trial speed1p-createidx 150000 row {
164 CREATE INDEX i1a ON t1(a);
165 CREATE INDEX i1b ON t1(b);
166 CREATE INDEX i1c ON t1(c);
170 # 5000 SELECTs on an integer comparison where the integer is
174 for {set i 0} {$i<5000} {incr i} {
175 set lwr [expr {$i*100}]
176 set upr [expr {($i+10)*100}]
177 lappend list $lwr $upr
180 foreach {lwr upr} $::list {
181 db eval {SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr}
184 explain {SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr}
186 speed_trial_tcl speed1p-select3 5000 stmt $script
189 # 100000 random SELECTs against rowid.
192 for {set i 1} {$i<=100000} {incr i} {
193 set id [expr {int(rand()*50000)+1}]
198 db eval {SELECT c FROM t1 WHERE rowid=$id}
201 explain {SELECT c FROM t1 WHERE rowid=$id}
203 speed_trial_tcl speed1p-select4 100000 row $script
206 # 100000 random SELECTs against a unique indexed column.
209 for {set i 1} {$i<=100000} {incr i} {
210 set id [expr {int(rand()*50000)+1}]
215 db eval {SELECT c FROM t1 WHERE a=$id}
218 explain {SELECT c FROM t1 WHERE a=$id}
220 speed_trial_tcl speed1p-select5 100000 row $script
223 # 50000 random SELECTs against an indexed column text column
225 set list [db eval {SELECT c FROM t1 ORDER BY random() LIMIT 50000}]
228 db eval {SELECT c FROM t1 WHERE c=$c}
231 explain {SELECT c FROM t1 WHERE c=$c}
233 speed_trial_tcl speed1p-select6 50000 row $script
238 speed_trial speed1p-vacuum 100000 row VACUUM
240 # 5000 updates of ranges where the field being compared is indexed.
243 for {set i 0} {$i<5000} {incr i} {
244 set lwr [expr {$i*2}]
245 set upr [expr {($i+1)*2}]
246 lappend list $lwr $upr
249 foreach {lwr upr} $::list {
250 db eval {UPDATE t1 SET b=b*2 WHERE a>=$lwr AND a<$upr}
253 explain {UPDATE t1 SET b=b*2 WHERE a>=$lwr AND a<$upr}
255 speed_trial_tcl speed1p-update1 5000 stmt $script
258 # 50000 single-row updates. An index is used to find the row quickly.
261 for {set i 0} {$i<50000} {incr i} {
262 set r [expr {int(rand()*500000)}]
266 foreach {i r} $::list {
267 db eval {UPDATE t1 SET b=$r WHERE a=$i}
270 explain {UPDATE t1 SET b=$r WHERE a=$i}
272 speed_trial_tcl speed1p-update2 50000 row $script
275 # 1 big text update that touches every row in the table.
277 explain {UPDATE t1 SET c=a}
278 speed_trial speed1p-update3 50000 row {
282 # Many individual text updates. Each row in the table is
283 # touched through an index.
286 for {set i 1} {$i<=50000} {incr i} {
287 set r [expr {int(rand()*500000)}]
288 lappend list $i [number_name $r]
291 foreach {i x} $::list {
292 db eval {UPDATE t1 SET c=$x WHERE a=$i}
295 explain {UPDATE t1 SET c=$x WHERE a=$i}
297 speed_trial_tcl speed1p-update4 50000 row $script
300 # Delete all content in a table.
302 explain {DELETE FROM t1}
303 speed_trial speed1p-delete1 50000 row {DELETE FROM t1}
305 # Copy one table into another
307 explain {INSERT INTO t1 SELECT * FROM t2}
308 speed_trial speed1p-copy1 50000 row {INSERT INTO t1 SELECT * FROM t2}
310 # Delete all content in a table, one row at a time.
312 explain {DELETE FROM t1 WHERE 1}
313 speed_trial speed1p-delete2 50000 row {DELETE FROM t1 WHERE 1}
315 # Refill the table yet again
317 speed_trial speed1p-copy2 50000 row {INSERT INTO t1 SELECT * FROM t2}
319 # Drop the table and recreate it without its indices.
321 explain {DROP TABLE t1}
322 explain {CREATE TABLE tX(a INTEGER, b INTEGER, c TEXT)}
324 speed_trial speed1p-drop1 50000 row {
326 CREATE TABLE t1(a INTEGER, b INTEGER, c TEXT);
330 # Refill the table yet again. This copy should be faster because
331 # there are no indices to deal with.
333 speed_trial speed1p-copy3 50000 row {INSERT INTO t1 SELECT * FROM t2}
335 # Select 20000 rows from the table at random.
337 explain {SELECT rowid FROM t1 ORDER BY random() LIMIT 20000}
338 speed_trial speed1p-random1 50000 row {
339 SELECT rowid FROM t1 ORDER BY random() LIMIT 20000
342 # Delete 20000 random rows from the table.
344 explain {DELETE FROM t1 WHERE rowid IN
345 (SELECT rowid FROM t1 ORDER BY random() LIMIT 20000)}
346 speed_trial speed1p-random-del1 20000 row {
347 DELETE FROM t1 WHERE rowid IN
348 (SELECT rowid FROM t1 ORDER BY random() LIMIT 20000)
350 do_test speed1p-1.1 {
351 db one {SELECT count(*) FROM t1}
355 # Delete 20000 more rows at random from the table.
357 speed_trial speed1p-random-del2 20000 row {
358 DELETE FROM t1 WHERE rowid IN
359 (SELECT rowid FROM t1 ORDER BY random() LIMIT 20000)
361 do_test speed1p-1.2 {
362 db one {SELECT count(*) FROM t1}
364 speed_trial_summary speed1