speed.tcl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. #
  2. # Run this Tcl script to generate the speed.html file.
  3. #
  4. set rcsid {$Id: speed.tcl,v 1.6 2002/03/11 02:06:14 drh Exp $ }
  5. puts {<html>
  6. <head>
  7. <title>Database Speed Comparison: SQLite versus PostgreSQL</title>
  8. </head>
  9. <body bgcolor=white>
  10. <h1 align=center>
  11. Database Speed Comparison
  12. </h1>}
  13. puts "<p align=center>
  14. (This page was last modified on [lrange $rcsid 3 4] UTC)
  15. </p>"
  16. puts {
  17. <h2>Executive Summary</h2>
  18. <p>A series of tests were run to measure the relative performance of
  19. SQLite 2.4.0, PostgreSQL, and MySQL
  20. The following are general
  21. conclusions drawn from these experiments:
  22. </p>
  23. <ul>
  24. <li><p>
  25. SQLite 2.4.0 is significantly faster than PostgreSQL
  26. for most common operations.
  27. </p></li>
  28. <li><p>
  29. The speed of SQLite 2.4.0 is similar to MySQL.
  30. This is true in spite of the
  31. fact that SQLite contains full transaction support whereas the
  32. version of MySQL tested did not.
  33. </p></li>
  34. </ul>
  35. <h2>Test Environment</h2>
  36. <p>
  37. The platform used for these tests is a 1.6GHz Athlon with 1GB or memory
  38. and an IDE disk drive. The operating system is RedHat Linux 7.2 with
  39. a stock kernel.
  40. </p>
  41. <p>
  42. The PostgreSQL and MySQL servers used were as delivered by default on
  43. RedHat 7.2. No effort was made to tune these engines. Note in particular
  44. the the default MySQL configuration on RedHat 7.2 does not support
  45. transactions. Not having to support transactions gives MySQL a
  46. big advantage, but SQLite is still able to hold its own on most
  47. tests.
  48. </p>
  49. <p>
  50. SQLite was compiled with -O6 optimization and with
  51. the -DNDEBUG=1 switch which disables the many "assert()" statements
  52. in the SQLite code. The -DNDEBUG=1 compiler option roughly doubles
  53. the speed of SQLite.
  54. </p>
  55. <p>
  56. All tests are conducted on an otherwise quiescent machine.
  57. A simple Tcl script was used to generate and run all the tests.
  58. A copy of this Tcl script can be found in the SQLite source tree
  59. in the file <b>tools/speedtest.tcl</b>.
  60. </p>
  61. <p>
  62. The times reported on all tests represent wall-clock time
  63. in seconds. Two separate time values are reported for SQLite.
  64. The first value is for SQLite in its default configuration with
  65. full disk synchronization turned on. With synchronization turned
  66. on, SQLite executes
  67. an <b>fsync()</b> system call (or the equivalent) at key points
  68. to make certain that critical data has
  69. actually been written to the disk drive surface. Synchronization
  70. is necessary to guarantee the integrity of the database if the
  71. operating system crashes or the computer powers down unexpectedly
  72. in the middle of a database update. The second time reported for SQLite is
  73. when synchronization is turned off. With synchronization off,
  74. SQLite is sometimes much faster, but there is a risk that an
  75. operating system crash or an unexpected power failure could
  76. damage the database. Generally speaking, the synchronous SQLite
  77. times are for comparison against PostgreSQL (which is also
  78. synchronous) and the asynchronous SQLite times are for
  79. comparison against the asynchronous MySQL engine.
  80. </p>
  81. <h2>Test 1: 1000 INSERTs</h2>
  82. <blockquote>
  83. CREATE TABLE t1(a INTEGER, b INTEGER, c VARCHAR(100));<br>
  84. INSERT INTO t1 VALUES(1,13153,'thirteen thousand one hundred fifty three');<br>
  85. INSERT INTO t1 VALUES(2,75560,'seventy five thousand five hundred sixty');<br>
  86. <i>... 995 lines omitted</i><br>
  87. INSERT INTO t1 VALUES(998,66289,'sixty six thousand two hundred eighty nine');<br>
  88. INSERT INTO t1 VALUES(999,24322,'twenty four thousand three hundred twenty two');<br>
  89. INSERT INTO t1 VALUES(1000,94142,'ninety four thousand one hundred forty two');<br>
  90. </blockquote><table border=0 cellpadding=0 cellspacing=0>
  91. <tr><td>PostgreSQL:</td><td align="right">&nbsp;&nbsp;&nbsp;4.027</td></tr>
  92. <tr><td>MySQL:</td><td align="right">&nbsp;&nbsp;&nbsp;0.113</td></tr>
  93. <tr><td>SQLite 2.4:</td><td align="right">&nbsp;&nbsp;&nbsp;8.409</td></tr>
  94. <tr><td>SQLite 2.4 (nosync):</td><td align="right">&nbsp;&nbsp;&nbsp;0.188</td></tr>
  95. </table>
  96. <p>SQLite must close and reopen the database file, and thus invalidate
  97. its cache, for each SQL statement. In spite of this, the asynchronous
  98. version of SQLite is still nearly as fast as MySQL. Notice how much slower
  99. the synchronous version is, however. This is due to the necessity of
  100. calling <b>fsync()</b> after each SQL statement.</p>
  101. <h2>Test 2: 25000 INSERTs in a transaction</h2>
  102. <blockquote>
  103. BEGIN;<br>
  104. CREATE TABLE t2(a INTEGER, b INTEGER, c VARCHAR(100));<br>
  105. INSERT INTO t2 VALUES(1,298361,'two hundred ninety eight thousand three hundred sixty one');<br>
  106. <i>... 24997 lines omitted</i><br>
  107. INSERT INTO t2 VALUES(24999,447847,'four hundred forty seven thousand eight hundred forty seven');<br>
  108. INSERT INTO t2 VALUES(25000,473330,'four hundred seventy three thousand three hundred thirty');<br>
  109. COMMIT;<br>
  110. </blockquote><table border=0 cellpadding=0 cellspacing=0>
  111. <tr><td>PostgreSQL:</td><td align="right">&nbsp;&nbsp;&nbsp;5.175</td></tr>
  112. <tr><td>MySQL:</td><td align="right">&nbsp;&nbsp;&nbsp;2.444</td></tr>
  113. <tr><td>SQLite 2.4:</td><td align="right">&nbsp;&nbsp;&nbsp;0.858</td></tr>
  114. <tr><td>SQLite 2.4 (nosync):</td><td align="right">&nbsp;&nbsp;&nbsp;0.739</td></tr>
  115. </table>
  116. <p>
  117. When all the INSERTs are put in a transaction, SQLite no longer has to
  118. close and reopen the database between each statement. It also does not
  119. have to do any fsync()s until the very end. When unshackled in
  120. this way, SQLite is much faster than either PostgreSQL and MySQL.
  121. </p>
  122. <h2>Test 3: 100 SELECTs without an index</h2>
  123. <blockquote>
  124. SELECT count(*), avg(b) FROM t2 WHERE b>=0 AND b<1000;<br>
  125. SELECT count(*), avg(b) FROM t2 WHERE b>=100 AND b<1100;<br>
  126. SELECT count(*), avg(b) FROM t2 WHERE b>=200 AND b<1200;<br>
  127. <i>... 94 lines omitted</i><br>
  128. SELECT count(*), avg(b) FROM t2 WHERE b>=9700 AND b<10700;<br>
  129. SELECT count(*), avg(b) FROM t2 WHERE b>=9800 AND b<10800;<br>
  130. SELECT count(*), avg(b) FROM t2 WHERE b>=9900 AND b<10900;<br>
  131. </blockquote><table border=0 cellpadding=0 cellspacing=0>
  132. <tr><td>PostgreSQL:</td><td align="right">&nbsp;&nbsp;&nbsp;3.773</td></tr>
  133. <tr><td>MySQL:</td><td align="right">&nbsp;&nbsp;&nbsp;3.023</td></tr>
  134. <tr><td>SQLite 2.4:</td><td align="right">&nbsp;&nbsp;&nbsp;6.281</td></tr>
  135. <tr><td>SQLite 2.4 (nosync):</td><td align="right">&nbsp;&nbsp;&nbsp;6.247</td></tr>
  136. </table>
  137. <p>
  138. This test does 100 queries on a 25000 entry table without an index,
  139. thus requiring a full table scan. SQLite is about half the speed of
  140. PostgreSQL and MySQL. This is because SQLite stores all data as strings
  141. and must therefore call <b>strtod()</b> 5 million times in the
  142. course of evaluating the WHERE clauses. Both PostgreSQL and MySQL
  143. store data as binary values where appropriate and can forego
  144. this conversion effort.
  145. </p>
  146. <h2>Test 4: 100 SELECTs on a string comparison</h2>
  147. <blockquote>
  148. SELECT count(*), avg(b) FROM t2 WHERE c LIKE '%one%';<br>
  149. SELECT count(*), avg(b) FROM t2 WHERE c LIKE '%two%';<br>
  150. SELECT count(*), avg(b) FROM t2 WHERE c LIKE '%three%';<br>
  151. <i>... 94 lines omitted</i><br>
  152. SELECT count(*), avg(b) FROM t2 WHERE c LIKE '%ninety eight%';<br>
  153. SELECT count(*), avg(b) FROM t2 WHERE c LIKE '%ninety nine%';<br>
  154. SELECT count(*), avg(b) FROM t2 WHERE c LIKE '%one hundred%';<br>
  155. </blockquote><table border=0 cellpadding=0 cellspacing=0>
  156. <tr><td>PostgreSQL:</td><td align="right">&nbsp;&nbsp;&nbsp;16.726</td></tr>
  157. <tr><td>MySQL:</td><td align="right">&nbsp;&nbsp;&nbsp;5.237</td></tr>
  158. <tr><td>SQLite 2.4:</td><td align="right">&nbsp;&nbsp;&nbsp;6.137</td></tr>
  159. <tr><td>SQLite 2.4 (nosync):</td><td align="right">&nbsp;&nbsp;&nbsp;6.112</td></tr>
  160. </table>
  161. <p>
  162. This set of 100 queries uses string comparisons instead of
  163. numerical comparisions. As a result, the speed of SQLite is
  164. compariable to are better then PostgreSQL and MySQL.
  165. </p>
  166. <h2>Test 5: Creating an index</h2>
  167. <blockquote>
  168. CREATE INDEX i2a ON t2(a);<br>CREATE INDEX i2b ON t2(b);
  169. </blockquote><table border=0 cellpadding=0 cellspacing=0>
  170. <tr><td>PostgreSQL:</td><td align="right">&nbsp;&nbsp;&nbsp;0.510</td></tr>
  171. <tr><td>MySQL:</td><td align="right">&nbsp;&nbsp;&nbsp;0.352</td></tr>
  172. <tr><td>SQLite 2.4:</td><td align="right">&nbsp;&nbsp;&nbsp;0.809</td></tr>
  173. <tr><td>SQLite 2.4 (nosync):</td><td align="right">&nbsp;&nbsp;&nbsp;0.720</td></tr>
  174. </table>
  175. <p>
  176. SQLite is slower at creating new indices. But since creating
  177. new indices is an uncommon operation, this is not seen as a
  178. problem.
  179. </p>
  180. <h2>Test 6: 5000 SELECTs with an index</h2>
  181. <blockquote>
  182. SELECT count(*), avg(b) FROM t2 WHERE b>=0 AND b<100;<br>
  183. SELECT count(*), avg(b) FROM t2 WHERE b>=100 AND b<200;<br>
  184. SELECT count(*), avg(b) FROM t2 WHERE b>=200 AND b<300;<br>
  185. <i>... 4994 lines omitted</i><br>
  186. SELECT count(*), avg(b) FROM t2 WHERE b>=499700 AND b<499800;<br>
  187. SELECT count(*), avg(b) FROM t2 WHERE b>=499800 AND b<499900;<br>
  188. SELECT count(*), avg(b) FROM t2 WHERE b>=499900 AND b<500000;<br>
  189. </blockquote><table border=0 cellpadding=0 cellspacing=0>
  190. <tr><td>PostgreSQL:</td><td align="right">&nbsp;&nbsp;&nbsp;5.318</td></tr>
  191. <tr><td>MySQL:</td><td align="right">&nbsp;&nbsp;&nbsp;1.555</td></tr>
  192. <tr><td>SQLite 2.4:</td><td align="right">&nbsp;&nbsp;&nbsp;1.289</td></tr>
  193. <tr><td>SQLite 2.4 (nosync):</td><td align="right">&nbsp;&nbsp;&nbsp;1.273</td></tr>
  194. </table>
  195. <p>
  196. This test runs a set of 5000 queries that are similar in form to
  197. those in test 3. But now instead of being half as fast, SQLite
  198. is faster than both PostgreSQL and MySQL.
  199. </p>
  200. <h2>Test 7: 1000 UPDATEs without an index</h2>
  201. <blockquote>
  202. BEGIN;<br>
  203. UPDATE t1 SET b=b*2 WHERE a>=0 AND a<10;<br>
  204. UPDATE t1 SET b=b*2 WHERE a>=10 AND a<20;<br>
  205. <i>... 996 lines omitted</i><br>
  206. UPDATE t1 SET b=b*2 WHERE a>=9980 AND a<9990;<br>
  207. UPDATE t1 SET b=b*2 WHERE a>=9990 AND a<10000;<br>
  208. COMMIT;<br>
  209. </blockquote><table border=0 cellpadding=0 cellspacing=0>
  210. <tr><td>PostgreSQL:</td><td align="right">&nbsp;&nbsp;&nbsp;1.828</td></tr>
  211. <tr><td>MySQL:</td><td align="right">&nbsp;&nbsp;&nbsp;9.272</td></tr>
  212. <tr><td>SQLite 2.4:</td><td align="right">&nbsp;&nbsp;&nbsp;0.915</td></tr>
  213. <tr><td>SQLite 2.4 (nosync):</td><td align="right">&nbsp;&nbsp;&nbsp;0.889</td></tr>
  214. </table>
  215. <p>
  216. Here is a case where MySQL is over 10 times slower than SQLite.
  217. The reason for this is unclear.
  218. </p>
  219. <h2>Test 8: 25000 UPDATEs with an index</h2>
  220. <blockquote>
  221. BEGIN;<br>
  222. UPDATE t2 SET b=271822 WHERE a=1;<br>
  223. UPDATE t2 SET b=28304 WHERE a=2;<br>
  224. <i>... 24996 lines omitted</i><br>
  225. UPDATE t2 SET b=442549 WHERE a=24999;<br>
  226. UPDATE t2 SET b=423958 WHERE a=25000;<br>
  227. COMMIT;<br>
  228. </blockquote><table border=0 cellpadding=0 cellspacing=0>
  229. <tr><td>PostgreSQL:</td><td align="right">&nbsp;&nbsp;&nbsp;28.021</td></tr>
  230. <tr><td>MySQL:</td><td align="right">&nbsp;&nbsp;&nbsp;8.565</td></tr>
  231. <tr><td>SQLite 2.4:</td><td align="right">&nbsp;&nbsp;&nbsp;10.939</td></tr>
  232. <tr><td>SQLite 2.4 (nosync):</td><td align="right">&nbsp;&nbsp;&nbsp;11.199</td></tr>
  233. </table>
  234. <p>
  235. In this case MySQL is slightly faster than SQLite, though not by much.
  236. The difference is believed to have to do with the fact SQLite
  237. handles the integers as strings instead of binary numbers.
  238. </p>
  239. <h2>Test 9: 25000 text UPDATEs with an index</h2>
  240. <blockquote>
  241. BEGIN;<br>
  242. UPDATE t2 SET c='four hundred sixty eight thousand twenty six' WHERE a=1;<br>
  243. UPDATE t2 SET c='one hundred twenty one thousand nine hundred twenty eight' WHERE a=2;<br>
  244. <i>... 24996 lines omitted</i><br>
  245. UPDATE t2 SET c='thirty five thousand sixty five' WHERE a=24999;<br>
  246. UPDATE t2 SET c='three hundred forty seven thousand three hundred ninety three' WHERE a=25000;<br>
  247. COMMIT;<br>
  248. </blockquote><table border=0 cellpadding=0 cellspacing=0>
  249. <tr><td>PostgreSQL:</td><td align="right">&nbsp;&nbsp;&nbsp;48.739</td></tr>
  250. <tr><td>MySQL:</td><td align="right">&nbsp;&nbsp;&nbsp;7.059</td></tr>
  251. <tr><td>SQLite 2.4:</td><td align="right">&nbsp;&nbsp;&nbsp;7.868</td></tr>
  252. <tr><td>SQLite 2.4 (nosync):</td><td align="right">&nbsp;&nbsp;&nbsp;6.720</td></tr>
  253. </table>
  254. <p>
  255. When updating a text field instead of an integer field,
  256. SQLite is slightly faster than MySQL.
  257. </p>
  258. <h2>Test 10: INSERTs from a SELECT</h2>
  259. <blockquote>
  260. BEGIN;<br>INSERT INTO t1 SELECT * FROM t2;<br>INSERT INTO t2 SELECT * FROM t1;<br>COMMIT;
  261. </blockquote><table border=0 cellpadding=0 cellspacing=0>
  262. <tr><td>PostgreSQL:</td><td align="right">&nbsp;&nbsp;&nbsp;54.822</td></tr>
  263. <tr><td>MySQL:</td><td align="right">&nbsp;&nbsp;&nbsp;1.512</td></tr>
  264. <tr><td>SQLite 2.4:</td><td align="right">&nbsp;&nbsp;&nbsp;4.423</td></tr>
  265. <tr><td>SQLite 2.4 (nosync):</td><td align="right">&nbsp;&nbsp;&nbsp;2.386</td></tr>
  266. </table>
  267. <p>
  268. The poor performance of PostgreSQL in this case appears to be due to its
  269. synchronous behavior. The CPU was mostly idle during the 55 second run.
  270. </p>
  271. <h2>Test 11: DELETE without an index</h2>
  272. <blockquote>
  273. DELETE FROM t2 WHERE c LIKE '%fifty%';
  274. </blockquote><table border=0 cellpadding=0 cellspacing=0>
  275. <tr><td>PostgreSQL:</td><td align="right">&nbsp;&nbsp;&nbsp;0.734</td></tr>
  276. <tr><td>MySQL:</td><td align="right">&nbsp;&nbsp;&nbsp;0.888</td></tr>
  277. <tr><td>SQLite 2.4:</td><td align="right">&nbsp;&nbsp;&nbsp;5.405</td></tr>
  278. <tr><td>SQLite 2.4 (nosync):</td><td align="right">&nbsp;&nbsp;&nbsp;0.731</td></tr>
  279. </table>
  280. <h2>Test 12: DELETE with an index</h2>
  281. <blockquote>
  282. DELETE FROM t2 WHERE a>10 AND a<20000;
  283. </blockquote><table border=0 cellpadding=0 cellspacing=0>
  284. <tr><td>PostgreSQL:</td><td align="right">&nbsp;&nbsp;&nbsp;2.318</td></tr>
  285. <tr><td>MySQL:</td><td align="right">&nbsp;&nbsp;&nbsp;2.600</td></tr>
  286. <tr><td>SQLite 2.4:</td><td align="right">&nbsp;&nbsp;&nbsp;1.436</td></tr>
  287. <tr><td>SQLite 2.4 (nosync):</td><td align="right">&nbsp;&nbsp;&nbsp;0.775</td></tr>
  288. </table>
  289. <h2>Test 13: A big INSERT after a big DELETE</h2>
  290. <blockquote>
  291. INSERT INTO t2 SELECT * FROM t1;
  292. </blockquote><table border=0 cellpadding=0 cellspacing=0>
  293. <tr><td>PostgreSQL:</td><td align="right">&nbsp;&nbsp;&nbsp;63.867</td></tr>
  294. <tr><td>MySQL:</td><td align="right">&nbsp;&nbsp;&nbsp;1.839</td></tr>
  295. <tr><td>SQLite 2.4:</td><td align="right">&nbsp;&nbsp;&nbsp;3.971</td></tr>
  296. <tr><td>SQLite 2.4 (nosync):</td><td align="right">&nbsp;&nbsp;&nbsp;1.993</td></tr>
  297. </table>
  298. <p>
  299. Earlier versions of SQLite would show decreasing performance after a
  300. sequence DELETEs followed by new INSERTs. As this test shows, the
  301. problem has now been resolved.
  302. </p>
  303. <h2>Test 14: A big DELETE followed by many small INSERTs</h2>
  304. <blockquote>
  305. BEGIN;<br>
  306. DELETE FROM t1;<br>
  307. INSERT INTO t1 VALUES(1,29676,'twenty nine thousand six hundred seventy six');<br>
  308. <i>... 2997 lines omitted</i><br>
  309. INSERT INTO t1 VALUES(2999,37835,'thirty seven thousand eight hundred thirty five');<br>
  310. INSERT INTO t1 VALUES(3000,97817,'ninety seven thousand eight hundred seventeen');<br>
  311. COMMIT;<br>
  312. </blockquote><table border=0 cellpadding=0 cellspacing=0>
  313. <tr><td>PostgreSQL:</td><td align="right">&nbsp;&nbsp;&nbsp;1.209</td></tr>
  314. <tr><td>MySQL:</td><td align="right">&nbsp;&nbsp;&nbsp;1.031</td></tr>
  315. <tr><td>SQLite 2.4:</td><td align="right">&nbsp;&nbsp;&nbsp;0.298</td></tr>
  316. <tr><td>SQLite 2.4 (nosync):</td><td align="right">&nbsp;&nbsp;&nbsp;0.282</td></tr>
  317. </table>
  318. <h2>Test 15: DROP TABLE</h2>
  319. <blockquote>
  320. DROP TABLE t1;<br>DROP TABLE t2;
  321. </blockquote><table border=0 cellpadding=0 cellspacing=0>
  322. <tr><td>PostgreSQL:</td><td align="right">&nbsp;&nbsp;&nbsp;0.105</td></tr>
  323. <tr><td>MySQL:</td><td align="right">&nbsp;&nbsp;&nbsp;0.015</td></tr>
  324. <tr><td>SQLite 2.4:</td><td align="right">&nbsp;&nbsp;&nbsp;0.472</td></tr>
  325. <tr><td>SQLite 2.4 (nosync):</td><td align="right">&nbsp;&nbsp;&nbsp;0.232</td></tr>
  326. </table>
  327. <p>
  328. SQLite is slower than the other databases when it comes to dropping tables.
  329. This is not seen as a big problem, however, since DROP TABLE is seldom
  330. used in speed-critical situations.
  331. </p>
  332. }
  333. puts {
  334. <p><hr /></p>
  335. <p><a href="index.html"><img src="/goback.jpg" border=0 />
  336. Back to the SQLite Home Page</a>
  337. </p>
  338. </body></html>}