| 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>The SQLITE_STMT Virtual Table</title> |
| 7 |
<!-- path= --> |
| 8 |
</head> |
| 9 |
<body> |
| 10 |
<div class=nosearch> |
| 11 |
<a href="index.html"> |
| 12 |
<img class="logo" src="images/sqlite370_banner.svg" 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 |
The SQLITE_STMT Virtual Table |
| 122 |
</div> |
| 123 |
</div> |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
<h1 id="overview"><span>1. </span>Overview</h1> |
| 129 |
|
| 130 |
<p> |
| 131 |
The SQLITE_STMT extension implements an <a href="vtab.html#epoonlyvtab">eponymous-only virtual table</a> that |
| 132 |
provides information about all <a href="c3ref/stmt.html">prepared statements</a> associated with |
| 133 |
the <a href="c3ref/sqlite3.html">database connection</a>. |
| 134 |
</p> |
| 135 |
|
| 136 |
<p> |
| 137 |
The SQLITE_STMT extension is included in the <a href="amalgamation.html">amalgamation</a> though |
| 138 |
it is disabled |
| 139 |
by default. Use the <a href="compile.html#enable_stmtvtab">SQLITE_ENABLE_STMTVTAB</a> compile-time option to enable |
| 140 |
the SQLITE_STMT extension. The SQLITE_STMT extension can also be |
| 141 |
loaded at run-time |
| 142 |
by compiling the extension into a shared library or DLL using the source |
| 143 |
code at <a href="https://sqlite.org/src/file/ext/misc/stmt.c">https://sqlite.org/src/file/ext/misc/stmt.c</a> and following the |
| 144 |
instructions for how to <a href="loadext.html#build">compile loadable extensions</a>. |
| 145 |
</p> |
| 146 |
|
| 147 |
<p> |
| 148 |
The SQLITE_STMT extension is enabled in default builds |
| 149 |
of the <a href="cli.html">command-line shell</a>. |
| 150 |
|
| 151 |
</p><h1 id="usage"><span>2. </span>Usage</h1> |
| 152 |
|
| 153 |
<p> |
| 154 |
The SQLITE_STMT virtual table is a read-only table that can be directly |
| 155 |
queried to access information about all prepared statements on the |
| 156 |
current database connection. For example: |
| 157 |
|
| 158 |
</p><div class="codeblock"><pre>SELECT * FROM sqlite_stmt; |
| 159 |
</pre></div> |
| 160 |
|
| 161 |
<p> |
| 162 |
A statement such as the above can be run immediately prior to invoking |
| 163 |
<a href="c3ref/close.html">sqlite3_close()</a> to confirm that all prepared statements have been |
| 164 |
<a href="c3ref/finalize.html">finalized</a> and to help identify and track down prepared |
| 165 |
statements that have "leaked" and missed finalization. |
| 166 |
|
| 167 |
</p><p> |
| 168 |
The SQLITE_STMT virtual table can also be used to access performance |
| 169 |
information about prepared statements, to aid in optimizing an application. |
| 170 |
For example, |
| 171 |
to find out how much memory is being used by <a href="c3ref/stmt.html">prepared statements</a> that have |
| 172 |
never been used, one could run: |
| 173 |
|
| 174 |
</p><div class="codeblock"><pre>SELECT sum(mem) FROM sqlite_stmt WHERE run=0; |
| 175 |
</pre></div> |
| 176 |
|
| 177 |
<h2 id="columns"><span>2.1. </span>Columns</h2> |
| 178 |
|
| 179 |
<p> |
| 180 |
The columns provided by the SQLITE_STMT virtual table are summarized by |
| 181 |
the hypothetical CREATE TABLE statement show here: |
| 182 |
|
| 183 |
</p><div class="codeblock"><pre>CREATE TABLE sqlite_stmt( |
| 184 |
sql TEXT, -- Original SQL text |
| 185 |
ncol INT, -- Number of output columns |
| 186 |
ro BOOLEAN, -- True for "read only" statements |
| 187 |
busy BOOLEAN, -- True if the statement is current running |
| 188 |
nscan INT, -- Number of full-scan steps |
| 189 |
nsort INT, -- Number of sort operations |
| 190 |
naidx INT, -- Number of automatic index inserts |
| 191 |
nstep INT, -- Number of byte-code engine steps |
| 192 |
reprep INT, -- Number of reprepare operations |
| 193 |
run INT, -- Number of times this statement has been run |
| 194 |
mem INT -- Heap memory used by this statement |
| 195 |
); |
| 196 |
</pre></div> |
| 197 |
|
| 198 |
<p>Future releases may add new output columns and may change the order |
| 199 |
of legacy columns. |
| 200 |
Further detail about the meaning of each column in the SQLITE_STMT virtual |
| 201 |
table is provided below: |
| 202 |
|
| 203 |
</p><ul> |
| 204 |
<li><p><b>sql</b>: |
| 205 |
The original SQL text of the prepared statement. If the prepared |
| 206 |
statement is compiled using the <a href="c3ref/prepare.html">sqlite3_prepare()</a> interface, then |
| 207 |
the SQL text might not have been saved, in which case this column |
| 208 |
will be NULL. |
| 209 |
|
| 210 |
</p></li><li><p><b>ncol</b>: |
| 211 |
The number of columns in the result set of a query. |
| 212 |
For DML statements, this column has a value of 0. |
| 213 |
|
| 214 |
</p></li><li><p><b>ro</b>: |
| 215 |
The "read only" column. This column is true (non-zero) if the |
| 216 |
SQL statement is a query and false (zero) if it is a DML statement. |
| 217 |
|
| 218 |
</p></li><li><p><b>busy</b>: |
| 219 |
This field is true if the prepared statement is currently running. |
| 220 |
In other words, this field is true if <a href="c3ref/step.html">sqlite3_step()</a> has been called |
| 221 |
on the <a href="c3ref/stmt.html">prepared statement</a> at least once but <a href="c3ref/reset.html">sqlite3_reset()</a> has |
| 222 |
not yet been called to reset it. |
| 223 |
|
| 224 |
</p></li><li><p><b>nscan</b>: |
| 225 |
This field is the number of times that the <a href="opcode.html">bytecode engine</a> has stepped |
| 226 |
through a table as part of a full-table scan. A large number if this |
| 227 |
field may indicate an opportunity to improve performance by adding an |
| 228 |
index. This field is equivalent to the <a href="c3ref/c_stmtstatus_counter.html#sqlitestmtstatusfullscanstep">SQLITE_STMTSTATUS_FULLSCAN_STEP</a> |
| 229 |
value. |
| 230 |
|
| 231 |
</p></li><li><p><b>nsort</b>: |
| 232 |
This field is the number of times that the <a href="opcode.html">bytecode engine</a> had to sort. |
| 233 |
A positive value in this field may indicate an opportunity to improve |
| 234 |
performance by adding an index that will cause the query results to |
| 235 |
appear naturally in the desired order. |
| 236 |
This field is equivalent to the <a href="c3ref/c_stmtstatus_counter.html#sqlitestmtstatussort">SQLITE_STMTSTATUS_SORT</a> value. |
| 237 |
|
| 238 |
</p></li><li><p><b>naidx</b>: |
| 239 |
This field is the number of rows that have been inserted into |
| 240 |
<a href="optoverview.html#autoindex">automatic indexes</a>. A positive value in this field may indicate |
| 241 |
an opportunity to improve performance by adding a named index that |
| 242 |
takes the place of the automatic index. |
| 243 |
This field is equivalent to the <a href="c3ref/c_stmtstatus_counter.html#sqlitestmtstatusautoindex">SQLITE_STMTSTATUS_AUTOINDEX</a> value. |
| 244 |
|
| 245 |
</p></li><li><p><b>nstep</b>: |
| 246 |
This field is the number of <a href="opcode.html">bytecode engine</a> operations that have |
| 247 |
been performed for the prepared statement. This field can be used |
| 248 |
as a proxy for how much CPU time a statement has used. |
| 249 |
This field is equivalent to the <a href="c3ref/c_stmtstatus_counter.html#sqlitestmtstatusvmstep">SQLITE_STMTSTATUS_VM_STEP</a> value. |
| 250 |
|
| 251 |
</p></li><li><p><b>reprep</b>: |
| 252 |
This field is the number of times that the statement has had to be |
| 253 |
reprepared due to schema changes or changes to parameter bindings. |
| 254 |
This field is equivalent to the <a href="c3ref/c_stmtstatus_counter.html#sqlitestmtstatusreprepare">SQLITE_STMTSTATUS_REPREPARE</a> value. |
| 255 |
|
| 256 |
</p></li><li><p><b>run</b>: |
| 257 |
This field is the number of times that the statement has been run. |
| 258 |
This field is equivalent to the <a href="c3ref/c_stmtstatus_counter.html#sqlitestmtstatusrun">SQLITE_STMTSTATUS_RUN</a> value. |
| 259 |
|
| 260 |
</p></li><li><p><b>mem</b>: |
| 261 |
This field is the number of bytes of heap storage used by the |
| 262 |
prepared statement. |
| 263 |
This field is equivalent to the <a href="c3ref/c_stmtstatus_counter.html#sqlitestmtstatusmemused">SQLITE_STMTSTATUS_MEMUSED</a> value. |
| 264 |
</p></li></ul> |
| 265 |
<p align="center"><small><i>This page was last updated on 2025-05-31 13:08:22Z </small></i></p> |
| 266 |
|