Skip to content

Commit 39f94fd

Browse files
authored
Add DEFER clause (#1556)
1 parent 776fc62 commit 39f94fd

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

src/content/doc-surrealql/statements/define/indexes.mdx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ DEFINE INDEX [ OVERWRITE | IF NOT EXISTS ] @name
3535
[ @special_clause ]
3636
[ COMMENT @string ]
3737
[ CONCURRENTLY ]
38+
[ DEFER ]
3839
```
3940

4041
The `@special_clause` part of the statement is an optional part in which an index can be declared for special usage such as guaranteeing unique values, full-text search, and so on. The available clauses are:
@@ -563,6 +564,21 @@ SELECT * FROM account WHERE name CONTAINS "Billy McConnell" EXPLAIN FULL;
563564
SELECT * FROM account WHERE name CONTAINSANY ["Billy McConnell"] EXPLAIN FULL;
564565
```
565566

567+
## The `DEFER` clause
568+
569+
<Since v="v2.4.0" />
570+
571+
Index updates in SurrealDB occur synchronously during document operations. This ensures **immediate consistency**, in which all reads return the most recent write. However, this can become a bottleneck during high-volume parallel ingestion, leading to write-write conflicts and increased latency, particularly with Full-Text or Vector indexes.
572+
573+
The `DEFER` clause can be used in this case if **eventual consistency** is acceptable, namely a setting in which reads may return stale data for a short period, but will eventually converge to the most recent write. An index with this clause will be enqueued in a persistent background queue so that ingestion and indexing are decoupled.
574+
575+
```surql
576+
DEFINE ANALYZER simple TOKENIZERS blank,class FILTERS lowercase;
577+
DEFINE INDEX title_index ON blog FIELDS title SEARCH ANALYZER simple BM25(1.2,0.75) HIGHLIGHTS DEFER;
578+
```
579+
580+
Note: As unique indexes offer a guarantee that no records that contravene the index will ever exist, the `UNIQUE` clause cannot be used together with `DEFER`.
581+
566582
## Performance Implications
567583

568584
When defining indexes, it's essential to consider the fields most frequently queried or used to optimize performance.

0 commit comments

Comments
 (0)