| 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>ANALYZE</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 |
ANALYZE |
| 122 |
</div> |
| 123 |
<div class="fancy_toc"> |
| 124 |
<a onclick="toggle_toc()"> |
| 125 |
<span class="fancy_toc_mark" id="toc_mk">►</span> |
| 126 |
Table Of Contents |
| 127 |
</a> |
| 128 |
<div id="toc_sub"><div class="fancy-toc1"><a href="#overview">1. Overview</a></div> |
| 129 |
<div class="fancy-toc1"><a href="#recommended_usage_patterns">2. Recommended usage patterns</a></div> |
| 130 |
<div class="fancy-toc2"><a href="#periodically_run_pragma_optimize_">2.1. Periodically run "PRAGMA optimize"</a></div> |
| 131 |
<div class="fancy-toc2"><a href="#fixed_results_of_analyze">2.2. Fixed results of ANALYZE</a></div> |
| 132 |
<div class="fancy-toc1"><a href="#details">3. Details</a></div> |
| 133 |
<div class="fancy-toc1"><a href="#automatically_running_analyze">4. Automatically Running ANALYZE</a></div> |
| 134 |
<div class="fancy-toc1"><a href="#approximate_analyze_for_large_databases">5. Approximate ANALYZE For Large Databases</a></div> |
| 135 |
<div class="fancy-toc2"><a href="#limitations_of_approximate_analyze">5.1. Limitations of approximate ANALYZE</a></div> |
| 136 |
</div> |
| 137 |
</div> |
| 138 |
<script> |
| 139 |
function toggle_toc(){ |
| 140 |
var sub = document.getElementById("toc_sub") |
| 141 |
var mk = document.getElementById("toc_mk") |
| 142 |
if( sub.style.display!="block" ){ |
| 143 |
sub.style.display = "block"; |
| 144 |
mk.innerHTML = "▼"; |
| 145 |
} else { |
| 146 |
sub.style.display = "none"; |
| 147 |
mk.innerHTML = "►"; |
| 148 |
} |
| 149 |
} |
| 150 |
</script> |
| 151 |
</div> |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
<h1 id="overview"><span>1. </span>Overview</h1> |
| 157 |
|
| 158 |
<p><b><a href="syntax/analyze-stmt.html">analyze-stmt:</a></b> |
| 159 |
<button id='x3115000d' onclick='hideorshow("x3115000d","x4b619059")'>hide</button></p> |
| 160 |
<div id='x4b619059' class='imgcontainer'> |
| 161 |
<div style="max-width:654px"><svg xmlns='http://www.w3.org/2000/svg' class="pikchr" viewBox="0 0 654.557 140.4"> |
| 162 |
<circle cx="5" cy="17" r="3.6" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 163 |
<polygon points="32,17 20,21 20,12" style="fill:rgb(0,0,0)"/> |
| 164 |
<path d="M9,17L26,17" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 165 |
<path d="M47,32L111,32A15 15 0 0 0 127 17A15 15 0 0 0 111 2L47,2A15 15 0 0 0 32 17A15 15 0 0 0 47 32Z" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 166 |
<text x="79" y="17" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">ANALYZE</text> |
| 167 |
<polygon points="150,17 138,21 138,12" style="fill:rgb(0,0,0)"/> |
| 168 |
<path d="M127,17L144,17" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 169 |
<polygon points="188,123 176,127 176,118" style="fill:rgb(0,0,0)"/> |
| 170 |
<path d="M150,17 L 157,17 Q 165,17 165,32 L 165,108 Q 165,123 173,123 L 182,123" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 171 |
<path d="M203,138L299,138A15 15 0 0 0 314 123A15 15 0 0 0 299 108L203,108A15 15 0 0 0 188 123A15 15 0 0 0 203 138Z" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 172 |
<text x="251" y="123" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">schema-name</text> |
| 173 |
<polygon points="337,123 326,127 326,118" style="fill:rgb(0,0,0)"/> |
| 174 |
<path d="M314,123L332,123" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 175 |
<path d="M352,138A15 15 0 0 0 368 123A15 15 0 0 0 352 108A15 15 0 0 0 337 123A15 15 0 0 0 352 138Z" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 176 |
<text x="352" y="123" text-anchor="middle" font-weight="bold" fill="rgb(0,0,0)" dominant-baseline="central">.</text> |
| 177 |
<polygon points="391,123 379,127 379,118" style="fill:rgb(0,0,0)"/> |
| 178 |
<path d="M368,123L385,123" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 179 |
<path d="M406,138L556,138A15 15 0 0 0 571 123A15 15 0 0 0 556 108L406,108A15 15 0 0 0 391 123A15 15 0 0 0 406 138Z" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 180 |
<text x="481" y="123" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">table-or-index-name</text> |
| 181 |
<polygon points="594,123 582,127 582,118" style="fill:rgb(0,0,0)"/> |
| 182 |
<path d="M571,123L588,123" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 183 |
<polygon points="645,17 633,21 633,12" style="fill:rgb(0,0,0)"/> |
| 184 |
<path d="M594,123 L 601,123 Q 609,123 609,108 L 609,32 Q 609,17 624,17 L 624,17 L 639,17" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 185 |
<circle cx="648" cy="17" r="3.6" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 186 |
<polygon points="397,17 386,21 386,12" style="fill:rgb(0,0,0)"/> |
| 187 |
<path d="M150,17L391,17" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 188 |
<path d="M397,17L633,17" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 189 |
<polygon points="188,47 176,51 176,43" style="fill:rgb(0,0,0)"/> |
| 190 |
<path d="M150,17 L 157,17 Q 165,17 165,32 L 165,32 Q 165,47 173,47 L 182,47" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 191 |
<path d="M203,62L299,62A15 15 0 0 0 314 47L314,47A15 15 0 0 0 299 32L203,32A15 15 0 0 0 188 47L188,47A15 15 0 0 0 203 62Z" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 192 |
<text x="251" y="47" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">schema-name</text> |
| 193 |
<polygon points="594,47 582,51 582,43" style="fill:rgb(0,0,0)"/> |
| 194 |
<path d="M314,47L588,47" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 195 |
<path d="M594,47 L 601,47 Q 609,47 609,40 L 609,32" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 196 |
<polygon points="188,85 176,89 176,81" style="fill:rgb(0,0,0)"/> |
| 197 |
<path d="M150,17 L 157,17 Q 165,17 165,32 L 165,70 Q 165,85 173,85 L 182,85" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 198 |
<path d="M203,100L353,100A15 15 0 0 0 368 85A15 15 0 0 0 353 70L203,70A15 15 0 0 0 188 85A15 15 0 0 0 203 100Z" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 199 |
<text x="278" y="85" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">index-or-table-name</text> |
| 200 |
<polygon points="594,85 582,89 582,81" style="fill:rgb(0,0,0)"/> |
| 201 |
<path d="M368,85L588,85" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 202 |
<path d="M594,85 L 601,85 Q 609,85 609,77 L 609,70" style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" /> |
| 203 |
</svg> |
| 204 |
</div> |
| 205 |
</div> |
| 206 |
|
| 207 |
|
| 208 |
<p> The ANALYZE command gathers statistics about tables and |
| 209 |
indices and stores the collected information |
| 210 |
in <a href="fileformat2.html#intschema">internal tables</a> of the database where the query optimizer can |
| 211 |
access the information and use it to help make better query planning choices. |
| 212 |
If no arguments are given, the main database and all attached databases are |
| 213 |
analyzed. If a schema name is given as the argument, then all tables |
| 214 |
and indices in that one database are analyzed. |
| 215 |
If the argument is a table name, then only that table and the |
| 216 |
indices associated with that table are analyzed. If the argument |
| 217 |
is an index name, then only that one index is analyzed.</p> |
| 218 |
|
| 219 |
<a name="req"></a> |
| 220 |
|
| 221 |
<h1 id="recommended_usage_patterns"><span>2. </span>Recommended usage patterns</h1> |
| 222 |
|
| 223 |
<p> The use of ANALYZE is never required. However, if an application |
| 224 |
makes complex queries that have many possible query plans, the query |
| 225 |
planner will be better able to pick the best plan if ANALYZE has |
| 226 |
been run. This can result it significant performance improvements for |
| 227 |
some queries. |
| 228 |
|
| 229 |
</p><p> Two recommended approaches for when and how to run ANALYZE are |
| 230 |
described in the next subsections, in order of preference. |
| 231 |
|
| 232 |
<a name="pragopt"></a> |
| 233 |
|
| 234 |
</p><h2 id="periodically_run_pragma_optimize_"><span>2.1. </span>Periodically run "PRAGMA optimize"</h2> |
| 235 |
|
| 236 |
<p>The <a href="pragma.html#pragma_optimize">PRAGMA optimize</a> command will automatically run ANALYZE when |
| 237 |
needed. Suggested use: |
| 238 |
|
| 239 |
</p><ol> |
| 240 |
<li><p> |
| 241 |
Applications with short-lived database connections should run |
| 242 |
"PRAGMA optimize;" once, just prior to closing each database connection. |
| 243 |
|
| 244 |
</p></li><li><p> |
| 245 |
Applications that use long-lived database connections should run |
| 246 |
"PRAGMA optimize=0x10002;" when the connection is first opened, and then |
| 247 |
also run "PRAGMA optimize;" periodically, perhaps once per day, or more if |
| 248 |
the database is evolving rapidly. |
| 249 |
|
| 250 |
</p></li><li><p>All applications should run "PRAGMA optimize;" after a schema change, |
| 251 |
especially after one or more <a href="lang_createindex.html">CREATE INDEX</a> statements. |
| 252 |
</p></li></ol> |
| 253 |
|
| 254 |
<p> |
| 255 |
|
| 256 |
</p><p>The <a href="pragma.html#pragma_optimize">PRAGMA optimize</a> command is usually a no-op but it will occasionally |
| 257 |
run one or more ANALYZE subcommands on individual tables of the database |
| 258 |
if doing so will be useful to the query planner. |
| 259 |
Since SQLite version 3.46.0 (2024-05-23), the "PRAGMA optimize" command |
| 260 |
automatically limits the scope of ANALYZE subcommands so that |
| 261 |
the overall "PRAGMA optimize" command completes quickly even on enormous |
| 262 |
databases. There is no need to use <a href="pragma.html#pragma_analysis_limit">PRAGMA analysis_limit</a>. This is the |
| 263 |
recommended way of running ANALYZE moving forward. |
| 264 |
|
| 265 |
</p><p>The <a href="pragma.html#pragma_optimize">PRAGMA optimize</a> command will normally only consider running ANALYZE on |
| 266 |
tables that have been previously queried by the same database connection or |
| 267 |
that do not have entries in the <a href="fileformat2.html#stat1tab">sqlite_stat1</a> table. |
| 268 |
However, if the 0x10000 bit is added to the argument, PRAGMA optimize will |
| 269 |
examine all tables to see if they can benefit from ANALYZE, not just those |
| 270 |
that have been recently queried. |
| 271 |
There is no query history when a database connection first opens, and |
| 272 |
that is why adding the 0x10000 bit is recommended when running PRAGMA optimize |
| 273 |
on a fresh database connection. |
| 274 |
|
| 275 |
</p><p>See the <a href="lang_analyze.html#autoanalyze">Automatically Running ANALYZE</a> and |
| 276 |
<a href="lang_analyze.html#approx">Approximate ANALYZE For Large Databases</a> sections below for additional |
| 277 |
information. |
| 278 |
|
| 279 |
<a name="statanal"></a> |
| 280 |
|
| 281 |
</p><h2 id="fixed_results_of_analyze"><span>2.2. </span>Fixed results of ANALYZE</h2> |
| 282 |
|
| 283 |
<p>Running ANALYZE can cause SQLite to choose different query plans |
| 284 |
for subsequent queries. This is almost always a positive thing, as the |
| 285 |
query plans chosen after ANALYZE will in nearly every case be better than |
| 286 |
the query plans picked before ANALYZE. That is the whole point of ANALYZE. |
| 287 |
But there can be no proof of running ANALYZE will always be beneficial. |
| 288 |
One can construct pathological cases where running |
| 289 |
ANALYZE could make some subsequent queries run slower. |
| 290 |
|
| 291 |
</p><p>Some developers prefer that once the design of an application is frozen, |
| 292 |
SQLite will always pick the same query plans as it did during |
| 293 |
development and testing. |
| 294 |
Then if a millions of copies of the application are shipped to customers, |
| 295 |
the developers are assured that all of those millions of copies are running |
| 296 |
the same query plans regardless of what data the individual customers insert |
| 297 |
into their particular databases. This can help in reproducing complaints |
| 298 |
of performance problems coming back from the field. |
| 299 |
|
| 300 |
</p><p>To achieve this objection, never run a full ANALYZE nor the |
| 301 |
"PRAGMA optimize" command in the application. |
| 302 |
Rather, only run ANALYZE during development, manually using the |
| 303 |
<a href="cli.html">command-line interface</a> or similar, on a test database |
| 304 |
that is similar in size and content to live databases. Then capture |
| 305 |
the result of this one-time ANALYZE using a script like the |
| 306 |
following: |
| 307 |
|
| 308 |
</p><div class="codeblock"><pre>.mode list |
| 309 |
SELECT |
| 310 |
'ANALYZE sqlite_schema;' || |
| 311 |
'DELETE FROM sqlite_stat1;' || |
| 312 |
'INSERT INTO sqlite_stat1(tbl,idx,stat)VALUES' || |
| 313 |
(SELECT group_concat(format('(%Q,%Q,%Q)',tbl,idx,stat),',') |
| 314 |
FROM sqlite_stat1) || |
| 315 |
';ANALYZE sqlite_schema;'; |
| 316 |
</pre></div> |
| 317 |
|
| 318 |
<p>When creating a new instance of the database in deployed instances of |
| 319 |
the application, or perhaps every time the application is started up in |
| 320 |
the case of long-running applications, run the commands generated by |
| 321 |
script above. This will populate the <a href="fileformat2.html#stat1tab">sqlite_stat1</a> table exactly as |
| 322 |
it was during development and testing and ensure that the query plans |
| 323 |
selected in the field are same has those selected during testing in the |
| 324 |
lab. Maybe copy/paste the string generated by the script above into |
| 325 |
a static string constant named "zStat1Init" and then invoke: |
| 326 |
|
| 327 |
</p><div class="codeblock"><pre>sqlite3_exec(db, zStat1Init, 0, 0, 0); |
| 328 |
</pre></div> |
| 329 |
|
| 330 |
<p>Perhaps also add "BEGIN;" at the start of the string constant and |
| 331 |
"COMMIT;" at the end, depending on the context in which the script is run. |
| 332 |
|
| 333 |
</p><p>See the <a href="queryplanner-ng.html#qpstab">query planner stability guarantee</a> for addition information. |
| 334 |
|
| 335 |
</p><h1 id="details"><span>3. </span>Details</h1> |
| 336 |
|
| 337 |
<p> The default implementation stores all statistics in a single |
| 338 |
table named "<a href="fileformat2.html#stat1tab">sqlite_stat1</a>". |
| 339 |
If SQLite is compiled with the |
| 340 |
<a href="compile.html#enable_stat4">SQLITE_ENABLE_STAT4</a> option, then additional histogram data is |
| 341 |
collected and stored in <a href="fileformat2.html#stat4tab">sqlite_stat4</a>. |
| 342 |
Older versions of SQLite would make use of the <a href="fileformat2.html#stat2tab">sqlite_stat2</a> table |
| 343 |
or <a href="fileformat2.html#stat3tab">sqlite_stat3</a> table |
| 344 |
when compiled with <a href="compile.html#enable_stat2">SQLITE_ENABLE_STAT2</a> or <a href="compile.html#enable_stat3">SQLITE_ENABLE_STAT3</a>, |
| 345 |
but all recent versions of |
| 346 |
SQLite ignore the sqlite_stat2 and sqlite_stat3 tables. |
| 347 |
Future enhancements may create |
| 348 |
additional <a href="fileformat2.html#intschema">internal tables</a> with the same name pattern except with |
| 349 |
final digit larger than "4". |
| 350 |
All of these tables are collectively referred to as "statistics tables". |
| 351 |
</p> |
| 352 |
|
| 353 |
<p> The content of the statistics tables can be queried using <a href="lang_select.html">SELECT</a> |
| 354 |
and can be changed using the <a href="lang_delete.html">DELETE</a>, <a href="lang_insert.html">INSERT</a>, and <a href="lang_update.html">UPDATE</a> commands. |
| 355 |
The <a href="lang_droptable.html">DROP TABLE</a> command works on statistics tables |
| 356 |
as of SQLite version 3.7.9. (2011-11-01) |
| 357 |
The <a href="lang_altertable.html">ALTER TABLE</a> command does not work on statistics tables. |
| 358 |
Appropriate care should be used when changing the content of the statistics |
| 359 |
tables as invalid content can cause SQLite to select inefficient |
| 360 |
query plans. Generally speaking, one should not modify the content of |
| 361 |
the statistics tables by any mechanism other than invoking the |
| 362 |
ANALYZE command. |
| 363 |
See "<a href="optoverview.html#manctrl">Manual Control Of Query Plans Using SQLITE_STAT Tables</a>" for |
| 364 |
further information.</p> |
| 365 |
|
| 366 |
<p> Statistics gathered by ANALYZE are <u>not</u> updated as |
| 367 |
the content of the database changes. If the content of the database |
| 368 |
changes significantly, or if the database schema changes, then one should |
| 369 |
consider rerunning the ANALYZE command in order to update the statistics.</p> |
| 370 |
|
| 371 |
<p> The query planner loads the content of the statistics tables |
| 372 |
into memory when the schema is read. Hence, when an application |
| 373 |
changes the statistics tables directly, SQLite will not immediately |
| 374 |
notice the changes. An application |
| 375 |
can force the query planner to reread the statistics tables by running |
| 376 |
<b>ANALYZE sqlite_schema</b>. </p> |
| 377 |
|
| 378 |
<a name="autoanalyze"></a> |
| 379 |
|
| 380 |
<h1 id="automatically_running_analyze"><span>4. </span>Automatically Running ANALYZE</h1> |
| 381 |
|
| 382 |
<p>The <a href="pragma.html#pragma_optimize">PRAGMA optimize</a> command will automatically run ANALYZE on individual |
| 383 |
tables on an as-needed basis. The recommended practice is for applications |
| 384 |
to invoke the <a href="pragma.html#pragma_optimize">PRAGMA optimize</a> statement just before closing each database |
| 385 |
connection. Or, if the application keeps a single database connection open |
| 386 |
for a long time, then it should run "PRAGMA optimize=0x10002" when the |
| 387 |
connection is first opened and run "PRAGMA optimize;" periodically thereafter, |
| 388 |
perhaps once per day or even once per hour.</p> |
| 389 |
|
| 390 |
<p>Each SQLite <a href="c3ref/sqlite3.html">database connection</a> records cases when the query planner would |
| 391 |
benefit from having accurate results of ANALYZE at hand. These records |
| 392 |
are held in memory and accumulate over the life of a database connection. |
| 393 |
The <a href="pragma.html#pragma_optimize">PRAGMA optimize</a> command looks at those records and runs ANALYZE on only |
| 394 |
those tables for which new or updated ANALYZE data seems likely to be useful. |
| 395 |
In most cases <a href="pragma.html#pragma_optimize">PRAGMA optimize</a> will not run ANALYZE, but it will occasionally |
| 396 |
do so either for tables that have never before been analyzed, or for tables |
| 397 |
that have grown significantly since they were last analyzed.</p> |
| 398 |
|
| 399 |
<p>Since the actions of <a href="pragma.html#pragma_optimize">PRAGMA optimize</a> are determined to some extent by |
| 400 |
prior queries that have been evaluated on the same database connection, it |
| 401 |
is recommended that <a href="pragma.html#pragma_optimize">PRAGMA optimize</a> be deferred until the database connection |
| 402 |
is closing and has thus had an opportunity to accumulate as much usage information |
| 403 |
as possible. It is also reasonable to set a timer to run <a href="pragma.html#pragma_optimize">PRAGMA optimize</a> |
| 404 |
every few hours, or every few days, for database connections that stay open |
| 405 |
for a long time. When running <a href="pragma.html#pragma_optimize">PRAGMA optimize</a> immediately after a |
| 406 |
database connection is opened, one can add the 0x10000 bit to the bitmask |
| 407 |
argument (thus making the command read "PRAGMA optimize=0x10002") which |
| 408 |
causes all tables to be examined, even tables that have not been |
| 409 |
queried during the current connection.</p> |
| 410 |
|
| 411 |
<p>The <a href="pragma.html#pragma_optimize">PRAGMA optimize</a> command was first introduced with |
| 412 |
SQLite 3.18.0 (2017-03-28) and is a no-op for all prior releases |
| 413 |
of SQLite. The <a href="pragma.html#pragma_optimize">PRAGMA optimize</a> command was significantly enhanced |
| 414 |
in SQLite 3.46.0 (2024-05-23) and the advice given in this |
| 415 |
documentation is based on those enhancements. Applications that |
| 416 |
use earlier versions of SQLite should consult the corresponding |
| 417 |
documentation for better advice on the best ways to use PRAGMA optimize.</p> |
| 418 |
|
| 419 |
<a name="approx"></a> |
| 420 |
|
| 421 |
<h1 id="approximate_analyze_for_large_databases"><span>5. </span>Approximate ANALYZE For Large Databases</h1> |
| 422 |
|
| 423 |
<p>By default, ANALYZE does a full scan of every index. This can be slow for |
| 424 |
large databases. So beginning with SQLite version 3.32.0 (2020-05-22), the |
| 425 |
<a href="pragma.html#pragma_analysis_limit">PRAGMA analysis_limit</a> command can be used to limit the amount of |
| 426 |
scanning performed by ANALYZE, and thus help ANALYZE to run faster, |
| 427 |
even on very large database files. We call this running an |
| 428 |
"approximate ANALYZE". |
| 429 |
|
| 430 |
</p><p>The recommended usage pattern for the <a href="pragma.html#pragma_analysis_limit">analysis_limit</a> pragma is |
| 431 |
like this: |
| 432 |
|
| 433 |
</p><div class="codeblock"><pre>PRAGMA analysis_limit=1000; |
| 434 |
</pre></div> |
| 435 |
|
| 436 |
<p>This pragma tells the ANALYZE command to start a full scan |
| 437 |
of the index as it normally would. But when the number of rows visited |
| 438 |
reaches 1000 (or whatever other limit is specified by the pragma), the |
| 439 |
ANALYZE command will begin taking actions to stop the scan. If |
| 440 |
the left-most column of the index has changed at least once during the |
| 441 |
previous 1000 steps, then the analysis stops immediately. But if the |
| 442 |
left-most column has always been the same, then ANALYZE skips ahead to |
| 443 |
the first entry with a different left-most column and reads an additional |
| 444 |
1000 rows before terminating. |
| 445 |
|
| 446 |
</p><p>The details of the effects of the analysis limit described in the previous |
| 447 |
paragraph are subject to change in future versions of SQLite. But the |
| 448 |
core idea will remain the same. An analysis limit of N will strive to |
| 449 |
limit the number of rows visited in each index to approximately N. |
| 450 |
|
| 451 |
</p><p>Values of N between 100 and 1000 are recommended. |
| 452 |
Or, to disable the analysis limit, causing ANALYZE to do a |
| 453 |
complete scan of each index, set the analysis limit to 0. The default |
| 454 |
value for the analysis limit is 0 for backwards compatibility. |
| 455 |
|
| 456 |
</p><p>The values placed in the sqlite_stat1 table by an approximate ANALYZE |
| 457 |
are not exactly the same as what would be computed by an unrestricted |
| 458 |
analysis. But they are usually close enough. The index statistics in |
| 459 |
the sqlite_stat1 table are approximations in any case, so the fact that |
| 460 |
the results of an approximate ANALYZE are slightly different from |
| 461 |
a traditional full scan ANALYZE has little practical impact. It is |
| 462 |
possible to construct a pathological case where an approximate ANALYZE |
| 463 |
is noticeably inferior to a full-scan ANALYZE, but such cases are rare in |
| 464 |
real-world problems. |
| 465 |
|
| 466 |
</p><p>A good rule of thumb seems to be to always set "PRAGMA analysis_limit=N" |
| 467 |
for N between 100 and 1000 prior to running either "ANALYZE". It used |
| 468 |
to be that this was also recommended prior to running |
| 469 |
"<a href="pragma.html#pragma_optimize">PRAGMA optimize</a>", but since version 3.46.0 (2024-05-23) that |
| 470 |
happens automatically. The results are not quite as precise when using |
| 471 |
PRAGMA analysis_limit, but they are precise enough, and the fact that |
| 472 |
the results are computed so much faster means that developers are more |
| 473 |
likely to compute them. An approximate ANALYZE is better than not |
| 474 |
running ANALYZE at all. |
| 475 |
|
| 476 |
</p><h2 id="limitations_of_approximate_analyze"><span>5.1. </span>Limitations of approximate ANALYZE</h2> |
| 477 |
|
| 478 |
<p>The content in the sqlite_stat4 table cannot be computed with |
| 479 |
anything less than a full scan. Hence, if a non-zero analysis limit |
| 480 |
is specified, the sqlite_stat4 table is not computed. |
| 481 |
</p><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/lang_analyze.in?m=4607c3507e">2024-05-05 15:23:53</a> UTC </small></i></p> |
| 482 |
|