xref: /sqlite-3.40.0/test/speed1.test (revision bb77b753)
1abcddedbSdrh# 2006 November 23
2abcddedbSdrh#
3abcddedbSdrh# The author disclaims copyright to this source code.  In place of
4abcddedbSdrh# a legal notice, here is a blessing:
5abcddedbSdrh#
6abcddedbSdrh#    May you do good and not evil.
7abcddedbSdrh#    May you find forgiveness for yourself and forgive others.
8abcddedbSdrh#    May you share freely, never taking more than you give.
9abcddedbSdrh#
10abcddedbSdrh#*************************************************************************
11abcddedbSdrh# This file implements regression tests for SQLite library.  The
12abcddedbSdrh# focus of this script is measuring executing speed.
13abcddedbSdrh#
14*bb77b753Sdrh# $Id: speed1.test,v 1.11 2009/04/09 01:23:49 drh Exp $
15abcddedbSdrh#
16abcddedbSdrh
172abcd58fSdrhsqlite3_shutdown
18d63bd759Sdrh#sqlite3_config_scratch 29000 1
19ab7bee89Sdanielk1977set old_lookaside [sqlite3_config_lookaside 1000 300]
20d63bd759Sdrh#sqlite3_config_pagecache 1024 10000
21abcddedbSdrhset testdir [file dirname $argv0]
22abcddedbSdrhsource $testdir/tester.tcl
23dd92431aSdrhspeed_trial_init speed1
24abcddedbSdrh
2545068f4bSdrh# Set a uniform random seed
2645068f4bSdrhexpr srand(0)
2745068f4bSdrh
28741f7063Sdrhset sqlout [open speed1.txt w]
29741f7063Sdrhproc tracesql {sql} {
30741f7063Sdrh  puts $::sqlout $sql\;
31741f7063Sdrh}
32e4dd73b4Sdrh#db trace tracesql
33741f7063Sdrh
34abcddedbSdrh# The number_name procedure below converts its argment (an integer)
35abcddedbSdrh# into a string which is the English-language name for that number.
36abcddedbSdrh#
37abcddedbSdrh# Example:
38abcddedbSdrh#
39abcddedbSdrh#     puts [number_name 123]   ->  "one hundred twenty three"
40abcddedbSdrh#
41abcddedbSdrhset ones {zero one two three four five six seven eight nine
42abcddedbSdrh          ten eleven twelve thirteen fourteen fifteen sixteen seventeen
43abcddedbSdrh          eighteen nineteen}
44abcddedbSdrhset tens {{} ten twenty thirty forty fifty sixty seventy eighty ninety}
45abcddedbSdrhproc number_name {n} {
46abcddedbSdrh  if {$n>=1000} {
47abcddedbSdrh    set txt "[number_name [expr {$n/1000}]] thousand"
48abcddedbSdrh    set n [expr {$n%1000}]
49abcddedbSdrh  } else {
50abcddedbSdrh    set txt {}
51abcddedbSdrh  }
52abcddedbSdrh  if {$n>=100} {
53abcddedbSdrh    append txt " [lindex $::ones [expr {$n/100}]] hundred"
54abcddedbSdrh    set n [expr {$n%100}]
55abcddedbSdrh  }
56abcddedbSdrh  if {$n>=20} {
57abcddedbSdrh    append txt " [lindex $::tens [expr {$n/10}]]"
58abcddedbSdrh    set n [expr {$n%10}]
59abcddedbSdrh  }
60abcddedbSdrh  if {$n>0} {
61abcddedbSdrh    append txt " [lindex $::ones $n]"
62abcddedbSdrh  }
63abcddedbSdrh  set txt [string trim $txt]
64abcddedbSdrh  if {$txt==""} {set txt zero}
65abcddedbSdrh  return $txt
66abcddedbSdrh}
67abcddedbSdrh
68abcddedbSdrh# Create a database schema.
69abcddedbSdrh#
70abcddedbSdrhdo_test speed1-1.0 {
71abcddedbSdrh  execsql {
72e4dd73b4Sdrh    PRAGMA page_size=1024;
73e4dd73b4Sdrh    PRAGMA cache_size=8192;
74e4dd73b4Sdrh    PRAGMA locking_mode=EXCLUSIVE;
75abcddedbSdrh    CREATE TABLE t1(a INTEGER, b INTEGER, c TEXT);
76abcddedbSdrh    CREATE TABLE t2(a INTEGER, b INTEGER, c TEXT);
77abcddedbSdrh    CREATE INDEX i2a ON t2(a);
78abcddedbSdrh    CREATE INDEX i2b ON t2(b);
79e4dd73b4Sdrh  }
80e4dd73b4Sdrh  execsql {
81abcddedbSdrh    SELECT name FROM sqlite_master ORDER BY 1;
82abcddedbSdrh  }
83abcddedbSdrh} {i2a i2b t1 t2}
84abcddedbSdrh
85abcddedbSdrh
86abcddedbSdrh# 50000 INSERTs on an unindexed table
87abcddedbSdrh#
88abcddedbSdrhset sql {}
89abcddedbSdrhfor {set i 1} {$i<=50000} {incr i} {
90abcddedbSdrh  set r [expr {int(rand()*500000)}]
91abcddedbSdrh  append sql "INSERT INTO t1 VALUES($i,$r,'[number_name $r]');\n"
92abcddedbSdrh}
93abcddedbSdrhdb eval BEGIN
94abcddedbSdrhspeed_trial speed1-insert1 50000 row $sql
95abcddedbSdrhdb eval COMMIT
96abcddedbSdrh
97abcddedbSdrh# 50000 INSERTs on an indexed table
98abcddedbSdrh#
99abcddedbSdrhset sql {}
100abcddedbSdrhfor {set i 1} {$i<=50000} {incr i} {
101abcddedbSdrh  set r [expr {int(rand()*500000)}]
102abcddedbSdrh  append sql "INSERT INTO t2 VALUES($i,$r,'[number_name $r]');\n"
103abcddedbSdrh}
104abcddedbSdrhdb eval BEGIN
105abcddedbSdrhspeed_trial speed1-insert2 50000 row $sql
106abcddedbSdrhdb eval COMMIT
107abcddedbSdrh
108abcddedbSdrh
109abcddedbSdrh
110abcddedbSdrh# 50 SELECTs on an integer comparison.  There is no index so
111abcddedbSdrh# a full table scan is required.
112abcddedbSdrh#
113abcddedbSdrhset sql {}
114abcddedbSdrhfor {set i 0} {$i<50} {incr i} {
115abcddedbSdrh  set lwr [expr {$i*100}]
116abcddedbSdrh  set upr [expr {($i+10)*100}]
117abcddedbSdrh  append sql "SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr;"
118abcddedbSdrh}
119abcddedbSdrhdb eval BEGIN
120abcddedbSdrhspeed_trial speed1-select1 [expr {50*50000}] row $sql
121abcddedbSdrhdb eval COMMIT
122abcddedbSdrh
123abcddedbSdrh# 50 SELECTs on an LIKE comparison.  There is no index so a full
124abcddedbSdrh# table scan is required.
125abcddedbSdrh#
126abcddedbSdrhset sql {}
127abcddedbSdrhfor {set i 0} {$i<50} {incr i} {
128abcddedbSdrh  append sql \
129abcddedbSdrh    "SELECT count(*), avg(b) FROM t1 WHERE c LIKE '%[number_name $i]%';"
130abcddedbSdrh}
131abcddedbSdrhdb eval BEGIN
132abcddedbSdrhspeed_trial speed1-select2 [expr {50*50000}] row $sql
133abcddedbSdrhdb eval COMMIT
134abcddedbSdrh
135abcddedbSdrh# Create indices
136abcddedbSdrh#
137abcddedbSdrhdb eval BEGIN
138abcddedbSdrhspeed_trial speed1-createidx 150000 row {
139abcddedbSdrh  CREATE INDEX i1a ON t1(a);
140abcddedbSdrh  CREATE INDEX i1b ON t1(b);
141abcddedbSdrh  CREATE INDEX i1c ON t1(c);
142abcddedbSdrh}
143abcddedbSdrhdb eval COMMIT
144abcddedbSdrh
145abcddedbSdrh# 5000 SELECTs on an integer comparison where the integer is
146abcddedbSdrh# indexed.
147abcddedbSdrh#
148abcddedbSdrhset sql {}
149abcddedbSdrhfor {set i 0} {$i<5000} {incr i} {
150abcddedbSdrh  set lwr [expr {$i*100}]
151abcddedbSdrh  set upr [expr {($i+10)*100}]
152abcddedbSdrh  append sql "SELECT count(*), avg(b) FROM t1 WHERE b>=$lwr AND b<$upr;"
153abcddedbSdrh}
154abcddedbSdrhdb eval BEGIN
155abcddedbSdrhspeed_trial speed1-select3 5000 stmt $sql
156abcddedbSdrhdb eval COMMIT
157abcddedbSdrh
158741f7063Sdrh# 100000 random SELECTs against rowid.
159abcddedbSdrh#
160abcddedbSdrhset sql {}
161741f7063Sdrhfor {set i 1} {$i<=100000} {incr i} {
162abcddedbSdrh  set id [expr {int(rand()*50000)+1}]
163abcddedbSdrh  append sql "SELECT c FROM t1 WHERE rowid=$id;"
164abcddedbSdrh}
165abcddedbSdrhdb eval BEGIN
166741f7063Sdrhspeed_trial speed1-select4 100000 row $sql
167abcddedbSdrhdb eval COMMIT
168abcddedbSdrh
169741f7063Sdrh# 100000 random SELECTs against a unique indexed column.
170abcddedbSdrh#
171abcddedbSdrhset sql {}
172741f7063Sdrhfor {set i 1} {$i<=100000} {incr i} {
173abcddedbSdrh  set id [expr {int(rand()*50000)+1}]
174abcddedbSdrh  append sql "SELECT c FROM t1 WHERE a=$id;"
175abcddedbSdrh}
176abcddedbSdrhdb eval BEGIN
177741f7063Sdrhspeed_trial speed1-select5 100000 row $sql
178abcddedbSdrhdb eval COMMIT
179abcddedbSdrh
180741f7063Sdrh# 50000 random SELECTs against an indexed column text column
181abcddedbSdrh#
182abcddedbSdrhset sql {}
183741f7063Sdrhdb eval {SELECT c FROM t1 ORDER BY random() LIMIT 50000} {
184abcddedbSdrh  append sql "SELECT c FROM t1 WHERE c='$c';"
185abcddedbSdrh}
186abcddedbSdrhdb eval BEGIN
187741f7063Sdrhspeed_trial speed1-select6 50000 row $sql
188abcddedbSdrhdb eval COMMIT
189abcddedbSdrh
190abcddedbSdrh
191abcddedbSdrh# Vacuum
192abcddedbSdrhspeed_trial speed1-vacuum 100000 row VACUUM
193abcddedbSdrh
194abcddedbSdrh# 5000 updates of ranges where the field being compared is indexed.
195abcddedbSdrh#
196abcddedbSdrhset sql {}
197abcddedbSdrhfor {set i 0} {$i<5000} {incr i} {
198abcddedbSdrh  set lwr [expr {$i*2}]
199abcddedbSdrh  set upr [expr {($i+1)*2}]
200abcddedbSdrh  append sql "UPDATE t1 SET b=b*2 WHERE a>=$lwr AND a<$upr;"
201abcddedbSdrh}
202abcddedbSdrhdb eval BEGIN
203abcddedbSdrhspeed_trial speed1-update1 5000 stmt $sql
204abcddedbSdrhdb eval COMMIT
205abcddedbSdrh
206abcddedbSdrh# 50000 single-row updates.  An index is used to find the row quickly.
207abcddedbSdrh#
208abcddedbSdrhset sql {}
209abcddedbSdrhfor {set i 0} {$i<50000} {incr i} {
210abcddedbSdrh  set r [expr {int(rand()*500000)}]
211abcddedbSdrh  append sql "UPDATE t1 SET b=$r WHERE a=$i;"
212abcddedbSdrh}
213abcddedbSdrhdb eval BEGIN
214abcddedbSdrhspeed_trial speed1-update2 50000 row $sql
215abcddedbSdrhdb eval COMMIT
216abcddedbSdrh
217abcddedbSdrh# 1 big text update that touches every row in the table.
218abcddedbSdrh#
219abcddedbSdrhspeed_trial speed1-update3 50000 row {
220abcddedbSdrh  UPDATE t1 SET c=a;
221abcddedbSdrh}
222abcddedbSdrh
223abcddedbSdrh# Many individual text updates.  Each row in the table is
224abcddedbSdrh# touched through an index.
225abcddedbSdrh#
226abcddedbSdrhset sql {}
227abcddedbSdrhfor {set i 1} {$i<=50000} {incr i} {
228abcddedbSdrh  set r [expr {int(rand()*500000)}]
229abcddedbSdrh  append sql "UPDATE t1 SET c='[number_name $r]' WHERE a=$i;"
230abcddedbSdrh}
231abcddedbSdrhdb eval BEGIN
232abcddedbSdrhspeed_trial speed1-update4 50000 row $sql
233abcddedbSdrhdb eval COMMIT
234abcddedbSdrh
235abcddedbSdrh# Delete all content in a table.
236abcddedbSdrh#
237abcddedbSdrhspeed_trial speed1-delete1 50000 row {DELETE FROM t1}
238abcddedbSdrh
239abcddedbSdrh# Copy one table into another
240abcddedbSdrh#
241abcddedbSdrhspeed_trial speed1-copy1 50000 row {INSERT INTO t1 SELECT * FROM t2}
242abcddedbSdrh
243abcddedbSdrh# Delete all content in a table, one row at a time.
244abcddedbSdrh#
245abcddedbSdrhspeed_trial speed1-delete2 50000 row {DELETE FROM t1 WHERE 1}
246abcddedbSdrh
247abcddedbSdrh# Refill the table yet again
248abcddedbSdrh#
249abcddedbSdrhspeed_trial speed1-copy2 50000 row {INSERT INTO t1 SELECT * FROM t2}
250abcddedbSdrh
251abcddedbSdrh# Drop the table and recreate it without its indices.
252abcddedbSdrh#
253abcddedbSdrhdb eval BEGIN
254abcddedbSdrhspeed_trial speed1-drop1 50000 row {
255abcddedbSdrh   DROP TABLE t1;
256abcddedbSdrh   CREATE TABLE t1(a INTEGER, b INTEGER, c TEXT);
257abcddedbSdrh}
258abcddedbSdrhdb eval COMMIT
259abcddedbSdrh
260abcddedbSdrh# Refill the table yet again.  This copy should be faster because
261abcddedbSdrh# there are no indices to deal with.
262abcddedbSdrh#
263abcddedbSdrhspeed_trial speed1-copy3 50000 row {INSERT INTO t1 SELECT * FROM t2}
264abcddedbSdrh
265abcddedbSdrh# Select 20000 rows from the table at random.
266abcddedbSdrh#
267abcddedbSdrhspeed_trial speed1-random1 50000 row {
268abcddedbSdrh  SELECT rowid FROM t1 ORDER BY random() LIMIT 20000
269abcddedbSdrh}
270abcddedbSdrh
271abcddedbSdrh# Delete 20000 random rows from the table.
272abcddedbSdrh#
273abcddedbSdrhspeed_trial speed1-random-del1 20000 row {
274abcddedbSdrh  DELETE FROM t1 WHERE rowid IN
275abcddedbSdrh    (SELECT rowid FROM t1 ORDER BY random() LIMIT 20000)
276abcddedbSdrh}
277abcddedbSdrhdo_test speed1-1.1 {
278abcddedbSdrh  db one {SELECT count(*) FROM t1}
279abcddedbSdrh} 30000
280abcddedbSdrh
281abcddedbSdrh
282abcddedbSdrh# Delete 20000 more rows at random from the table.
283abcddedbSdrh#
284abcddedbSdrhspeed_trial speed1-random-del2 20000 row {
285abcddedbSdrh  DELETE FROM t1 WHERE rowid IN
286abcddedbSdrh    (SELECT rowid FROM t1 ORDER BY random() LIMIT 20000)
287abcddedbSdrh}
288abcddedbSdrhdo_test speed1-1.2 {
289abcddedbSdrh  db one {SELECT count(*) FROM t1}
290abcddedbSdrh} 10000
291dd92431aSdrhspeed_trial_summary speed1
292abcddedbSdrh
293ab7bee89Sdanielk1977db close
294ab7bee89Sdanielk1977sqlite3_shutdown
295ab7bee89Sdanielk1977eval sqlite3_config_lookaside $old_lookaside
296*bb77b753Sdrhsqlite3_initialize
297*bb77b753Sdrhautoinstall_test_functions
298abcddedbSdrhfinish_test
299