Skip to content

Commit dfc9e77

Browse files
Oxyjunvy-ton
andauthored
[D1] Adding PRAGMA optimize for D1 (#19996)
* Adding PRAGMA optimize for D1. * Adding PRAGMA optimize definition to partial. * Including a line in the best practices section. * Fixing wording, linking to SQLite documentation. * Minor wording update. * Fixing incorrect description of PRAGMA optimize. * Specifying that ANALYZE may not always run. * Wording edit * Adding corresponding D1 changelog. * Adding changelog entry for DO. * Editing wording to make link more explicit. * Introducing glossary term query planner, adding more details. * Adding D1 glossary. * Update src/content/release-notes/d1.yaml Co-authored-by: Vy Ton <[email protected]> * Changing release note wording, explicitly mentioning we don't support PRAGMA optimize(-1). --------- Co-authored-by: Vy Ton <[email protected]>
1 parent f980ece commit dfc9e77

File tree

6 files changed

+58
-2
lines changed

6 files changed

+58
-2
lines changed

src/content/docs/d1/best-practices/use-indexes.mdx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ sidebar:
66

77
---
88

9+
import { GlossaryTooltip } from "~/components";
10+
911
Indexes enable D1 to improve query performance over the indexed columns for common (popular) queries by reducing the amount of data (number of rows) the database has to scan when running a query.
1012

1113
## When is an index useful?
@@ -62,6 +64,14 @@ SELECT * FROM orders WHERE order_date = '2023-05-01'
6264

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

67+
### Run `PRAGMA optimize`
68+
69+
After creating an index, run the `PRAGMA optimize` command to improve your database performance.
70+
71+
`PRAGMA optimize` runs `ANALYZE` command on each table in the database, which collects statistics on the tables and indices. These statistics allows the <GlossaryTooltip term="query planner">query planner</GlossaryTooltip> to generate the most efficient query plan when executing the user query.
72+
73+
For more information, refer to [`PRAGMA optimize`](/d1/sql-api/sql-statements/#pragma-optimize).
74+
6575
## List indexes
6676

6777
List the indexes on a database, as well as the SQL definition, by querying the `sqlite_schema` system table:
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
title: Glossary
3+
pcx_content_type: glossary
4+
sidebar:
5+
order: 12
6+
7+
---
8+
9+
import { Glossary } from "~/components"
10+
11+
Review the definitions for terms used across Cloudflare's D1 documentation.
12+
13+
<Glossary product="d1" />

src/content/glossary/d1.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
productName: D1
3+
entries:
4+
- term: "query planner"
5+
general_definition: |-
6+
A component in a database management system which takes a user query and generates the most efficient plan of executing that query (the query plan). For example, the query planner decides which indices to use, or which table to access first.

src/content/partials/d1/use-pragma-statements.mdx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{}
33
---
44

5-
import { Details } from "~/components";
5+
import { Details, GlossaryTooltip } from "~/components";
66

77
The PRAGMA statement examples on this page use the following SQL.
88

@@ -411,3 +411,19 @@ PRAGMA defer_foreign_keys = off
411411
```
412412

413413
Refer to the [foreign key documentation](/d1/sql-api/foreign-keys/) to learn more about how to work with foreign keys.
414+
415+
### `PRAGMA optimize`
416+
417+
Attempts to optimize all schemas in a database by running the `ANALYZE` command for each table, if necessary. `ANALYZE` updates an internal table which contain statistics about tables and indices. These statistics helps the <GlossaryTooltip term="query planner">query planner</GlossaryTooltip> to execute the input query more efficiently.
418+
419+
When `PRAGMA optimize` runs `ANALYZE`, it sets a limit to ensure the command does not take too long to execute. Alternatively, `PRAGMA optimize` may deem it unnecessary to run `ANALYZE` (for example, if the schema has not changed significantly). In this scenario, no optimizations are made.
420+
421+
We recommend running this command after making any changes to the schema (for example, after [creating an index](/d1/best-practices/use-indexes/)).
422+
423+
:::note
424+
Currently, D1 does not support `PRAGMA optimize(-1)`.
425+
426+
`PRAGMA optimize(-1)` is a command which displays all optimizations that would have been performed without actually executing them.
427+
:::
428+
429+
Refer to [SQLite PRAGMA optimize documentation](https://www.sqlite.org/pragma.html#pragma_optimize) for more information on how `PRAGMA optimize` optimizes a database.

src/content/release-notes/d1.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@ productLink: "/d1/"
55
productArea: Developer platform
66
productAreaLink: /workers/platform/changelog/platform/
77
entries:
8+
- publish_date: "2025-02-19"
9+
title: D1 supports `PRAGMA optimize`
10+
description: |-
11+
D1 now supports `PRAGMA optimize` command, which can improve database query performance. It is recommended to run this command after a schema change (for example, after creating an index). Refer to [`PRAGMA optimize`](/d1/sql-api/sql-statements/#pragma-optimize) for more information.
12+
813
- publish_date: "2025-02-04"
914
title: Fixed bug with D1 read-only access via UI and /query REST API.
1015
description: |-
1116
Fixed a bug with D1 permissions which allowed users with read-only roles via the UI and users with read-only API tokens via the `/query` [REST API](/api/resources/d1/subresources/database/methods/query/) to execute queries that modified databases. UI actions via the `Tables` tab, such as creating and deleting tables, were incorrectly allowed with read-only access. However, UI actions via the `Console` tab were not affected by this bug and correctly required write access.
12-
17+
1318
Write queries with read-only access will now fail. If you relied on the previous incorrect behavior, please assign the correct roles to users or permissions to API tokens to perform D1 write queries.
1419
1520
- publish_date: "2025-01-13"

src/content/release-notes/durable-objects.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@ productLink: "/durable-objects/"
55
productArea: Developer platform
66
productAreaLink: /workers/platform/changelog/platform/
77
entries:
8+
- publish_date: "2025-02-19"
9+
description: |-
10+
SQLite-backed Durable Objects now support `PRAGMA optimize` command, which can improve database query performance. It is recommended to run this command after a schema change (for example, after creating an index). Refer to [`PRAGMA optimize`](/d1/sql-api/sql-statements/#pragma-optimize) for more information.
11+
812
- publish_date: "2025-02-11"
913
description: |-
1014
When Durable Objects generate an "internal error" exception in response to certain failures, the exception message may provide a reference ID that customers can include in support communication for easier error identification. For example, an exception with the new message might look like: `internal error; reference = 0123456789abcdefghijklmn`.
15+
1116
- publish_date: "2024-10-07"
1217
title: Alarms re-enabled in (beta) SQLite-backed Durable Object classes
1318
description: |-
1419
The issue identified with [alarms](/durable-objects/api/alarms/) in [beta Durable Object classes with a SQLite storage backend](/durable-objects/best-practices/access-durable-objects-storage/#sqlite-storage-backend) has been resolved and alarms have been re-enabled.
20+
1521
- publish_date: "2024-09-27"
1622
title: Alarms disabled in (beta) SQLite-backed Durable Object classes
1723
description: |-

0 commit comments

Comments
 (0)