Debian Codegraph @

www/whentouse.html

raw
1 <!DOCTYPE html>
2 <html><head>
3 <meta name="viewport" content="width=device-width, initial-scale=1.0">
4 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
5 <link href="sqlite.css" rel="stylesheet">
6 <title>Appropriate Uses For SQLite</title>
7 <!-- path= -->
8 </head>
9 <body>
10 <div class=nosearch>
11 <a href="index.html">
12 <img class="logo" src="images/sqlite370_banner.gif" alt="SQLite" border="0">
13 </a>
14 <div><!-- IE hack to prevent disappearing logo --></div>
15 <div class="tagline desktoponly">
16 Small. Fast. Reliable.<br>Choose any three.
17 </div>
18 <div class="menu mainmenu">
19 <ul>
20 <li><a href="index.html">Home</a>
21 <li class='mobileonly'><a href="javascript:void(0)" onclick='toggle_div("submenu")'>Menu</a>
22 <li class='wideonly'><a href='about.html'>About</a>
23 <li class='desktoponly'><a href="docs.html">Documentation</a>
24 <li class='desktoponly'><a href="download.html">Download</a>
25 <li class='wideonly'><a href='copyright.html'>License</a>
26 <li class='desktoponly'><a href="support.html">Support</a>
27 <li class='desktoponly'><a href="prosupport.html">Purchase</a>
28 <li class='search' id='search_menubutton'>
29 <a href="javascript:void(0)" onclick='toggle_search()'>Search</a>
30 </ul>
31 </div>
32 <div class="menu submenu" id="submenu">
33 <ul>
34 <li><a href='about.html'>About</a>
35 <li><a href='docs.html'>Documentation</a>
36 <li><a href='download.html'>Download</a>
37 <li><a href='support.html'>Support</a>
38 <li><a href='prosupport.html'>Purchase</a>
39 </ul>
40 </div>
41 <div class="searchmenu" id="searchmenu">
42 <form method="GET" action="search">
43 <select name="s" id="searchtype">
44 <option value="d">Search Documentation</option>
45 <option value="c">Search Changelog</option>
46 </select>
47 <input type="text" name="q" id="searchbox" value="">
48 <input type="submit" value="Go">
49 </form>
50 </div>
51 </div>
52 <script>
53 function toggle_div(nm) {
54 var w = document.getElementById(nm);
55 if( w.style.display=="block" ){
56 w.style.display = "none";
57 }else{
58 w.style.display = "block";
59 }
60 }
61 function toggle_search() {
62 var w = document.getElementById("searchmenu");
63 if( w.style.display=="block" ){
64 w.style.display = "none";
65 } else {
66 w.style.display = "block";
67 setTimeout(function(){
68 document.getElementById("searchbox").focus()
69 }, 30);
70 }
71 }
72 function div_off(nm){document.getElementById(nm).style.display="none";}
73 window.onbeforeunload = function(e){div_off("submenu");}
74 /* Disable the Search feature if we are not operating from CGI, since */
75 /* Search is accomplished using CGI and will not work without it. */
76 if( !location.origin || !location.origin.match || !location.origin.match(/http/) ){
77 document.getElementById("search_menubutton").style.display = "none";
78 }
79 /* Used by the Hide/Show button beside syntax diagrams, to toggle the */
80 function hideorshow(btn,obj){
81 var x = document.getElementById(obj);
82 var b = document.getElementById(btn);
83 if( x.style.display!='none' ){
84 x.style.display = 'none';
85 b.innerHTML='show';
86 }else{
87 x.style.display = '';
88 b.innerHTML='hide';
89 }
90 return false;
91 }
92 var antiRobot = 0;
93 function antiRobotGo(){
94 if( antiRobot!=3 ) return;
95 antiRobot = 7;
96 var j = document.getElementById("mtimelink");
97 if(j && j.hasAttribute("data-href")) j.href=j.getAttribute("data-href");
98 }
99 function antiRobotDefense(){
100 document.body.onmousedown=function(){
101 antiRobot |= 2;
102 antiRobotGo();
103 document.body.onmousedown=null;
104 }
105 document.body.onmousemove=function(){
106 antiRobot |= 2;
107 antiRobotGo();
108 document.body.onmousemove=null;
109 }
110 setTimeout(function(){
111 antiRobot |= 1;
112 antiRobotGo();
113 }, 100)
114 antiRobotGo();
115 }
116 antiRobotDefense();
117 </script>
118 <div class=fancy>
119 <div class=nosearch>
120 <div class="fancy_title">
121 Appropriate Uses For SQLite
122 </div>
123 <div class="fancy_toc">
124 <a onclick="toggle_toc()">
125 <span class="fancy_toc_mark" id="toc_mk">&#x25ba;</span>
126 Table Of Contents
127 </a>
128 <div id="toc_sub"><div class="fancy-toc1"><a href="#situations_where_sqlite_works_well">1. Situations Where SQLite Works Well</a></div>
129 <div class="fancy-toc1"><a href="#situations_where_a_client_server_rdbms_may_work_better">2. Situations Where A Client/Server RDBMS May Work Better</a></div>
130 <div class="fancy-toc1"><a href="#checklist_for_choosing_the_right_database_engine">3. Checklist For Choosing The Right Database Engine</a></div>
131 </div>
132 </div>
133 <script>
134 function toggle_toc(){
135 var sub = document.getElementById("toc_sub")
136 var mk = document.getElementById("toc_mk")
137 if( sub.style.display!="block" ){
138 sub.style.display = "block";
139 mk.innerHTML = "&#x25bc;";
140 } else {
141 sub.style.display = "none";
142 mk.innerHTML = "&#x25ba;";
143 }
144 }
145 </script>
146 </div>
147
148
149
150
151 <p>
152 SQLite is not directly comparable to client/server SQL database engines such
153 as MySQL, Oracle, PostgreSQL, or SQL Server since SQLite is trying to
154 solve a different problem.
155 </p>
156
157 <p>
158 Client/server SQL database engines strive to implement a shared repository
159 of enterprise data. They emphasize scalability, concurrency, centralization,
160 and control.
161 SQLite strives to provide local data storage for
162 individual applications and devices. SQLite emphasizes economy,
163 efficiency, reliability, independence, and simplicity.
164 </p>
165
166
167 <p>
168 SQLite does not compete with client/server databases.
169 SQLite competes with <a href="http://man.he.net/man3/fopen">fopen()</a>.
170 </p>
171
172 <h1 id="situations_where_sqlite_works_well"><span>1. </span>Situations Where SQLite Works Well</h1>
173
174 <ul>
175
176 <a name="appfileformat"></a>
177
178 <li><b>Embedded devices and the internet of things</b>
179
180 <p>Because an SQLite database requires no administration,
181 it works well in devices that must operate without expert human support.
182 SQLite is a good fit for use in
183 cellphones, set-top boxes, televisions, game consoles,
184 cameras, watches, kitchen appliances, thermostats, automobiles,
185 machine tools, airplanes, remote sensors, drones, medical devices,
186 and robots: the "internet of things".
187 </p>
188
189 <p>Client/server database engines are designed to live inside a
190 lovingly-attended datacenter at the core of the network.
191 SQLite works there too, but SQLite also thrives at the edge of the network,
192 fending for itself while providing fast and
193 reliable data services to applications that would otherwise
194 have dodgy connectivity.
195 </p></li>
196
197 <li><p><b>Application file format</b></p>
198
199 <p>
200 SQLite is often used as the on-disk file format
201 for desktop applications such as version control systems,
202 financial analysis tools, media cataloging and editing suites, CAD
203 packages, record keeping programs, and so forth. The traditional
204 File/Open operation calls sqlite3_open() to attach to the database
205 file. Updates happen automatically as application content is revised
206 so the File/Save menu option becomes superfluous. The File/Save_As
207 menu option can be implemented using the <a href="backup.html">backup API</a>.
208 </p>
209
210 <p>There are many benefits to this approach, including improved
211 performance, reduced cost and complexity, and
212 improved reliability. See technical notes
213 <a href="aff_short.html">"aff_short.html"</a> and
214 <a href="appfileformat.html">"appfileformat.html"</a> and
215 <a href="fasterthanfs.html">"fasterthanfs.html"</a> for more information.
216 This use case is closely related to the
217 <a href="#wireproto">data transfer format</a> and
218 <a href="#container">data container</a> use cases below.
219 </p></li>
220
221 <a name="website"></a>
222
223 <li><p><b>Websites</b></p>
224
225 <p>SQLite works great as the database engine for most low to
226 medium traffic websites (which is to say, most websites).
227 The amount of web traffic that SQLite can handle depends
228 on how heavily the website uses its database. Generally
229 speaking, any site that gets fewer than 100K hits/day should work
230 fine with SQLite.
231 The 100K hits/day figure is a conservative estimate, not a
232 hard upper bound.
233 SQLite has been demonstrated to work with 10 times that amount
234 of traffic.</p>
235
236 <p>The SQLite website (<a href="https://www.sqlite.org/">https://www.sqlite.org/</a>) uses SQLite itself,
237 of course, and as of this writing (2015) it handles about 400K to 500K
238 HTTP requests per day, about 15-20% of which are dynamic pages touching
239 the database. Dynamic content uses <a href="np1queryprob.html">about 200 SQL statements per webpage</a>.
240 This setup runs on a single VM that shares a physical server with 23 others
241 and yet still keeps the load average below 0.1 most of the time.
242
243 </p><p>See also: <a href="https://news.ycombinator.com/item?id=33975635">Hacker New discussion from 2022-12-13</a>.
244 </p></li>
245
246 <li><p><b>Data analysis</b></p>
247
248 <p>
249 People who understand SQL can employ the
250 <a href="cli.html">sqlite3 command-line shell</a> (or various third-party
251 SQLite access programs) to analyze large
252 datasets. Raw data can be imported from CSV files, then that
253 data can be sliced and diced to generate a myriad of summary
254 reports. More complex analysis can be done using simple scripts written
255 in Tcl or Python (both of which come with SQLite built-in) or in R or
256 other languages using readily available adaptors.
257 Possible uses include website log analysis, sports
258 statistics analysis, compilation of programming metrics, and
259 analysis of experimental results. Many bioinformatics researchers
260 use SQLite in this way.
261 </p>
262
263 <p>
264 The same thing can be done with an enterprise client/server
265 database, of course. The advantage of SQLite is
266 that it is easier to install and use and the resulting database
267 is a single file that can be written to a USB memory stick
268 or emailed to a colleague.
269 </p>
270 </li>
271
272 <li><p><b>Cache for enterprise data</b></p>
273
274 <p>
275 Many applications use SQLite as a cache of relevant content from
276 an enterprise RDBMS.
277 This reduces latency, since most queries now occur against the local
278 cache and avoid a network round-trip. It also reduces the load
279 on the network and on the central database server. And in many cases,
280 it means that the client-side application can continue operating during
281 network outages.
282 </p>
283 </li>
284
285 <a name="serversidedb"></a>
286
287 <li><p><b>Server-side database</b></p>
288
289 <p>
290 Systems designers
291 report success using SQLite as a data store on server applications
292 running in the datacenter, or in other words, using SQLite as the underlying
293 storage engine for an application-specific database server.</p>
294
295 <p>With this pattern, the overall system is still client/server:
296 clients send requests to the server and get back replies over the network.
297 But instead of sending generic SQL and getting back raw table content,
298 the client requests and server responses are high-level and
299 application-specific.
300 The server translates requests into multiple SQL queries, gathers the
301 results, does post-processing, filtering, and analysis, then constructs
302 a high-level reply containing only the essential information.</p>
303
304 <p>Developers report that SQLite is often faster than a client/server
305 SQL database engine in this scenario.
306 Database requests are serialized by the server, so concurrency is not
307 an issue. Concurrency is also improved by "database sharding":
308 using separate database files for different subdomains. For
309 example, the server might have a separate SQLite database for each
310 user, so that the server can handle hundreds or thousands of simultaneous
311 connections, but each SQLite database is only used by one connection.</p>
312 </li>
313
314 <a name="wireproto"></a>
315
316 <li><p><b>Data transfer format</b></p><p>
317
318 </p><p>Because an SQLite database is a single compact file in a
319 <a href="fileformat2.html">well-defined cross-platform format</a>, it is often used
320 as a container for transferring content from one system to another.
321 The sender gathers content into an SQLite database file, transfers
322 that one file to the receiver, then the receiver uses SQL to extract
323 the content as needed.
324
325 </p><p>An SQLite database facilitates data transfer between systems even
326 when the endpoints have different word sizes and/or byte orders.
327 The data can be a complex mix of large binary blobs, text, and small
328 numeric or boolean values. The data format can be easily extended
329 by adding new tables and/or columns, without breaking legacy receivers.
330 The SQL query language means that receivers are not required to parse
331 the entire transfer all at once, but can instead query the
332 received content as needed. The data format is "transparent" in the
333 sense that it is easily decoded for human viewing using
334 a variety of universally available, open-source tools, from multiple
335 vendors.
336 </p></li>
337
338 <a name="container"></a>
339
340 <li><p><b>File archive and/or data container</b></p>
341
342 <p>
343 The <a href="sqlar.html">SQLite Archive</a> idea shows how
344 SQLite can be used as a substitute for ZIP archives or Tarballs.
345 An archive of files stored in SQLite is only very slightly larger, and
346 in some cases actually smaller, than the equivalent ZIP archive.
347 And an SQLite archive features incremental and atomic updating
348 and the ability to store much richer metadata.
349 </p>
350
351 <p><a href="https://www.fossil-scm.org/">Fossil</a> version 2.5 and later offers
352 <a href="sqlar.html">SQLite Archive files</a> as a download format, in addition
353 to traditional tarball and ZIP archive.
354 The <a href="cli.html">sqlite3.exe command-line shell</a> version 3.22.0 and later will create,
355 list, or unpack an SQL archiving using the
356 <a href="cli.html#sqlar">.archive command</a>.</p>
357
358 <p>
359 SQLite is a good solution for any situation that requires bundling
360 diverse content into a self-contained and self-describing package
361 for shipment across a network.
362 Content is encoded in a
363 <a href="fileformat2.html">well-defined, cross-platform, and stable file format</a>.
364 The encoding is efficient, and receivers can extract small subsets
365 of the content without having to read and parse the entire file.
366 </p>
367
368 <p>SQL archives are useful as the distribution format for software
369 or content updates that are broadcast to many clients. Variations
370 on this idea are used, for example, to transmit TV programming guides
371 to set-top boxes and to send over-the-air updates to vehicle navigation
372 systems.</p>
373 </li>
374
375 <li><p><b>Replacement for <i>ad hoc</i> disk files</b></p>
376
377 <p>Many programs use
378 <a href="http://man.he.net/man3/fopen">fopen()</a>,
379 <a href="http://man.he.net/man3/fread">fread()</a>, and
380 <a href="http://man.he.net/man3/fwrite">fwrite()</a> to create and
381 manage files of data in home-grown formats. SQLite works
382 particularly well as a
383 replacement for these <i>ad hoc</i> data files.
384 Contrary to intuition, SQLite can be <a href="fasterthanfs.html">faster than the filesystem</a>
385 for reading and writing content to disk.
386 </p></li>
387
388 <li><p><b>Internal or temporary databases</b></p>
389
390 <p>
391 For programs that have a lot of data that must be sifted and sorted
392 in diverse ways, it is often easier and quicker to load the data into
393 an in-memory SQLite database and use queries with joins and ORDER BY
394 clauses to extract the data in the form and order needed rather than
395 to try to code the same operations manually.
396 Using an SQL database internally in this way also gives the program
397 greater flexibility since new columns and indices can be added without
398 having to recode every query.
399 </p>
400 </li>
401
402 <li><p><b>Stand-in for an enterprise database during demos or testing</b></p>
403
404 <p>
405 Client applications typically use a generic database interface that allows
406 connections to various SQL database engines. It makes good sense to
407 include SQLite in the mix of supported databases and to statically
408 link the SQLite engine in with the client. That way the client program
409 can be used standalone with an SQLite data file for testing or for
410 demonstrations.
411 </p>
412 </li>
413
414 <li><p><b>Education and Training</b></p>
415
416 <p>
417 Because it is simple to setup and use (installation is trivial: just
418 copy the <b>sqlite3</b> or <b>sqlite3.exe</b> executable to the target machine
419 and run it) SQLite makes a good database engine for use in teaching SQL.
420 Students can easily create as many databases as they like and can
421 email databases to the instructor for comments or grading. For more
422 advanced students who are interested in studying how an RDBMS is
423 implemented, the modular and well-commented and documented SQLite code
424 can serve as a good basis.
425 </p>
426 </li>
427
428 <li><p><b>Experimental SQL language extensions</b></p>
429
430 <p>The simple, modular design of SQLite makes it a good platform for
431 prototyping new, experimental database language features or ideas.
432 </p>
433 </li>
434
435
436 </ul>
437
438 <h1 id="situations_where_a_client_server_rdbms_may_work_better"><span>2. </span>Situations Where A Client/Server RDBMS May Work Better</h1>
439
440 <ul>
441 <li><p><b>Client/Server Applications</b></p><p>
442
443 </p><p>If there are many client programs sending SQL to the same
444 database over a network, then use a client/server database
445 engine instead of SQLite. SQLite will work over a network filesystem,
446 but because of the latency associated with most network filesystems,
447 performance will not be great. Also, file locking logic is buggy in
448 many network filesystem implementations (on both Unix and Windows).
449 If file locking does not work correctly,
450 two or more clients might try to modify the
451 same part of the same database at the same time, resulting in
452 corruption. Because this problem results from bugs in
453 the underlying filesystem implementation, there is nothing SQLite
454 can do to prevent it.</p>
455
456 <p>A good rule of thumb is to avoid using SQLite
457 in situations where the same database will be accessed directly
458 (without an intervening application server) and simultaneously
459 from many computers over a network.</p>
460 </li>
461
462 <li><p><b>High-volume Websites</b></p>
463
464 <p>SQLite will normally work fine as the database backend to a website.
465 But if the website is write-intensive or is so busy that it requires
466 multiple servers, then consider using an enterprise-class client/server
467 database engine instead of SQLite.</p>
468 </li>
469
470 <li><p><b>Very large datasets</b></p>
471
472 <p>An SQLite database is limited in size to 281 terabytes
473 (2<sup><small>48</small></sup> bytes, 256 tibibytes).
474 And even if it could handle larger databases, SQLite stores the entire
475 database in a single disk file and many filesystems limit the maximum
476 size of files to something less than this. So if you are contemplating
477 databases of this magnitude, you would do well to consider using a
478 client/server database engine that spreads its content across multiple
479 disk files, and perhaps across multiple volumes.
480 </p>
481 </li>
482
483 <li><p><b>High Concurrency</b></p>
484
485 <p>
486 SQLite supports an unlimited number of simultaneous readers, but it
487 will only allow one writer at any instant in time.
488 For many situations, this is not a problem. Writers queue up. Each application
489 does its database work quickly and moves on, and no lock lasts for more
490 than a few dozen milliseconds. But there are some applications that require
491 more concurrency, and those applications may need to seek a different
492 solution.
493 </p>
494 </li>
495
496 </ul>
497
498 <a name="dbcklst"></a>
499
500 <h1 id="checklist_for_choosing_the_right_database_engine"><span>3. </span>Checklist For Choosing The Right Database Engine</h1>
501
502 <ol>
503 <li><p><b>Is the data separated from the application by a network?
504 &rarr; choose client/server</b></p>
505
506 <p>Relational database engines act as bandwidth-reducing data filters.
507 So it is best to keep the database engine and the data on
508 the same physical device so that the high-bandwidth engine-to-disk
509 link does not have to traverse the network, only the lower-bandwidth
510 application-to-engine link.
511
512 </p><p>But SQLite is built into the application. So if the data is on a
513 separate device from the application, it is required that the higher
514 bandwidth engine-to-disk link be across the network. This works, but
515 it is suboptimal. Hence, it is usually better to select a client/server
516 database engine when the data is on a separate device from the
517 application.
518
519 </p><p><em>Nota Bene:</em>
520 In this rule, "application" means the code that issues SQL statements.
521 If the "application" is an <a href="whentouse.html#serversidedb">application server</a> and
522 if the content resides on the same physical machine as the application server,
523 then SQLite might still be appropriate even though the end user is
524 another network hop away.</p>
525 </li>
526
527 <li><p><b>Many concurrent writers? &rarr; choose client/server</b></p>
528
529 <p>If many threads and/or processes need to write the
530 database at the same instant (and they cannot queue up and take turns)
531 then it is best to select a database engine that supports that
532 capability, which always means a client/server database engine.
533
534 </p><p>SQLite only supports one writer at a time per database file.
535 But in most cases, a write transaction only takes milliseconds and
536 so multiple writers can simply take turns. SQLite will handle
537 more write concurrency than many people suspect. Nevertheless,
538 client/server database systems, because they have a long-running
539 server process at hand to coordinate access, can usually handle
540 far more write concurrency than SQLite ever will.
541 </p></li>
542
543 <li><p><b>Big data? &rarr; choose client/server</b></p>
544
545 <p>If your data will grow to a size that you are uncomfortable
546 or unable to fit into a single disk file, then you should select
547 a solution other than SQLite. SQLite supports databases up to
548 281 terabytes in size, assuming you can find a disk drive and filesystem
549 that will support 281-terabyte files. Even so, when the size of the
550 content looks like it might creep into the terabyte range, it would
551 be good to consider a centralized client/server database.
552 </p></li>
553
554 <li><p><b>Otherwise &rarr; choose SQLite!</b></p>
555
556 <p>For device-local storage with low writer concurrency and less than a
557 terabyte of content, SQLite is almost always a better solution. SQLite
558 is fast and reliable and it requires no configuration or maintenance.
559 It keeps things simple. SQLite "just works".
560 </p></li>
561 </ol>
562 <p align="center"><small><i>This page last modified on <a href="https://sqlite.org/docsrc/honeypot" id="mtimelink" data-href="https://sqlite.org/docsrc/finfo/pages/whentouse.in?m=3425224a93">2024-04-03 17:48:26</a> UTC </small></i></p>
563