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
19 # The number_name procedure below converts its argment (an integer)
20 # into a string which is the English-language name for that number.
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
29 set tens
{{} ten twenty thirty forty fifty sixty seventy eighty ninety
}
30 proc number_name
{n
} {
32 set txt
"[number_name [expr {$n/1000}]] thousand"
33 set n
[expr {$n%1000}]
38 append txt
" [lindex $::ones [expr {$n/100}]] hundred"
42 append txt
" [lindex $::tens [expr {$n/10}]]"
46 append txt
" [lindex $::ones $n]"
48 set txt
[string trim
$txt]
49 if {$txt==""} {set txt zero
}
53 # Create a database schema.
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
71 for {set i
1} {$i<=50000} {incr i
} {
72 set r
[expr {int
(rand
()*500000)}]
73 set x
[number_name
$r]
75 puts "INSERT INTO t1 VALUES($i,$r,'$x');"
79 # 50000 INSERTs on an indexed table
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]');"
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]%';"
109 CREATE INDEX i1a ON t1
(a
);
110 CREATE INDEX i1b ON t1
(b
);
111 CREATE INDEX i1c ON t1
(c
);
115 # 5000 SELECTs on an integer comparison where the integer is
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';"
152 # 5000 updates of ranges where the field being compared is indexed.
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;"
162 # 50000 single-row updates. An index is used to find the row quickly.
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;"
171 # 1 big text update that touches every row in the table.
177 # Many individual text updates. Each row in the table is
178 # touched through an index.
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;"
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.
208 CREATE TABLE t1
(a INTEGER
, b INTEGER
, c TEXT
);
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.
220 SELECT rowid FROM t1 ORDER BY random
() LIMIT
20000;
223 # Delete 20000 random rows from the table.
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.
234 DELETE FROM t1 WHERE rowid IN
235 (SELECT rowid FROM t1 ORDER BY random
() LIMIT
20000);
237 puts {SELECT count
(*) FROM t1
;}