SQLite PRAGMA

PRAGMA optimize

Attempt to optimize the database.

PRAGMA optimize;
PRAGMA optimize(MASK);
PRAGMA schema.optimize;
PRAGMA schema.optimize(MASK);

What the SQLite documentation says

Attempt to optimize the database. All schemas are optimized in the first two forms, and only the specified schema is optimized in the latter two.

In most applications, using PRAGMA optimize as follows will help SQLite to achieve the best possible query performance:

  1. Applications with short-lived database connections should run "PRAGMA optimize;" once, just prior to closing each database connection.

  2. Applications that use long-lived database connections should run "PRAGMA optimize=0x10002;" when the connection is first opened, and then also run "PRAGMA optimize;" periodically, perhaps once per day or once per hour.

  3. All applications should run "PRAGMA optimize;" after a schema change, especially after one or more CREATE INDEX statements.

This pragma is usually a no-op or nearly so and is very fast. On the occasions where it does need to run ANALYZE on one or more tables, it sets a temporary analysis limit, valid for the duration of this pragma only, that prevents the ANALYZE invocations from running for too long.

Recommended practice is that applications with short-lived database connections should run "PRAGMA optimize" once when the database connection closes. Applications with long-lived database connections should run "PRAGMA optimize=0x10002" when the database connection first opens, then run "PRAGMA optimize" again at periodic intervals - perhaps once per day. All applications should run "PRAGMA optimize" after schema changes, especially CREATE INDEX.

The details of optimizations performed by this pragma are expected to change and improve over time. Applications should anticipate that this pragma will perform new optimizations in future releases.

The optional MASK argument is a bitmask of optimizations to perform:

0x00001 Debugging mode. Do not actually perform any optimizations but instead return one line of text for each optimization that would have been done. Off by default.
0x00002 Run ANALYZE on tables that might benefit. On by default.
0x00010 When running ANALYZE, set a temporary PRAGMA analysis_limit to prevent excess run-time. On by default.
0x10000 Check the size of all tables, not just tables that have been recently used, to see if any have grown and shrunk significantly and hence might benefit from being re-analyzed. Off by default.

The default MASK is 0xfffe.

To see all optimizations that would have been done without actually doing them, run "PRAGMA optimize(-1)".

Determination Of When To Run Analyze

In the current implementation, a table is analyzed if and only if all of the following are true:

  1. MASK bit 0x02 is set.
  2. The table is an ordinary table, not a view or virtual table.
  3. The table name does not begin with "sqlite_".
  4. One or more of the following are true:
    1. The 0x10000 bit of MASK is set
    2. One or more indexes on the table lack entries in the sqlite_stat1 table.
    3. The query planner used sqlite_stat1 statistics for one or more indexes of this table at some point during the lifetime of the current database connection.
  5. One or more of the following are true:
    1. One or more indexes on the table lack entries in the sqlite_stat1 table.
    2. The number of rows in the table has increased or decreased by 10-fold since the last time ANALYZE was run on the table.

The rules for when tables are analyzed are likely to change in future releases. New MASK values may be added in the future. Future versions of this pragma might accept a string literal argument instead of a bit mask, though the bit mask argument will continue to be supported for backwards compatibility.

From sqlite.org's pragma documentation for SQLite 3.54.1, in the public domain.

← module_list · page_count →