You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: modules/n1ql/pages/n1ql-language-reference/execute.adoc
+5-1Lines changed: 5 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -67,7 +67,7 @@ Once retrieved, the query engine creates a local cached copy of the prepared sta
67
67
An error is returned if the name does not identify a prepared statement.
68
68
69
69
[[auto-reprepare]]
70
-
== Auto-Reprepare
70
+
== Automatic Reprepare for Invalid Plans
71
71
72
72
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.
73
73
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.
76
76
However, if any required resources are found to be missing, execution of the affected prepared statement fails until those resources are created again.
77
77
Once the resources are available again, execution proceeds without any further intervention.
78
78
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].
Copy file name to clipboardExpand all lines: modules/n1ql/pages/n1ql-language-reference/prepare.adoc
+40-5Lines changed: 40 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -214,13 +214,48 @@ For more details, refer to xref:n1ql:n1ql-manage/query-settings.adoc#auto-prepar
214
214
Auto-prepare is disabled for {sqlpp} requests which contain parameters, if they do not use the PREPARE statement.
215
215
endif::flag-devex-rest-api[]
216
216
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.
219
239
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.
221
241
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
224
259
225
260
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.
0 commit comments