Merge trunk changes into this branch.
[sqlite.git] / tool / mkspeedsql.tcl
blob04bafc04c1b99a73c7d88f1bb8e535d90f7e1e08
1 # 2008 October 9
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 generates SQL text used for performance testing.
13 # $Id: mkspeedsql.tcl,v 1.1 2008/10/09 17:57:34 drh Exp $
16 # Set a uniform random seed
17 expr srand(0)
19 # The number_name procedure below converts its argment (an integer)
20 # into a string which is the English-language name for that number.
22 # Example:
24 # puts [number_name 123] -> "one hundred twenty three"
26 set ones {zero one two three four five six seven eight nine
27 ten eleven twelve thirteen fourteen fifteen sixteen seventeen
28 eighteen nineteen}
29 set tens {{} ten twenty thirty forty fifty sixty seventy eighty ninety}
30 proc number_name {n} {
31 if {$n>=1000} {
32 set txt "[number_name [expr {$n/1000}]] thousand"
33 set n [expr {$n%1000}]
34 } else {
35 set txt {}
37 if {$n>=100} {
38 append txt " [lindex $::ones [expr {$n/100}]] hundred"
39 set n [expr {$n%100}]
41 if {$n>=20} {
42 append txt " [lindex $::tens [expr {$n/10}]]"
43 set n [expr {$n%10}]
45 if {$n>0} {
46 append txt " [lindex $::ones $n]"
48 set txt [string trim $txt]
49 if {$txt==""} {set txt zero}
50 return $txt
53 # Create a database schema.
55 puts {
56 PRAGMA page_size=1024;
57 PRAGMA cache_size=8192;
58 PRAGMA locking_mode=EXCLUSIVE;
59 CREATE TABLE t1(a INTEGER, b INTEGER, c TEXT);
60 CREATE TABLE t2(a INTEGER, b INTEGER, c TEXT);
61 CREATE INDEX i2a ON t2(a);
62 CREATE INDEX i2b ON t2(b);
63 SELECT name FROM sqlite_master ORDER BY 1;
67 # 50000 INSERTs on an unindexed table
69 set t1c_list {}
70 puts {BEGIN;}
71 for {set i 1} {$i<=50000} {incr i} {
72 set r [expr {int(rand()*500000)}]
73 set x [number_name $r]
74 lappend t1c_list $x
75 puts "INSERT INTO t1 VALUES($i,$r,'$x');"
77 puts {COMMIT;}
79 # 50000 INSERTs on an indexed table
81 puts {BEGIN;}
82 for {set i 1} {$i<=50000} {incr i} {
83 set r [expr {int(rand()*500000)}]
84 puts "INSERT INTO t2 VALUES($i,$r,'[number_name $r]');"
86 puts {COMMIT;}
89 # 50 SELECTs on an integer comparison. There is no index so
90 # a full table scan is required.
92 for {set i 0} {$i<50} {incr i} {
93 set lwr [expr {$i*100}]
94 set upr [expr {($i+10)*100}]
95 puts "SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr;"
98 # 50 SELECTs on an LIKE comparison. There is no index so a full
99 # table scan is required.
101 for {set i 0} {$i<50} {incr i} {
102 puts "SELECT count(*), avg(b) FROM t1 WHERE c LIKE '%[number_name $i]%';"
105 # Create indices
107 puts {BEGIN;}
108 puts {
109 CREATE INDEX i1a ON t1(a);
110 CREATE INDEX i1b ON t1(b);
111 CREATE INDEX i1c ON t1(c);
113 puts {COMMIT;}
115 # 5000 SELECTs on an integer comparison where the integer is
116 # indexed.
118 set sql {}
119 for {set i 0} {$i<5000} {incr i} {
120 set lwr [expr {$i*100}]
121 set upr [expr {($i+10)*100}]
122 puts "SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr;"
125 # 100000 random SELECTs against rowid.
127 for {set i 1} {$i<=100000} {incr i} {
128 set id [expr {int(rand()*50000)+1}]
129 puts "SELECT c FROM t1 WHERE rowid=$id;"
132 # 100000 random SELECTs against a unique indexed column.
134 for {set i 1} {$i<=100000} {incr i} {
135 set id [expr {int(rand()*50000)+1}]
136 puts "SELECT c FROM t1 WHERE a=$id;"
139 # 50000 random SELECTs against an indexed column text column
141 set nt1c [llength $t1c_list]
142 for {set i 0} {$i<50000} {incr i} {
143 set r [expr {int(rand()*$nt1c)}]
144 set c [lindex $t1c_list $i]
145 puts "SELECT c FROM t1 WHERE c='$c';"
149 # Vacuum
150 puts {VACUUM;}
152 # 5000 updates of ranges where the field being compared is indexed.
154 puts {BEGIN;}
155 for {set i 0} {$i<5000} {incr i} {
156 set lwr [expr {$i*2}]
157 set upr [expr {($i+1)*2}]
158 puts "UPDATE t1 SET b=b*2 WHERE a>=$lwr AND a<$upr;"
160 puts {COMMIT;}
162 # 50000 single-row updates. An index is used to find the row quickly.
164 puts {BEGIN;}
165 for {set i 0} {$i<50000} {incr i} {
166 set r [expr {int(rand()*500000)}]
167 puts "UPDATE t1 SET b=$r WHERE a=$i;"
169 puts {COMMIT;}
171 # 1 big text update that touches every row in the table.
173 puts {
174 UPDATE t1 SET c=a;
177 # Many individual text updates. Each row in the table is
178 # touched through an index.
180 puts {BEGIN;}
181 for {set i 1} {$i<=50000} {incr i} {
182 set r [expr {int(rand()*500000)}]
183 puts "UPDATE t1 SET c='[number_name $r]' WHERE a=$i;"
185 puts {COMMIT;}
187 # Delete all content in a table.
189 puts {DELETE FROM t1;}
191 # Copy one table into another
193 puts {INSERT INTO t1 SELECT * FROM t2;}
195 # Delete all content in a table, one row at a time.
197 puts {DELETE FROM t1 WHERE 1;}
199 # Refill the table yet again
201 puts {INSERT INTO t1 SELECT * FROM t2;}
203 # Drop the table and recreate it without its indices.
205 puts {BEGIN;}
206 puts {
207 DROP TABLE t1;
208 CREATE TABLE t1(a INTEGER, b INTEGER, c TEXT);
210 puts {COMMIT;}
212 # Refill the table yet again. This copy should be faster because
213 # there are no indices to deal with.
215 puts {INSERT INTO t1 SELECT * FROM t2;}
217 # Select 20000 rows from the table at random.
219 puts {
220 SELECT rowid FROM t1 ORDER BY random() LIMIT 20000;
223 # Delete 20000 random rows from the table.
225 puts {
226 DELETE FROM t1 WHERE rowid IN
227 (SELECT rowid FROM t1 ORDER BY random() LIMIT 20000);
229 puts {SELECT count(*) FROM t1;}
231 # Delete 20000 more rows at random from the table.
233 puts {
234 DELETE FROM t1 WHERE rowid IN
235 (SELECT rowid FROM t1 ORDER BY random() LIMIT 20000);
237 puts {SELECT count(*) FROM t1;}