Skip to content

Commit

Permalink
Add indexes to docs
Browse files Browse the repository at this point in the history
  • Loading branch information
steida committed Mar 21, 2024
1 parent 03a56c6 commit d3a86ed
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions apps/web/pages/docs/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"installation": "Installation",
"quickstart": "Quickstart",
"api": "API",
"indexes": "Indexes",
"migrations": "Migrations",
"patterns": "Patterns",
"evolu-server": "Evolu Server",
Expand Down
48 changes: 48 additions & 0 deletions apps/web/pages/docs/indexes.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Callout } from "nextra-theme-docs";

# Indexes

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

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

<Callout type="info" emoji="ℹ️">
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.
</Callout>

If you want to learn what indexes are and how they should be used, check this
article [squeezing-performance-from-sqlite-indexes](https://medium.com/@JasonWyatt/squeezing-performance-from-sqlite-indexes-indexes-c4e175f3c346).

## Usage

```ts
const indexes = [
createIndex("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](https://sqlite.org/cli.html#index_recommendations_sqlite_expert_)
for index recommendations. Download "Precompiled Binaries" [here](https://www.sqlite.org/download.html), 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.
To get SQL for creating tables, use the global `logSql` Evolu config option
and reset Evolu.

0 comments on commit d3a86ed

Please sign in to comment.