Debian Codegraph @

www/tempfiles.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>Temporary Files Used By 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 Temporary Files Used By 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="#introduction">1. Introduction</a></div>
129 <div class="fancy-toc1"><a href="#nine_kinds_of_temporary_files">2. Nine Kinds Of Temporary Files</a></div>
130 <div class="fancy-toc2"><a href="#rollback_journals">2.1. Rollback Journals</a></div>
131 <div class="fancy-toc2"><a href="#write_ahead_log_wal_files">2.2. Write-Ahead Log (WAL) Files</a></div>
132 <div class="fancy-toc2"><a href="#shared_memory_files">2.3. Shared-Memory Files</a></div>
133 <div class="fancy-toc2"><a href="#super_journal_files">2.4. Super-Journal Files</a></div>
134 <div class="fancy-toc2"><a href="#statement_journal_files">2.5. Statement Journal Files</a></div>
135 <div class="fancy-toc2"><a href="#temp_databases">2.6. TEMP Databases</a></div>
136 <div class="fancy-toc2"><a href="#materializations_of_views_and_subqueries">2.7. Materializations Of Views And Subqueries</a></div>
137 <div class="fancy-toc2"><a href="#transient_indices">2.8. Transient Indices</a></div>
138 <div class="fancy-toc2"><a href="#transient_database_used_by_vacuum_">2.9. Transient Database Used By <a href="lang_vacuum.html">VACUUM</a></a></div>
139 <div class="fancy-toc1"><a href="#the_sqlite_temp_store_compile_time_parameter_and_pragma">3. The SQLITE_TEMP_STORE Compile-Time Parameter and Pragma</a></div>
140 <div class="fancy-toc1"><a href="#other_temporary_file_optimizations">4. Other Temporary File Optimizations</a></div>
141 <div class="fancy-toc1"><a href="#temporary_file_storage_locations">5. Temporary File Storage Locations</a></div>
142 </div>
143 </div>
144 <script>
145 function toggle_toc(){
146 var sub = document.getElementById("toc_sub")
147 var mk = document.getElementById("toc_mk")
148 if( sub.style.display!="block" ){
149 sub.style.display = "block";
150 mk.innerHTML = "&#x25bc;";
151 } else {
152 sub.style.display = "none";
153 mk.innerHTML = "&#x25ba;";
154 }
155 }
156 </script>
157 </div>
158
159
160
161
162
163 <h1 id="introduction"><span>1. </span>Introduction</h1>
164
165 <p>
166 One of the <a href="different.html">distinctive features</a> of
167 SQLite is that a database consists of a single disk file.
168 This simplifies the use of SQLite since moving or backing up a
169 database is a simple as copying a single file. It also makes
170 SQLite appropriate for use as an
171 <a href="whentouse.html#appfileformat">application file format</a>.
172 But while a complete database is held in a single disk file,
173 SQLite does make use of many temporary files during the
174 course of processing a database.
175 </p>
176
177 <p>
178 This article describes the various temporary files that SQLite
179 creates and uses. It describes when the files are created, when
180 they are deleted, what they are used for, why they are important,
181 and how to avoid them on systems where creating temporary files is
182 expensive.
183 </p>
184
185 <p>
186 The manner in which SQLite uses temporary files is not considered
187 part of the contract that SQLite makes with applications. The
188 information in this document is a correct description of how
189 SQLite operates at the time that this document was written or last
190 updated. But there is no guarantee that future versions of SQLite
191 will use temporary files in the same way. New kinds of temporary
192 files might be employed and some of
193 the current temporary file uses might be discontinued
194 in future releases of SQLite.
195 </p>
196
197 <a name="types"></a>
198
199 <h1 id="nine_kinds_of_temporary_files"><span>2. </span>Nine Kinds Of Temporary Files</h1>
200
201 <p>
202 SQLite currently uses nine distinct types of temporary files:
203 </p>
204
205 <ol>
206 <li>Rollback journals</li>
207 <li>Super-journals</li>
208 <li>Write-ahead Log (WAL) files</li>
209 <li>Shared-memory files</li>
210 <li>Statement journals</li>
211 <li>TEMP databases</li>
212 <li>Materializations of views and subqueries</li>
213 <li>Transient indices</li>
214 <li>Transient databases used by VACUUM</li>
215 </ol>
216
217 <p>
218 Additional information about each of these temporary file types
219 is in the sequel.
220 </p>
221
222 <a name="rollbackjrnl"></a>
223
224 <h2 id="rollback_journals"><span>2.1. </span>Rollback Journals</h2>
225
226 <p>
227 A rollback journal is a temporary file used to implement
228 atomic commit and rollback capabilities in SQLite.
229 (For a detailed discussion of how this works, see
230 the separate document titled
231 <a href="atomiccommit.html">Atomic Commit In SQLite</a>.)
232 The rollback journal is always located in the same directory
233 as the database file and has the same name as the database
234 file except with the 8 characters "<b>-journal</b>" appended.
235 The rollback journal is usually created when a transaction
236 is first started and is usually deleted when a transaction
237 commits or rolls back.
238 The rollback journal file is essential for implementing the
239 atomic commit and rollback capabilities of SQLite. Without
240 a rollback journal, SQLite would be unable to rollback an
241 incomplete transaction, and if a crash or power loss occurred
242 in the middle of a transaction the entire database would likely
243 go corrupt without a rollback journal.
244 </p>
245
246 <p>
247 The rollback journal is <i>usually</i> created and destroyed at the
248 start and end of a transaction, respectively. But there are exceptions
249 to this rule.
250 </p>
251
252 <p>
253 If a crash or power loss occurs in the middle of a transaction,
254 then the rollback journal file is left on disk. The next time
255 another application attempts to open the database file, it notices
256 the presence of the abandoned rollback journal (we call it a "hot
257 journal" in this circumstance) and uses the information in the
258 journal to restore the database to its state prior to the start
259 of the incomplete transaction. This is how SQLite implements
260 atomic commit.
261 </p>
262
263 <p>
264 If an application puts SQLite in
265 <a href="pragma.html#pragma_locking_mode">exclusive locking mode</a> using
266 the pragma:
267 </p>
268
269 <blockquote><pre>
270 PRAGMA locking_mode=EXCLUSIVE;
271 </pre></blockquote>
272
273 <p>
274 SQLite creates a new rollback journal at the start of the first
275 transaction within an exclusive locking mode session. But at the
276 conclusion of the transaction, it does not delete the rollback
277 journal. The rollback journal might be truncated, or its header
278 might be zeroed (depending on what version of SQLite you are using)
279 but the rollback journal is not deleted. The rollback journal is
280 not deleted until exclusive access mode is exited.</p>
281
282 <p>
283 Rollback journal creation and deletion is also changed by the
284 <a href="pragma.html#pragma_journal_mode">journal_mode pragma</a>.
285 The default journaling mode is DELETE, which is the default behavior
286 of deleting the rollback journal file at the end of each transaction,
287 as described above. The PERSIST journal mode foregoes the deletion of
288 the journal file and instead overwrites the rollback journal header
289 with zeros, which prevents other processes from rolling back the
290 journal and thus has the same effect as deleting the journal file, though
291 without the expense of actually removing the file from disk. In other
292 words, journal mode PERSIST exhibits the same behavior as is seen
293 in EXCLUSIVE locking mode. The
294 OFF journal mode causes SQLite to omit the rollback journal, completely.
295 In other words, no rollback journal is ever written if journal mode is
296 set to OFF.
297 The OFF journal mode disables the atomic
298 commit and rollback capabilities of SQLite. The ROLLBACK command
299 is not available when OFF journal mode is set. And if a crash or power
300 loss occurs in the middle of a transaction that uses the OFF journal
301 mode, no recovery is possible and the database file will likely
302 go corrupt.
303 The MEMORY journal mode causes the rollback journal to be stored in
304 memory rather than on disk. The ROLLBACK command still works when
305 the journal mode is MEMORY, but because no file exists on disks for
306 recovery, a crash or power loss in the middle of a transaction that uses
307 the MEMORY journal mode will likely result in a corrupt database.
308 </p>
309
310 <a name="walfile"></a>
311
312 <h2 id="write_ahead_log_wal_files"><span>2.2. </span>Write-Ahead Log (WAL) Files</h2>
313
314 <p>
315 A write-ahead log or WAL file is used in place of a rollback journal
316 when SQLite is operating in <a href="wal.html">WAL mode</a>. As with the rollback journal,
317 the purpose of the WAL file is to implement atomic commit and rollback.
318 The WAL file is always located in the same directory
319 as the database file and has the same name as the database
320 file except with the 4 characters "<b>-wal</b>" appended.
321 The WAL file is created when the first connection to the
322 database is opened and is normally removed when the last
323 connection to the database closes. However, if the last connection
324 does not shutdown cleanly, the WAL file will remain in the filesystem
325 and will be automatically cleaned up the next time the database is
326 opened.
327 </p>
328
329 <a name="shmfile"></a>
330
331 <h2 id="shared_memory_files"><span>2.3. </span>Shared-Memory Files</h2>
332
333 <p>
334 When operating in <a href="wal.html">WAL mode</a>, all SQLite database connections associated
335 with the same database file need to share some memory that is used as an
336 index for the WAL file. In most implementations, this shared memory is
337 implemented by calling mmap() on a file created for this sole purpose:
338 the shared-memory file. The shared-memory file, if it exists, is located
339 in the same directory as the database file and has the same name as the
340 database file except with the 4 characters "<b>-shm</b>" appended.
341 Shared memory files only exist while running in WAL mode.
342 </p>
343
344 <p>
345 The shared-memory file contains no persistent content. The only purpose
346 of the shared-memory file is to provide a block of shared memory for use
347 by multiple processes all accessing the same database in WAL mode.
348 If the <a href="vfs.html">VFS</a> is able to provide an alternative method for accessing shared
349 memory, then that alternative method might be used rather than the
350 shared-memory file. For example, if <a href="pragma.html#pragma_locking_mode">PRAGMA locking_mode</a> is set to
351 EXCLUSIVE (meaning that only one process is able to access the database
352 file) then the shared memory will be allocated from heap rather than out
353 of the shared-memory file, and the shared-memory file will never be
354 created.
355 </p>
356
357 <p>
358 The shared-memory file has the same lifetime as its associated WAL file.
359 The shared-memory file is created when the WAL file is created and is
360 deleted when the WAL file is deleted. During WAL file recovery, the
361 shared memory file is recreated from scratch based on the contents of
362 the WAL file being recovered.
363 </p>
364
365 <a name="superjrnl"></a>
366
367 <h2 id="super_journal_files"><span>2.4. </span>Super-Journal Files</h2>
368
369 <p>
370 The super-journal file is used as part of the atomic commit
371 process when a single transaction makes changes to multiple
372 databases that have been added to a single <a href="c3ref/sqlite3.html">database connection</a>
373 using the <a href="lang_attach.html">ATTACH</a> statement. The super-journal file is always
374 located in the same directory as the main database file
375 (the main database file is the database that is identified
376 in the original <a href="c3ref/open.html">sqlite3_open()</a>, <a href="c3ref/open.html">sqlite3_open16()</a>, or
377 <a href="c3ref/open.html">sqlite3_open_v2()</a> call that created the <a href="c3ref/sqlite3.html">database connection</a>)
378 with a randomized suffix. The super-journal file contains
379 the names of all of the various attached auxiliary databases
380 that were changed during the transaction. The multi-database
381 transaction commits when the super-journal file is deleted.
382 See the documentation titled
383 <a href="atomiccommit.html">Atomic Commit In SQLite</a> for
384 additional detail.
385 </p>
386
387 <p>
388 Without the super-journal, the transaction commit on a multi-database
389 transaction would be atomic for each database individually, but it
390 would not be atomic across all databases. In other words, if the
391 commit were interrupted in the middle by a crash or power loss, then
392 the changes to one of the databases might complete while the changes
393 to another database might roll back. The super-journal causes all
394 changes in all databases to either rollback or commit together.
395 </p>
396
397 <p>
398 The super-journal file is only created for <a href="lang_transaction.html">COMMIT</a> operations that
399 involve multiple database files where at least two of the databases
400 meet all of the following requirements:
401
402 </p><ol>
403 <li>The database is modified by the transaction
404 </li><li>The <a href="pragma.html#pragma_synchronous">PRAGMA synchronous</a> setting is not OFF
405 </li><li>The <a href="pragma.html#pragma_journal_mode">PRAGMA journal_mode</a> is not OFF, MEMORY, or WAL
406 </li></ol>
407
408 <p>
409 This means that SQLite transactions are not atomic
410 across multiple database files on a power-loss when the database
411 files have synchronous turned off or when they are using journal
412 modes of OFF, MEMORY, or WAL. For synchronous OFF and for
413 journal_modes OFF and MEMORY, database will usually corrupt if
414 a transaction commit is interrupted by a power loss. For
415 <a href="wal.html">WAL mode</a>, individual database files are updated atomically
416 across a power-loss, but in the case of a multi-file transactions,
417 some files might rollback while others roll forward after
418 power is restored.
419 </p>
420
421
422 <a name="stmtjrnl"></a>
423
424 <h2 id="statement_journal_files"><span>2.5. </span>Statement Journal Files</h2>
425
426 <p>
427 A statement journal file is used to rollback partial results of
428 a single statement within a larger transaction. For example, suppose
429 an UPDATE statement will attempt to modify 100 rows in the database.
430 But after modifying the first 50 rows, the UPDATE hits
431 a constraint violation which should block the entire statement.
432 The statement journal is used to undo the first 50 row changes
433 so that the database is restored to the state it was in at the start
434 of the statement.
435 </p>
436
437 <p>
438 A statement journal is only created for an UPDATE or INSERT statement
439 that might change multiple rows of a database and which might hit a
440 constraint or a RAISE exception within a trigger and thus need to
441 undo partial results.
442 If the UPDATE or INSERT is not contained within BEGIN...COMMIT and if
443 there are no other active statements on the same database connection then
444 no statement journal is created since the ordinary
445 rollback journal can be used instead.
446 The statement journal is also omitted if an alternative
447 <a href="lang_conflict.html">conflict resolution algorithm</a> is
448 used. For example:
449 </p>
450
451 <blockquote><pre>
452 UPDATE OR FAIL ...
453 UPDATE OR IGNORE ...
454 UPDATE OR REPLACE ...
455 UPDATE OR ROLLBACK ...
456 INSERT OR FAIL ...
457 INSERT OR IGNORE ...
458 INSERT OR REPLACE ...
459 INSERT OR ROLLBACK ...
460 REPLACE INTO ....
461 </pre></blockquote>
462
463 <p>
464 The statement journal is given a randomized name, not necessarily
465 in the same directory as the main database, and is automatically
466 deleted at the conclusion of the transaction. The size of the
467 statement journal is proportional to the size of the change implemented
468 by the UPDATE or INSERT statement that caused the statement journal
469 to be created.
470 </p>
471
472 <a name="tempdb"></a>
473
474 <h2 id="temp_databases"><span>2.6. </span>TEMP Databases</h2>
475
476 <p>Tables created using the "CREATE TEMP TABLE" syntax are only
477 visible to the <a href="c3ref/sqlite3.html">database connection</a> in which the "CREATE TEMP TABLE"
478 statement is originally evaluated. These TEMP tables, together
479 with any associated indices, triggers, and views, are collectively
480 stored in a separate temporary database file that is created as
481 soon as the first "CREATE TEMP TABLE" statement is seen.
482 This separate temporary database file also has an associated
483 rollback journal.
484 The temporary database file used to store TEMP tables is deleted
485 automatically when the <a href="c3ref/sqlite3.html">database connection</a> is closed
486 using <a href="c3ref/close.html">sqlite3_close()</a>.
487 </p>
488
489 <p>
490 The TEMP database file is very similar to auxiliary database
491 files added using the <a href="lang_attach.html">ATTACH</a> statement, though with a few
492 special properties.
493 The TEMP database is always automatically deleted when the
494 <a href="c3ref/sqlite3.html">database connection</a> is closed.
495 The TEMP database always uses the
496 <a href="pragma.html#pragma_synchronous">synchronous=OFF</a> and <a href="pragma.html#pragma_journal_mode">journal_mode=PERSIST</a>
497 PRAGMA settings.
498 And, the TEMP database cannot be used with <a href="lang_detach.html">DETACH</a> nor can
499 another process <a href="lang_attach.html">ATTACH</a> the TEMP database.
500 </p>
501
502 <p>
503 The temporary files associated with the TEMP database and its
504 rollback journal are only created if the application makes use
505 of the "CREATE TEMP TABLE" statement.
506 </p>
507
508 <a name="views"></a>
509
510 <h2 id="materializations_of_views_and_subqueries"><span>2.7. </span>Materializations Of Views And Subqueries</h2>
511
512 <p>Queries that contain subqueries must sometime evaluate
513 the subqueries separately and store the results in a temporary
514 table, then use the content of the temporary table to evaluate
515 the outer query.
516 We call this "materializing" the subquery.
517 The query optimizer in SQLite attempts to avoid materializing,
518 but sometimes it is not easily avoidable.
519 The temporary tables created by materialization are each stored
520 in their own separate temporary file, which is automatically
521 deleted at the conclusion of the query.
522 The size of these temporary tables depends on the amount of
523 data in the materialization of the subquery, of course.
524 </p>
525
526 <p>
527 A subquery on the right-hand side of IN operator must often
528 be materialized. For example:
529 </p>
530
531 <blockquote><pre>
532 SELECT * FROM ex1 WHERE ex1.a IN (SELECT b FROM ex2);
533 </pre></blockquote>
534
535 <p>
536 In the query above, the subquery "SELECT b FROM ex2" is evaluated
537 and its results are stored in a temporary table (actually a temporary
538 index) that allows one to determine whether or not a value ex2.b
539 exists using a simple binary search. Once this table is constructed,
540 the outer query is run and for each prospective result row a check
541 is made to see if ex1.a is contained within the temporary table.
542 The row is output only if the check is true.
543 </p>
544
545 <p>
546 To avoid creating the temporary table, the query might be rewritten
547 as follows:
548 </p>
549
550 <blockquote><pre>
551 SELECT * FROM ex1 WHERE EXISTS(SELECT 1 FROM ex2 WHERE ex2.b=ex1.a);
552 </pre></blockquote>
553
554 <p>
555 Recent versions of SQLite (<a href="releaselog/3_5_4.html">version 3.5.4</a> 2007-12-14) and later)
556 will do this rewrite automatically
557 if an index exists on the column ex2.b.
558 </p>
559
560 <p>
561 If the right-hand side of an IN operator can be list of values
562 as in the following:
563 </p>
564 <blockquote><pre>
565 SELECT * FROM ex1 WHERE a IN (1,2,3);
566 </pre></blockquote>
567 <p>
568 List values on the right-hand side of IN are treated as a
569 subquery that must be materialized. In other words, the
570 previous statement acts as if it were:
571 </p>
572 <blockquote><pre>
573 SELECT * FROM ex1 WHERE a IN (SELECT 1 UNION ALL
574 SELECT 2 UNION ALL
575 SELECT 3);
576 </pre></blockquote>
577 <p>
578 A temporary index is always used to hold the values of the
579 right-hand side of an IN operator when that right-hand side
580 is a list of values.
581 </p>
582
583 <p>
584 Subqueries might also need to be materialized when they appear
585 in the FROM clause of a SELECT statement. For example:
586 </p>
587
588 <blockquote><pre>
589 SELECT * FROM ex1 JOIN (SELECT b FROM ex2) AS t ON t.b=ex1.a;
590 </pre></blockquote>
591
592 <p>
593 Depending on the query, SQLite might need to materialize the
594 "(SELECT b FROM ex2)" subquery into a temporary table, then
595 perform the join between ex1 and the temporary table. The
596 query optimizer tries to avoid this by "flattening" the
597 query. In the previous example the query can be flattened,
598 and SQLite will automatically transform the query into
599 </p>
600
601 <blockquote><pre>
602 SELECT ex1.*, ex2.b FROM ex1 JOIN ex2 ON ex2.b=ex1.a;
603 </pre></blockquote>
604
605 <p>
606 More complex queries may or may not be able to employ query
607 flattening to avoid the temporary table. Whether or not
608 the query can be flattened depends on such factors as whether
609 or not the subquery or outer query contain aggregate functions,
610 ORDER BY or GROUP BY clauses, LIMIT clauses, and so forth.
611 The rules for when a query can and cannot be flattened are
612 very complex and are beyond the scope of this document.
613 </p>
614
615 <a name="transidx"></a>
616
617 <h2 id="transient_indices"><span>2.8. </span>Transient Indices</h2>
618
619 <p>
620 SQLite may make use of transient indices to
621 implement SQL language features such as:
622 </p>
623
624 <ul>
625 <li>An ORDER BY or GROUP BY clause</li>
626 <li>The DISTINCT keyword in an aggregate query</li>
627 <li>Compound SELECT statements joined by UNION, EXCEPT, or INTERSECT</li>
628 </ul>
629
630 <p>
631 Each transient index is stored in its own temporary file.
632 The temporary file for a transient index is automatically deleted
633 at the end of the statement that uses it.
634 </p>
635
636 <p>
637 SQLite strives to implement ORDER BY clauses using a preexisting
638 index. If an appropriate index already exists, SQLite will walk
639 the index, rather than the underlying table, to extract the
640 requested information, and thus cause the rows to come out in
641 the desired order. But if SQLite cannot find an appropriate index
642 it will evaluate the query and store each row in a transient index
643 whose data is the row data and whose key is the ORDER BY terms.
644 After the query is evaluated, SQLite goes back and walks the
645 transient index from beginning to end in order to output the
646 rows in the desired order.
647 </p>
648
649 <p>
650 SQLite implements GROUP BY by ordering the output rows in the
651 order suggested by the GROUP BY terms. Each output row is
652 compared to the previous to see if it starts a new "group".
653 The ordering by GROUP BY terms is done in exactly the same way
654 as the ordering by ORDER BY terms. A preexisting index is used
655 if possible, but if no suitable index is available, a transient
656 index is created.
657 </p>
658
659 <p>
660 The DISTINCT keyword on an aggregate query is implemented by
661 creating a transient index in a temporary file and storing
662 each result row in that index. As new result rows are computed
663 a check is made to see if they already exist in the transient
664 index and if they do the new result row is discarded.
665 </p>
666
667 <p>
668 The UNION operator for compound queries is implemented by creating
669 a transient index in a temporary file and storing the results
670 of the left and right subquery in the transient index, discarding
671 duplicates. After both subqueries have been evaluated, the
672 transient index is walked from beginning to end to generate the final output.
673 </p>
674
675 <p>
676 The EXCEPT operator for compound queries is implemented by creating
677 a transient index in a temporary file, storing the results of the
678 left subquery in this transient index, then removing the result
679 from right subquery from the transient index, and finally walking
680 the index from beginning to end to obtain the final output.
681 </p>
682
683 <p>
684 The INTERSECT operator for compound queries is implemented by
685 creating two separate transient indices, each in a separate
686 temporary file. The left and right subqueries are evaluated
687 each into a separate transient index. Then the two indices
688 are walked together and entries that appear in both indices
689 are output.
690 </p>
691
692 <p>
693 Note that the UNION ALL operator for compound queries does not
694 use transient indices by itself (though of course the right
695 and left subqueries of the UNION ALL might use transient indices
696 depending on how they are composed.)
697
698 <a name="vacuumdb"></a>
699
700 </p><h2 id="transient_database_used_by_vacuum_"><span>2.9. </span>Transient Database Used By <a href="lang_vacuum.html">VACUUM</a></h2>
701
702 <p>
703 The <a href="lang_vacuum.html">VACUUM</a> command works by creating a temporary file
704 and then rebuilding the entire database into that temporary
705 file. Then the content of the temporary file is copied back
706 into the original database file and the temporary file is
707 deleted.
708 </p>
709
710 <p>
711 The temporary file created by the <a href="lang_vacuum.html">VACUUM</a> command exists only
712 for the duration of the command itself. The size of the temporary
713 file will be no larger than the original database.
714 </p>
715
716 <a name="tempstore"></a>
717
718 <h1 id="the_sqlite_temp_store_compile_time_parameter_and_pragma"><span>3. </span>The SQLITE_TEMP_STORE Compile-Time Parameter and Pragma</h1>
719
720 <p>
721 The temporary files associated with transaction control, namely
722 the rollback journal, super-journal, write-ahead log (WAL) files,
723 and shared-memory files, are always written to disk.
724 But the other kinds of temporary files might be stored in memory
725 only and never written to disk.
726 Whether or not temporary files other than the rollback,
727 super, and statement journals are written to disk or stored only in memory
728 depends on the <a href="compile.html#temp_store">SQLITE_TEMP_STORE</a> compile-time parameter, the
729 <a href="pragma.html#pragma_temp_store">temp_store pragma</a>,
730 and on the size of the temporary file.
731 </p>
732
733 <p>
734 The <a href="compile.html#temp_store">SQLITE_TEMP_STORE</a> compile-time parameter is a #define whose value is
735 an integer between 0 and 3, inclusive. The meaning of the
736 <a href="compile.html#temp_store">SQLITE_TEMP_STORE</a> compile-time parameter is as follows:
737 </p>
738
739 <ol type="1">
740 <li value="0">
741 Temporary files are always stored on disk regardless of the setting
742 of the <a href="pragma.html#pragma_temp_store">temp_store pragma</a>.
743 </li>
744 <li value="1">
745 Temporary files are stored on disk by default but this can be
746 overridden by the <a href="pragma.html#pragma_temp_store">temp_store pragma</a>.
747 </li>
748 <li value="2">
749 Temporary files are stored in memory by default but this can be
750 overridden by the <a href="pragma.html#pragma_temp_store">temp_store pragma</a>.
751 </li>
752 <li value="3">
753 Temporary files are always stored in memory regardless of the setting
754 of the <a href="pragma.html#pragma_temp_store">temp_store pragma</a>.
755 </li>
756 </ol>
757
758 <p>
759 The default value of the <a href="compile.html#temp_store">SQLITE_TEMP_STORE</a> compile-time parameter is 1,
760 which means to store temporary files on disk but provide the option
761 of overriding the behavior using the <a href="pragma.html#pragma_temp_store">temp_store pragma</a>.
762 </p>
763
764 <p>
765 The <a href="pragma.html#pragma_temp_store">temp_store pragma</a> has
766 an integer value which also influences the decision of where to store
767 temporary files. The values of the temp_store pragma have the
768 following meanings:
769 </p>
770
771 <ol type="1">
772 <li value="0">
773 Use either disk or memory storage for temporary files as determined
774 by the <a href="compile.html#temp_store">SQLITE_TEMP_STORE</a> compile-time parameter.
775 </li>
776 <li value="1">
777 If the <a href="compile.html#temp_store">SQLITE_TEMP_STORE</a> compile-time parameter specifies memory storage for
778 temporary files, then override that decision and use disk storage instead.
779 Otherwise follow the recommendation of the <a href="compile.html#temp_store">SQLITE_TEMP_STORE</a> compile-time
780 parameter.
781 </li>
782 <li value="2">
783 If the <a href="compile.html#temp_store">SQLITE_TEMP_STORE</a> compile-time parameter specifies disk storage for
784 temporary files, then override that decision and use memory storage instead.
785 Otherwise follow the recommendation of the <a href="compile.html#temp_store">SQLITE_TEMP_STORE</a> compile-time
786 parameter.
787 </li>
788 </ol>
789
790 <p>
791 The default setting for the <a href="pragma.html#pragma_temp_store">temp_store pragma</a> is 0,
792 which means to following the recommendation of <a href="compile.html#temp_store">SQLITE_TEMP_STORE</a> compile-time
793 parameter.
794 </p>
795
796 <p>
797 To reiterate, the <a href="compile.html#temp_store">SQLITE_TEMP_STORE</a> compile-time parameter and the
798 <a href="pragma.html#pragma_temp_store">temp_store pragma</a> only
799 influence the temporary files other than the rollback journal
800 and the super-journal. The rollback journal and the
801 super-journal are always written to disk regardless of the settings of
802 the <a href="compile.html#temp_store">SQLITE_TEMP_STORE</a> compile-time parameter and the
803 <a href="pragma.html#pragma_temp_store">temp_store pragma</a>.
804 </p>
805
806 <a name="otheropt"></a>
807
808 <h1 id="other_temporary_file_optimizations"><span>4. </span>Other Temporary File Optimizations</h1>
809
810 <p>
811 SQLite uses a page cache of recently read and written database
812 pages. This page cache is used not just for the main database
813 file but also for transient indices and tables stored in temporary
814 files. If SQLite needs to use a temporary index or table and
815 the <a href="compile.html#temp_store">SQLITE_TEMP_STORE</a> compile-time parameter and the
816 <a href="pragma.html#pragma_temp_store">temp_store pragma</a> are
817 set to store temporary tables and index on disk, the information
818 is still initially stored in memory in the page cache. The
819 temporary file is not opened and the information is not truly
820 written to disk until the page cache is full.
821 </p>
822
823 <p>
824 This means that for many common cases where the temporary tables
825 and indices are small (small enough to fit into the page cache)
826 no temporary files are created and no disk I/O occurs. Only
827 when the temporary data becomes too large to fit in RAM does
828 the information spill to disk.
829 </p>
830
831 <p>
832 Each temporary table and index is given its own page cache
833 which can store a maximum number of database pages determined
834 by the SQLITE_DEFAULT_TEMP_CACHE_SIZE compile-time parameter.
835 (The default value is 500 pages.)
836 The maximum number of database pages in the page cache is the
837 same for every temporary table and index. The value cannot
838 be changed at run-time or on a per-table or per-index basis.
839 Each temporary file gets its own private page cache with its
840 own SQLITE_DEFAULT_TEMP_CACHE_SIZE page limit.
841 </p>
842
843 <a name="tempdir"></a>
844
845 <h1 id="temporary_file_storage_locations"><span>5. </span>Temporary File Storage Locations</h1>
846
847 <p>
848 The directory or folder in which temporary files are created is
849 determined by the OS-specific <a href="vfs.html">VFS</a>.
850
851 </p><p>
852 On unix-like systems, directories are searched in the following order:
853 </p><ol>
854 <li>The directory set by <a href="pragma.html#pragma_temp_store_directory">PRAGMA temp_store_directory</a> or by the
855 <a href="c3ref/temp_directory.html">sqlite3_temp_directory</a> global variable
856 </li><li>The SQLITE_TMPDIR environment variable
857 </li><li>The TMPDIR environment variable
858 </li><li>/var/tmp
859 </li><li>/usr/tmp
860 </li><li>/tmp
861 </li><li>The current working directory (".")
862 </li></ol>
863 The first of the above that is found to exist and have the write and
864 execute bits set is used. The final "." fallback is important for some
865 applications that use SQLite inside of chroot jails that do not have
866 the standard temporary file locations available.
867
868 <p>
869 On Windows systems, folders are searched in the following order:
870 </p><ol>
871 <li>The folder set by <a href="pragma.html#pragma_temp_store_directory">PRAGMA temp_store_directory</a> or by the
872 <a href="c3ref/temp_directory.html">sqlite3_temp_directory</a> global variable
873 </li><li>The folder returned by the GetTempPath() system interface.
874 </li></ol>
875 SQLite itself does not pay any attention to environment variables
876 in this case, though presumably the GetTempPath() system call does.
877 The search algorithm is different for CYGWIN builds. Check the
878 source code for details.
879 <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/tempfiles.in?m=cd7bffb031">2022-01-08 05:02:57</a> UTC </small></i></p>
880