Skip to content

Commit 3935dd2

Browse files
[DOC-13962] Automatic Reprepare - Capella (#626)
1 parent 2a302cb commit 3935dd2

2 files changed

Lines changed: 45 additions & 6 deletions

File tree

modules/n1ql/pages/n1ql-language-reference/execute.adoc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Once retrieved, the query engine creates a local cached copy of the prepared sta
6767
An error is returned if the name does not identify a prepared statement.
6868

6969
[[auto-reprepare]]
70-
== Auto-Reprepare
70+
== Automatic Reprepare for Invalid Plans
7171

7272
Before execution, the query engine checks whether the statement plan is still valid -- i.e. that all the indexes and keyspaces to which the plan refers are unchanged.
7373
If any indexes or keyspaces have changed, the statement is automatically prepared again, so that the plan matches the new set of resources.
@@ -76,6 +76,10 @@ If this automatic reprepare succeeds, the statement simply executes as expected.
7676
However, if any required resources are found to be missing, execution of the affected prepared statement fails until those resources are created again.
7777
Once the resources are available again, execution proceeds without any further intervention.
7878

79+
NOTE: The Query Service also provides an opt-in feature that extends this behavior.
80+
When enabled, the service monitors GSI metadata version changes (such as when you create or drop indexes) and reprepares affected statements.
81+
For more information, see xref:n1ql-language-reference/prepare.adoc#auto-reprepare-index[Automatic Reprepare on Index Changes].
82+
7983
[[parameters]]
8084
== Parameters
8185

modules/n1ql/pages/n1ql-language-reference/prepare.adoc

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,48 @@ For more details, refer to xref:n1ql:n1ql-manage/query-settings.adoc#auto-prepar
214214
Auto-prepare is disabled for {sqlpp} requests which contain parameters, if they do not use the PREPARE statement.
215215
endif::flag-devex-rest-api[]
216216

217-
[[auto-reprepare]]
218-
== Manual Reprepare
217+
[[auto-reprepare-index]]
218+
== Automatic Reprepare on Index Changes
219+
220+
The Query Service validates a statement's prepared plan before executing it.
221+
If the plan is invalid—for example, if any indexes or keyspaces become unavailable—the service automatically reprepares the statement.
222+
This is the xref:n1ql-language-reference/execute.adoc#auto-reprepare[default behavior].
223+
224+
The Query Service also provides an opt-in feature that extends this behavior.
225+
When enabled, the service monitors GSI metadata version changes (such as when you create or drop indexes) and flags affected statements for repreparation.
226+
The next time a flagged statement runs, the service generates a new plan to match the updated index set.
227+
This ensures statements automatically use newer, more efficient indexes as they become available.
228+
229+
The following example shows how the feature works when it's active:
230+
231+
. You prepare a statement and no suitable indexes exist to support it;
232+
the Query Service creates a plan that uses a sequential scan.
233+
234+
. You then create a primary index that better supports the statement;
235+
the service flags the statement, and on the next run generates a new plan that uses the primary index.
236+
237+
. You later create a secondary index that is an even better choice for the statement;
238+
the service flags the statement again, and on the next run generates a new plan that uses the secondary index.
219239

220-
[.status]#Couchbase Server 8.0#
240+
If the feature is inactive, the statement continues to use the original plan (such as a sequential scan) even after you create new indexes.
221241

222-
If no indexes exist when you prepare a statement, then the prepared plan uses a sequential scan.
223-
If you create a primary index or a secondary index later, the statement still continues to use the sequential scan and does not automatically benefit from the new indexes.
242+
To enable the feature, contact xref:support:manage-support.adoc[Couchbase Support].
243+
244+
You can also manually reprepare statements at any time.
245+
For more information, see <<manual-reprepare>>.
246+
247+
[IMPORTANT]
248+
====
249+
This feature can increase the load on the Query Service.
250+
Any index change, even one unrelated to a given statement, can trigger a repreparation.
251+
For example, creating or dropping an index that a statement does not use can still flag it for repreparation.
252+
In such cases, the statement reprepares only to select the same plan again, resulting in redundant work.
253+
254+
To reduce this overhead, enable the feature temporarily when you create or drop indexes, and disable it after all statements are reprepared.
255+
If index changes are infrequent, the effect is minimal.
256+
====
257+
258+
== Manual Reprepare
224259

225260
To manually reprepare a statement, update xref:n1ql:n1ql-manage/monitoring-n1ql-query.adoc#sys-prepared[system:prepareds] and unset the `planPreparedTime` field for the statement.
226261

0 commit comments

Comments
 (0)