<title>The SQLITE_STMT Virtual Table</title>
<tcl>hd_keywords sqlite_stmt {SQLITE_STMT virtual table} \
        {the SQLITE_STMT extension}</tcl>
<fancy_format>

<h1>Overview</h1>

<p>
The SQLITE_STMT extension implements an [eponymous-only virtual table] that
provides information about all [prepared statements] associated with
the [database connection].
</p>

<p>
The SQLITE_STMT extension is included in the [amalgamation] though 
it is disabled
by default.  Use the [SQLITE_ENABLE_STMTVTAB] compile-time option to enable
the SQLITE_STMT extension.  The SQLITE_STMT extension can also be 
loaded at run-time
by compiling the extension into a shared library or DLL using the source
code at [https://sqlite.org/src/file/ext/misc/stmt.c] and following the
instructions for how to [compile loadable extensions].
</p>

<p>
The SQLITE_STMT extension is enabled in default builds
of the [command-line shell].

<h1>Usage</h1>

<p>
The SQLITE_STMT virtual table is a read-only table that can be directly
queried to access information about all prepared statements on the 
current database connection.  For example:

<codeblock>
SELECT * FROM sqlite_stmt;
</codeblock>

<p>
A statement such as the above can be run immediately prior to invoking
[sqlite3_close()] to confirm that all prepared statements have been 
[sqlite3_finalize|finalized] and to help identify and track down prepared
statements that have "leaked" and missed finalization.

<p>
The SQLITE_STMT virtual table can also be used to access performance
information about prepared statements, to aid in optimization an application.
For example,
to find out how much memory is being used by [prepared statements] that have
never been used, one could run:

<codeblock>
SELECT sum(mem) FROM sqlite_stmt WHERE run=0;
</codeblock>

<h2>Columns</h2>

<p>
The columns are provided by the SQLITE_STMT virtual table are summarized by
the hypothetical CREATE TABLE statement show here:

<codeblock>
CREATE TABLE sqlite_stmt(
  sql    TEXT,    -- Original SQL text
  ncol   INT,     -- Number of output columns
  ro     BOOLEAN, -- True for "read only" statements
  busy   BOOLEAN, -- True if the statement is current running
  nscan  INT,     -- Number of full-scan steps
  nsort  INT,     -- Number of sort operations
  naidx  INT,     -- Number of automatic index inserts
  nstep  INT,     -- Number of byte-code engine steps
  reprep INT,     -- Number of reprepare operations
  run    INT,     -- Number of times this statement has been run
  mem    INT      -- Heap memory used by this statement
);
</codeblock>

<p>Future releases may add new output columns and may change the order
of legacy columns.
Further detail about the meaning of each column in the SQLITE_STMT virtual
table is provided below:

<ul>
<li><p><b>sql</b>:
The original SQL text of the prepared statement.  If the prepared
statement is compiled using the [sqlite3_prepare()] interface, then
the SQL text might not have been saved, in which case this column
will be NULL.

<li><p><b>ncol</b>:
The number of columns in the result set of a query.
For DML statements, this column has a value of 0.

<li><p><b>ro</b>:
The "read only" column.  This column is true (non-zero) if the
SQL statement is a query and false (zero) if it is a DML statement.

<li><p><b>busy</b>:
This field is true if the prepared statement is currently running.
In other words, this field is true if [sqlite3_step()] has been called
on the [prepared statement] at least once but [sqlite3_reset()] has
not yet been called to reset it.

<li><p><b>nscan</b>:
This field is the number of times that the [bytecode engine] has stepped
through a table as part of a full-table scan.  A large number if this
field may indicate an opportunity to improve performance by adding an
index.  This field is equivalent to the [SQLITE_STMTSTATUS_FULLSCAN_STEP]
value.

<li><p><b>nsort</b>:
This field is the number of times that the [bytecode engine] had to sort.
A positive value in this field may indicate an opportunity to improve
performance by adding an index that will cause the query results to
appear naturally in the desired order.  
This field is equivalent to the [SQLITE_STMTSTATUS_SORT] value.

<li><p><b>naidx</b>:
This field is the number of rows that have been inserted into
[automatic indexes].  A positive value in this field may indicate 
an opportunity to improve performance by adding a named index that
take the place of the automatic index.
This field is equivalent to the [SQLITE_STMTSTATUS_AUTOINDEX] value.

<li><p><b>nstep</b>:
This field is the number of [bytecode engine] operations that have
been performed for the prepared statement.  This field can be used
as a proxy for how much CPU time a statement has used.
This field is equivalent to the [SQLITE_STMTSTATUS_VM_STEP] value.

<li><p><b>reprep</b>:
This field is the number of times that the statement has had to be
reprepared due to schema changes or changes to parameter bindings.
This field is equivalent to the [SQLITE_STMTSTATUS_REPREPARE] value.

<li><p><b>run</b>:
This field is the number of times that the statement has been run.
This field is equivalent to the [SQLITE_STMTSTATUS_RUN] value.

<li><p><b>mem</b>:
This field is the number of bytes of heap storage used by the
prepared statement.
This field is equivalent to the [SQLITE_STMTSTATUS_MEMUSED] value.
</ul>
