Speeding Up Ad‑hoc Queries on Large Couchbase Buckets with Parallel‑Enabled N1QL Indexes
Learn how a parallel‑enabled secondary index can turn seconds‑long ad‑hoc queries into sub‑second responses on large Couchbase buckets.
30 Oct 2026, 07:24 UTC

Problem: Slow ad‑hoc queries on large Couchbase buckets
When an application runs a query that filters on fields not covered by the primary key, Couchbase must examine every document in the bucket. On a bucket with millions of rows, this full‑bucket scan can take several seconds, which hurts user experience and increases load on the cluster. The latency becomes especially noticeable for ad‑hoc exploratory queries that are issued irregularly by developers or analysts.
Thesis: Parallel‑enabled secondary indexes accelerate ad‑hoc queries
The N1QL secondary index feature can be created with the `PARALLEL SCAN` clause. Once the index exists, the query optimizer can use it and the engine will scan the index partitions concurrently, cutting latency dramatically on large datasets. This approach turns queries that previously required seconds into sub‑second responses without changing the application code.
Full bucket scans versus index lookups
A full bucket scan reads every row, which is O(N) in the number of documents. An index lookup reads only the index entries that match the query predicate, which is typically O(log N) plus the number of matching entries. For read‑heavy workloads, the index avoids scanning the entire bucket and reduces I/O pressure on the data service.
Parallel scan in Couchbase
The `PARALLEL SCAN` option tells the index builder to read index partitions at the same time. It is ignored on Couchbase Server versions prior to 7.2, so the index must be created on 7.2 or later. When the index is online and the bucket size exceeds the default threshold (64 MB), the optimizer automatically selects the parallel scan for queries that can use the index. The feature is transparent to the query; the user does not need to reference it explicitly.
Worked example
- Create a test bucket (or use an existing one).
- Insert 100 k documents with a field `category` that will be queried.
- Build a secondary index that covers the query and enables parallel scan:
CREATE INDEX idx_category ON `bucket-name`(category) USING GSI PARALLEL SCAN;
- Run the query and note the execution time:
EXPLAIN SELECT COUNT(*) FROM `bucket-name` WHERE category = 'news';
- Measure the elapsed time with the Couchbase UI or `GET META`‑related timing information. In a typical 10 M‑document bucket, the same query without the index may take 3–5 seconds, while with the parallel index it often returns in under 1 second.
Verification steps
- Provision a test bucket and load a realistic data set (e.g., 100 k–1 M documents).
- Create the secondary index with `PARALLEL SCAN` as shown above.
- Run the target query with `EXPLAIN` before and after the index; the plan should display `ParallelScan` and a lower estimated cost.
- Check the index status via the Couchbase UI or `GET INDEXES` to confirm it is `online` and that `parallel` is enabled.
Trade‑off: write latency and storage overhead
Every index update must be written to the index structure, which adds latency to write operations. For workloads with high write throughput, monitor the index build progress and consider limiting the number of secondary indexes. Additionally, each index consumes memory and disk space proportional to the indexed data, so keep the index set minimal to avoid degrading overall performance. The storage cost is a trade‑off that must be weighed against the query latency benefit.
Actionable closing
To evaluate the benefit in your environment:
- Provision a test bucket and load a realistic data set (e.g., 100 k–1 M documents).
- Create the secondary index with `PARALLEL SCAN` as shown above.
- Run the target query with `EXPLAIN` before and after the index; the plan should display `ParallelScan` and a lower estimated cost.
- Check the index status via the Couchbase UI or `GET INDEXES` to confirm it is `online` and that `parallel` is enabled.
If the query latency drops to sub‑second levels and the additional write cost remains acceptable, adopting a parallel‑enabled secondary index is a practical engineering decision for large‑scale Couchbase deployments.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.