Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/content/docs/d1/best-practices/use-indexes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ SELECT * FROM orders WHERE order_date = '2023-05-01'

In more complex cases, you can confirm whether an index was used by D1 by [analyzing a query](#test-an-index) directly.

### Run `PRAGMA optimize`

After creating an index, run the [`PRAGMA optimize`](/d1/sql-api/sql-statements/#pragma-optimize) command to improve your database performance.

## List indexes

List the indexes on a database, as well as the SQL definition, by querying the `sqlite_schema` system table:
Expand Down
8 changes: 8 additions & 0 deletions src/content/partials/d1/use-pragma-statements.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -411,3 +411,11 @@ PRAGMA defer_foreign_keys = off
```

Refer to the [foreign key documentation](/d1/sql-api/foreign-keys/) to learn more about how to work with foreign keys.

### `PRAGMA optimize`

Attempts to [optimize all schemas]() in a database by running various maintenance operations. We recommend running this command after [creating an index](/d1/best-practices/use-indexes/).

Alternatively, to see all optimizations that would have been performed without actually executing them, run `PRAGMA optimize(-1)`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way D1 implements PRAGMA optimize, debug mode won't work since we constrain PRAGMA optimize to not return any rows.

As far as I can tell from reading the source code, debug mode returns a list of ANALYZE schema_name.table_name statements that PRAGMA optimize would have otherwise run.

@vy-ton I guess we should get debug mode to work, shouldn't we? Sorry for missing that detail. It's not documented at all! I had to read the source to figure it out.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we should get debug mode to work, shouldn't we?

@justin-mp Would be nice to close out the pragma optimize loop but I wouldn't rank it high given other dev week priorities. let's make sure to at least create a tracking ticket.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Oxyjun Given debug mode doesnt work, I would mention that PRAGMA optimize(-1) explicitly is not supported yet

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we should get debug mode to work, shouldn't we?

@justin-mp Would be nice to close out the pragma optimize loop but I wouldn't rank it high given other dev week priorities. let's make sure to at least create a tracking ticket.

See cloudflare/workerd#3595 for the fix.


Refer to [SQLite PRAGMA optimize documentation](https://www.sqlite.org/pragma.html#pragma_optimize) for more information on how the command optimizes a database.
Loading