-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |