speed4p.explain 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. # 2007 October 23
  2. #
  3. # The author disclaims copyright to this source code. In place of
  4. # a legal notice, here is a blessing:
  5. #
  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.
  9. #
  10. #*************************************************************************
  11. # This file implements regression tests for SQLite library. The
  12. # focus of this script is measuring executing speed. More specifically,
  13. # the focus is on the speed of:
  14. #
  15. # * joins
  16. # * views
  17. # * sub-selects
  18. # * triggers
  19. #
  20. # $Id: speed4p.explain,v 1.1 2008/04/16 12:57:48 drh Exp $
  21. #
  22. set testdir [file dirname $argv0]
  23. source $testdir/tester.tcl
  24. speed_trial_init speed1
  25. # Set a uniform random seed
  26. expr srand(0)
  27. set sqlout [open speed1.txt w]
  28. proc tracesql {sql} {
  29. puts $::sqlout $sql\;
  30. }
  31. #db trace tracesql
  32. # The number_name procedure below converts its argment (an integer)
  33. # into a string which is the English-language name for that number.
  34. #
  35. # Example:
  36. #
  37. # puts [number_name 123] -> "one hundred twenty three"
  38. #
  39. set ones {zero one two three four five six seven eight nine
  40. ten eleven twelve thirteen fourteen fifteen sixteen seventeen
  41. eighteen nineteen}
  42. set tens {{} ten twenty thirty forty fifty sixty seventy eighty ninety}
  43. proc number_name {n} {
  44. if {$n>=1000} {
  45. set txt "[number_name [expr {$n/1000}]] thousand"
  46. set n [expr {$n%1000}]
  47. } else {
  48. set txt {}
  49. }
  50. if {$n>=100} {
  51. append txt " [lindex $::ones [expr {$n/100}]] hundred"
  52. set n [expr {$n%100}]
  53. }
  54. if {$n>=20} {
  55. append txt " [lindex $::tens [expr {$n/10}]]"
  56. set n [expr {$n%10}]
  57. }
  58. if {$n>0} {
  59. append txt " [lindex $::ones $n]"
  60. }
  61. set txt [string trim $txt]
  62. if {$txt==""} {set txt zero}
  63. return $txt
  64. }
  65. # Summary of tests:
  66. #
  67. # speed4p-join1: Join three tables using IPK index.
  68. # speed4p-join2: Join three tables using an index.
  69. # speed4p-join3: Join two tables without an index.
  70. #
  71. # speed4p-view1: Querying a view.
  72. # speed4p-table1: Same queries as in speed4p-view1, but run directly against
  73. # the tables for comparison purposes.
  74. #
  75. # speed4p-subselect1: A SELECT statement that uses many sub-queries..
  76. #
  77. # speed4p-trigger1: An INSERT statement that fires a trigger.
  78. # speed4p-trigger2: An UPDATE statement that fires a trigger.
  79. # speed4p-trigger3: A DELETE statement that fires a trigger.
  80. # speed4p-notrigger1: Same operation as trigger1, but without the trigger.
  81. # speed4p-notrigger2: " trigger2 "
  82. # speed4p-notrigger3: " trigger3 "
  83. #
  84. # Set up the schema. Each of the tables t1, t2 and t3 contain 50,000 rows.
  85. # This creates a database of around 16MB.
  86. execsql {
  87. PRAGMA page_size=1024;
  88. PRAGMA cache_size=8192;
  89. PRAGMA locking_mode=EXCLUSIVE;
  90. BEGIN;
  91. CREATE TABLE t1(rowid INTEGER PRIMARY KEY, i INTEGER, t TEXT);
  92. CREATE TABLE t2(rowid INTEGER PRIMARY KEY, i INTEGER, t TEXT);
  93. CREATE TABLE t3(rowid INTEGER PRIMARY KEY, i INTEGER, t TEXT);
  94. CREATE VIEW v1 AS SELECT rowid, i, t FROM t1;
  95. CREATE VIEW v2 AS SELECT rowid, i, t FROM t2;
  96. CREATE VIEW v3 AS SELECT rowid, i, t FROM t3;
  97. }
  98. for {set jj 1} {$jj <= 3} {incr jj} {
  99. set stmt [string map "%T% t$jj" {INSERT INTO %T% VALUES(NULL, $i, $t)}]
  100. for {set ii 0} {$ii < 50000} {incr ii} {
  101. set i [expr {int(rand()*50000)}]
  102. set t [number_name $i]
  103. execsql $stmt
  104. }
  105. }
  106. execsql {
  107. CREATE INDEX i1 ON t1(t);
  108. CREATE INDEX i2 ON t2(t);
  109. CREATE INDEX i3 ON t3(t);
  110. COMMIT;
  111. }
  112. # Before running these tests, disable the compiled statement cache built into
  113. # the Tcl interface. This is because we want to test the speed of SQL
  114. # compilation as well as execution.
  115. #
  116. db cache size 0
  117. # Join t1, t2, t3 on IPK.
  118. set sql "SELECT * FROM t1, t2, t3 WHERE t1.oid = t2.oid AND t2.oid = t3.oid"
  119. explain $sql
  120. speed_trial speed4p-join1 50000 row $sql
  121. # Join t1, t2, t3 on the non-IPK index.
  122. set sql "SELECT * FROM t1, t2, t3 WHERE t1.t = t2.t AND t2.t = t3.t"
  123. explain $sql
  124. speed_trial speed4p-join2 50000 row $sql
  125. # Run 10000 simple queries against the views.
  126. set script {
  127. for {set ii 1} {$ii < 10000} {incr ii} {
  128. set v [expr {$ii*3}]
  129. set t [expr {$ii%3+1}]
  130. db eval "SELECT * FROM v$t WHERE rowid = \$v"
  131. }
  132. }
  133. explain {SELECT * FROm v1 WHERE rowid=$v}
  134. speed_trial_tcl speed4p-view1 10000 stmt $script
  135. # Run the same 10000 simple queries as in the previous test case against
  136. # the underlying tables. The compiled vdbe programs should be identical, so
  137. # the only difference in running time is the extra time taken to compile
  138. # the view definitions.
  139. #
  140. set script {
  141. for {set ii 1} {$ii < 10000} {incr ii} {
  142. set v [expr {$ii*3}]
  143. set t [expr {$ii%3+1}]
  144. db eval "SELECT t FROM t$t WHERE rowid = \$v"
  145. }
  146. }
  147. explain {SELECT * FROM t1 WHERE rowid=$v}
  148. speed_trial_tcl speed4p-table1 10000 stmt $script
  149. # Run a SELECT that uses sub-queries 10000 times. A total of 30000 sub-selects.
  150. #
  151. set script {
  152. for {set ii 1} {$ii < 10000} {incr ii} {
  153. set v [expr {$ii*3}]
  154. db eval {
  155. SELECT (SELECT t FROM t1 WHERE rowid = $v),
  156. (SELECT t FROM t2 WHERE rowid = $v),
  157. (SELECT t FROM t3 WHERE rowid = $v)
  158. }
  159. }
  160. }
  161. explain {
  162. SELECT (SELECT t FROM t1 WHERE rowid = $v),
  163. (SELECT t FROM t2 WHERE rowid = $v),
  164. (SELECT t FROM t3 WHERE rowid = $v)
  165. }
  166. speed_trial_tcl speed4p-subselect1 10000 stmt $script
  167. # The following block tests the speed of some DML statements that cause
  168. # triggers to fire.
  169. #
  170. execsql {
  171. CREATE TABLE log(op TEXT, r INTEGER, i INTEGER, t TEXT);
  172. CREATE TABLE t4(rowid INTEGER PRIMARY KEY, i INTEGER, t TEXT);
  173. CREATE TRIGGER t4_trigger1 AFTER INSERT ON t4 BEGIN
  174. INSERT INTO log VALUES('INSERT INTO t4', new.rowid, new.i, new.t);
  175. END;
  176. CREATE TRIGGER t4_trigger2 AFTER UPDATE ON t4 BEGIN
  177. INSERT INTO log VALUES('UPDATE OF t4', new.rowid, new.i, new.t);
  178. END;
  179. CREATE TRIGGER t4_trigger3 AFTER DELETE ON t4 BEGIN
  180. INSERT INTO log VALUES('DELETE OF t4', old.rowid, old.i, old.t);
  181. END;
  182. BEGIN;
  183. }
  184. set list {}
  185. for {set ii 1} {$ii < 10000} {incr ii} {
  186. lappend list $ii [number_name $ii]
  187. }
  188. set script {
  189. foreach {ii name} $::list {
  190. db eval {INSERT INTO t4 VALUES(NULL, $ii, $name)}
  191. }
  192. }
  193. explain {INSERT INTO t4 VALUES(NULL, $ii, $name)}
  194. speed_trial_tcl speed4p-trigger1 10000 stmt $script
  195. set list {}
  196. for {set ii 1} {$ii < 20000} {incr ii 2} {
  197. set ii2 [expr {$ii*2}]
  198. lappend list $ii $ii2 [number_name $ii2]
  199. }
  200. set script {
  201. foreach {ii ii2 name} $::list {
  202. db eval {
  203. UPDATE t4 SET i = $ii2, t = $name WHERE rowid = $ii;
  204. }
  205. }
  206. }
  207. explain {UPDATE t4 SET i = $ii2, t = $name WHERE rowid = $ii}
  208. speed_trial_tcl speed4p-trigger2 10000 stmt $script
  209. set script {
  210. for {set ii 1} {$ii < 20000} {incr ii 2} {
  211. db eval {DELETE FROM t4 WHERE rowid = $ii}
  212. }
  213. }
  214. explain {DELETE FROM t4 WHERE rowid = $ii}
  215. speed_trial_tcl speed4p-trigger3 10000 stmt $script
  216. execsql {COMMIT}
  217. # The following block contains the same tests as the above block that
  218. # tests triggers, with one crucial difference: no triggers are defined.
  219. # So the difference in speed between these tests and the preceding ones
  220. # is the amount of time taken to compile and execute the trigger programs.
  221. #
  222. execsql {
  223. DROP TABLE t4;
  224. DROP TABLE log;
  225. VACUUM;
  226. CREATE TABLE t4(rowid INTEGER PRIMARY KEY, i INTEGER, t TEXT);
  227. BEGIN;
  228. }
  229. set list {}
  230. for {set ii 1} {$ii < 10000} {incr ii} {
  231. lappend list $ii [number_name $ii]
  232. }
  233. set script {
  234. foreach {ii name} $::list {
  235. db eval {INSERT INTO t4 VALUES(NULL, $ii, $name);}
  236. }
  237. }
  238. explain {INSERT INTO t4 VALUES(NULL, $ii, $name)}
  239. speed_trial_tcl speed4p-notrigger1 10000 stmt $script
  240. set list {}
  241. for {set ii 1} {$ii < 20000} {incr ii 2} {
  242. set ii2 [expr {$ii*2}]
  243. lappend list $ii $ii2 [number_name $ii2]
  244. }
  245. set script {
  246. foreach {ii ii2 name} $::list {
  247. db eval {
  248. UPDATE t4 SET i = $ii2, t = $name WHERE rowid = $ii;
  249. }
  250. }
  251. }
  252. explain {UPDATE t4 SET i = $ii2, t = $name WHERE rowid = $ii}
  253. speed_trial_tcl speed4p-notrigger2 10000 stmt $script
  254. set script {
  255. for {set ii 1} {$ii < 20000} {incr ii 2} {
  256. db eval {DELETE FROM t4 WHERE rowid = $ii}
  257. }
  258. }
  259. explain {DELETE FROM t4 WHERE rowid = $ii}
  260. speed_trial_tcl speed4p-notrigger3 10000 stmt $script
  261. execsql {COMMIT}
  262. speed_trial_summary speed4
  263. finish_test