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
In the above examples, the `collect` method is used to extract the `name` attribute from each document in the result set.
149
149
150
+
## 7.8 Prepared Statement Plan Caching
151
+
152
+
Couchbase Server can cache the query execution plan for a SQL++ query so that subsequent executions skip the planning step. This is controlled by the `adhoc` query option: `adhoc: false` tells the server to prepare and cache the plan on first execution and reuse it on subsequent ones.
153
+
154
+
### Default behaviour
155
+
156
+
By default CouchbaseOrm runs queries with `adhoc: true` (the Couchbase SDK default), meaning no plan caching. This preserves the existing behaviour — you opt into plan caching explicitly.
157
+
158
+
### Enabling caching for a specific call
159
+
160
+
Pass `adhoc: false` directly to the query method to prepare and cache the plan (useful for frequently repeated queries):
161
+
162
+
```ruby
163
+
# Cache the plan for this query
164
+
N1QLTest.by_rating(key:1, adhoc:false)
165
+
166
+
# Relation query with plan caching
167
+
User.where(country:'FR').with(adhoc:false).to_a
168
+
```
169
+
170
+
### Enabling caching for a specific `n1ql` definition
171
+
172
+
Set `adhoc: false` in the macro options to always cache the plan for that particular query:
Override the thread-local config to change the default for all queries in the current thread:
181
+
182
+
```ruby
183
+
# Enable plan caching for all queries in this thread
184
+
CouchbaseOrm::N1ql.config(adhoc:false)
185
+
```
186
+
187
+
### Override priority
188
+
189
+
From highest to lowest: **per-call kwarg** > **per-`n1ql`-definition option** > **`N1ql.config`** > **default (`true`)**.
190
+
150
191
## 7.7 Indexing for SQL++
151
192
152
193
To optimize the performance of SQL++ queries, it's important to create appropriate indexes on the fields used in the query conditions. Couchbase Server provides a way to create indexes using the Index service.
0 commit comments