Documentation
Indexes

Indexes

Are your queries taking too long to run? Measure them with the logExecutionTime option.

const allTodos = evolu.createQuery(
  (db) => db.selectFrom("todo").orderBy("createdAt").selectAll(),
  {
    logExecutionTime: true,
    // logExplainQueryPlan: false,
  },
);
ℹ️

While indexes are not necessary for development, they are almost always required for production. But before adding them, measure and analyze the query first with logExecutionTime and logExplainQueryPlan createQuery options.

If you want to learn what indexes are and how they should be used, check this article squeezing-performance-from-sqlite-indexes (opens in a new tab).

Usage

const indexes = createIndexes((create) => [
  create("indexTodoCreatedAt").on("todo").column("createdAt"),
]);
 
const evolu = createEvolu(Database, { indexes });

That's all we have to do. Evolu automatically adds new indexes and drops old ones (not listed in the indexes array).

Index Recommendations (SQLite Expert)

SQLite has an excellent CLI tool (opens in a new tab) for index recommendations. Download "Precompiled Binaries" here (opens in a new tab), open a file or create a DB, run .expert, and paste the query SQL there.

To get the query SQL (for copy-pasting to .expert), use the logExecutionTime createQuery options.