---
name: cloudant-gen2-query-optimizing
title: 'IBM Cloudant Query performance guide: indexes and operators'
description: Optimize IBM Cloudant Query performance by avoiding large scans, ordering index fields, using index-friendly operators, and using partial indexes.
last-updated: 2026-12-01
---

> ## Documentation Index
> The table of contents for this documentation set is at https://cloud.ibm.com/docs/cloudant-gen2?format=markdown
> The index for all IBM Cloud docs is at: https://cloud.ibm.com/docs/llms.txt
> Use these files to discover more information as needed.

# IBM Cloudant Query performance guide: indexes and operators
{: #cloudant-query-performance-guide}

Optimize IBM Cloudant Query performance by avoiding large scans, ordering index fields, using index-friendly operators, and using partial indexes.
{: shortdesc}

This guide describes the essential techniques for optimizing IBM Cloudant Query performance. It starts with the most foundational optimizations and progresses to advanced techniques. By following this guide, you can convert slow, expensive queries into fast, efficient ones that scale with your data.

The guide covers the following topics:

- **Concepts:**
    - Selectivity of indexes
- **Foundation topics**:
    - Avoid large scans
    - Field ordering in compound indexes
    - Use operators that allow index use
    - Avoiding missing documents in JSON indexes
    - Monitoring performance
- **Advanced topics**:
    - Partial indexes
    - Covering indexes
    - Index consolidation strategy

The guide ends with a production readiness checklist.

## Key concept: selectivity
{: #key-concept-selectivity}

Performant and scalable queries depend on matching your query needs with indexes that efficiently serve those queries. A key concept in building such indexes is _selectivity_, which describes how well an index helps narrow down the result set.

A _high selectivity_ index (or _highly selective_ index) dramatically reduces the number of documents matched by a query. In contrast, a _low selectivity_ index matches many documents and offers less performance benefit.

- For an orders table, examples of _high selectivity_ indexes include:
    - `order_id`: Each order has a unique ID, so queries match at most one document.
    - `username`: Each user has far fewer orders than the total number of orders.
- Examples of _low selectivity_ indexes include:
    - `status`: Many orders can share the same status.
    - `country`: A broad field like this likely matches many orders.

Understanding selectivity helps you design indexes that make queries faster and more efficient.

## Foundation: avoid large database scans
{: #foundation-avoid-large-database-scans}

**Impact: 1000x+ performance improvement**

The most severe performance problem in IBM Cloudant Query is a large database scan. Two causes of this problem exist:

- The query uses a _low selectivity_ index, which causes many documents to be scanned to generate a much smaller result set.
- The query can't use a user-defined index, so it falls back on the built-in `_all_docs` index, which scans all the documents in the database.

### How to identify large scans
{: #how-to-identify-large-scans}

Use the `_explain` endpoint to check which index your query uses:

```bash
POST /orders/_explain
{
  "selector": {
    "username": "bob@email.com",
    "status": "cancelled",
    "date": {"$gte": "2018-01-01", "$lt": "2019-01-01"}
  }
}
```
{: codeblock}

If the response shows a specific index, evaluate whether the index is _high_ or _low_ selectivity. In this example, an index on the `status` field is likely a low selectivity index, so this query needs work:

```json
{
  "index": {
    "ddoc": "_design/status-index",
    "def": {
      "fields": [
        {
          "status": "asc"
        }
      ]
    },
    "name": "d479bdddf50865e520a0193704c8b93a3bd48f77",
    "type": "json"
  }
}
```
{: codeblock}

If the response shows `"name": "_all_docs"`, your query scans the entire database:

```json
{
  "index": {
    "ddoc": null,
    "name": "_all_docs",
    "type": "special"
  }
}
```
{: codeblock}

### Solution: create appropriate indexes
{: #solution-create-appropriate-indexes}

For the query above, the optimal index is a compound (multi-field) JSON index covering the fields you're querying:

```bash
POST /orders/_index
{
  "index": {
    "fields": ["username", "status", "date"]
  },
  "name": "username-status-date-index",
  "type": "json"
}
```
{: codeblock}

**Performance impact**: In a 100,000 document database, this single change can reduce query time from 20+ seconds to under 10 milliseconds.

### Production safety: prevent accidental large scans
{: #safety-prevent-accidental-large-scans}

If you know a certain index will be highly selective for a query, combine `use_index` with `allow_fallback: false` to force use of that index:

```json
{
  "selector": {"username": "bob@email.com", "status": "cancelled"},
  "use_index": ["my_design_doc", "username-index"],
  "allow_fallback": false
}
```
{: codeblock}

This query returns an error if `my_design_doc/username-index` is unusable for the query, rather than silently performing an expensive scan. The default for `allow_fallback` is `true`, meaning that IBM Cloudant uses a different index to respond to the query, potentially resulting in a large scan.

If you don't supply `use_index`, `allow_fallback: false` still prevents a query from using the `_all_docs` index:

```json
{
  "selector": {"username": "bob@email.com"},
  "allow_fallback": false
}
```
{: codeblock}

This query returns an error if no suitable user-defined index exists, which avoids an expensive scan.

See the [Query API documentation](https://cloud.ibm.com/docs/apis/cloudant/cloudant-gen2#postfind) for more details on these query parameters.

Consistently combining `use_index` with `allow_fallback` is recommended practice for critical queries.
{: tip}

## Foundation: field ordering in compound indexes
{: #foundation-field-ordering-in-compound-indexes}

**Impact: 100x performance improvement**

Once you have indexes, the most common mistake is incorrect field ordering in compound indexes. This can turn an efficient query into one that scans 40%+ of your database.

### Critical rule 1: compound index selectivity
{: #critical-rule-1-compound-index-selectivity}

Order your equality fields (non-range fields) by selectivity - most selective first:

- `user` field: 50 docs per user (high selectivity) — put this first.
- `status` field: 10,000 docs per status (medium selectivity).

**Optimal index ordering**: `["username", "status", "date"]` where `date` is the range field.

### Critical rule 2: range fields must come last
{: #critical-rule-2-range-fields-must-come-last}

When creating compound indexes, any field used with range operators (`$gt`, `$gte`, `$lt`, `$lte`) must appear **last** in the field list.

**Wrong - causes partial database scans:**

```jsonc
{
  "index": {
    "fields": ["date", "username", "status"]  // Bad - range field first
  }
}
```
{: codeblock}

**Correct - enables efficient seeking:**

```jsonc
{
  "index": {
    "fields": ["username", "status", "date"]  // Good - range field last
  }
}
```
{: codeblock}

### Critical rule 3: sort fields must come last
{: #critical-rule-3-sort-fields-must-come-last}

Indexes are also used for sorting query results. When your query filters on fields like `username` and `status`, and sorts by another field like `date`, the index must include all three fields — with the sort field **last**.

For example, to support the following query:

```json
{
  "selector": { "username": "bob@email.com", "status": "cancelled" },
  "sort": [ { "date": "asc" } ]
}
```
{: codeblock}

You need an index on `["username", "status", "date"]`. In this structure, the sort field (`date`) must come last. This ordering ensures IBM Cloudant can use the index both to filter and to sort efficiently.

### Why field ordering matters
{: #why-field-ordering-matters}

IBM Cloudant indexes work like sorted tables. An index on `["username", "status", "date"]` creates entries sorted first by user, then by status within each user, then by date within each user-status combination:

```json
["alice", "paid", "2018-01-15"]
["alice", "paid", "2018-02-20"]
["bob", "cancelled", "2018-01-10"]
["bob", "cancelled", "2018-01-15"]
["bob", "paid", "2018-01-12"]
```
{: codeblock}

When using an efficient index for a query like `user: "bob", status: "cancelled", date: {$gte: "2018-01-01"}`, IBM Cloudant can:

1. Jump directly to Bob's records.
2. Find the cancelled orders within Bob's records.
3. Scan efficiently through the date range.

If date were first, IBM Cloudant would need to examine every document in the date range across all users and statuses.


## Foundation: use operators that allow index use
{: #foundation-use-operators-that-allow-index-use}

**Impact: 10-100x performance difference**

It's essential to combine high-selectivity indexes with query selectors that allow use of those indexes. The operators used in the conditions of a selector can prevent index use.

When IBM Cloudant runs a query, only some operators allow it to use an index to answer the query efficiently. If you have a high-selectivity index on a field, but the operator in the condition for that field doesn't support index usage, the query can't use the index. As a result, the query is inefficient.

JSON and text indexes support use of different operators in conditions. If a JSON index doesn't support the operator you need, a text index might do.

The following table summarizes the key properties of indexes and the operators that they support:

| Characteristic                | JSON index                             | Text index                         |
| ----------------------------- | -------------------------------------- | ---------------------------------- |
| **Underlying technology**     | CouchDB MapReduce View (B-tree)        | Apache Lucene                      |
| **Primary use-case**          | Latency-sensitive, predictable queries | Flexible, ad-hoc queries           |
| **Performance**               | Faster for exact matches and ranges    | Slower but more versatile          |
| **Document requirements**     | Must contain ALL indexed fields        | Flexible - missing fields OK       |
| **Strongly typed**            | No                                     | Yes                                |
|                               |                                        |                                    |
| **Combination operators**     |                                        |                                    |
| `$and`                        | ✔️ Supported                           | ✔️ Supported                       |
| `$or`                         | ❌ Not supported                        | ✔️ Supported                       |
| `$nor`                        | ❌ Not supported                        | ✔️ Supported                       |
| `$not`                        | ❌ Not supported                        | ✔️ Supported                       |
|                               |                                        |                                    |
| **Condition operators**       |                                        |                                    |
| `$eq` (equality)              | ✔️ Optimal                             | ✔️ Supported                       |
| `$gt`, `$gte`, `$lt`, `$lte`  | ✔️ Optimal for ranges                  | ✔️ Supported                       |
| `$exists`                     | ✔️ Supported                           | ✔️ Supported                       |
| `$beginsWith`                 | ✔️ Supported                           | ✔️ Supported                       |
| `$ne` (not equal)             | ❌ Not supported                        | ✔️ Supported                       |
| `$in`                         | ❌ Not supported                        | ✔️ Supported                       |
| `$size`                       | ❌ Not supported                        | ✔️ Supported                       |
| `$type`                       | ❌ Not supported                        | ✔️ Supported                       |
| `$text`                       | ❌ Not supported                        | ✔️ Full-text search                |
| `$regex`                      | ❌ Not supported                        | ❌ Not supported                    |
|                               |                                        |                                    |
| **Field Handling**            |                                        |                                    |
| Sort order                    | Fixed by index field ordering          | Flexible                           |
| Array fields                  | ❌ Not supported                        | ✔️ Full support with `[]` notation |
| Nested objects (dot notation) | ✔️ Supported                           | ✔️ Supported                       |
| Missing fields                | Excludes documents                     | Includes documents                 |
|                               |                                        |                                    |
| **Query page size limits**    |                                        |                                    |
| Global query                  | ???                                    | 200                                |
| Partition-scoped query        | ???                                    | 200                                |
{: caption="Comparison of JSON and text indexes" caption-side="bottom"}

### Index type selection guidelines
{: #index-type-selection-guidelines}

Choose JSON indexes when:

- Query patterns are predictable and consistent.
- You need maximum performance for exact matches.
- Documents have consistent structure.
- You're primarily using `$and`, `$eq`, and range operators.

Choose text indexes when:

- You need `$or` operations across multiple fields.
- Documents have varying structures.
- You need full-text search capabilities.
- Query patterns are ad hoc or user-driven.
- You're using `$ne`, `$exists`, or complex operators.

The following example shows a decision matrix:

```jsonc
// Predictable e-commerce queries → JSON index
{
  "selector": {
    "username": "bob@email.com",
    "status": "paid",
    "date": {"$gte": "2018-01-01"}
  }
}

// Flexible search interface → Text index
{
  "selector": {
    "$or": [
      "status": {"$in": ["paid", "in-progress"]}
      "category": "electronics"
    ]
  }
}
```
{: codeblock}

## Foundation: query performance monitoring
{: #foundation-query-performance-monitoring}

**Impact: Ongoing optimization and problem detection**

Efficient querying requires continuous monitoring. IBM Cloudant provides tools to measure and track query performance over time.

IBM Cloudant Query provides the `execution_stats` field in the response to help evaluate your queries. See [Monitor for inefficient IBM IBM Cloudant queries](https://cloud.ibm.com/docs/cloudant-gen2?topic=cloudant-gen2-monitor-for-inefficient-query-requests&format=markdown) for a detailed guide to the metrics IBM Cloudant provides for analysing query execution.

## Warning: document structure requirements for JSON indexes
{: #warning-document-structure-requirements-for-json-indexes}

**Impact: Queries may return incomplete results or fail unexpectedly**

JSON indexes in IBM Cloudant have a crucial constraint that differs from SQL databases: **a document must contain ALL indexed fields to be included in the index**.

### Problem
{: #document-structure-problem}

If you create an index on `["username", "status", "date"]`, any document missing even one of these fields is excluded from the index entirely. This means:

```jsonc
// This document WON'T be indexed
{
  "_id": "order123",
  "username": "bob@email.com",
  "status": "paid"
  // Missing "date" field
}
```
{: codeblock}

### Solutions
{: #document-structure-solutions}

**Option 1: Ensure consistent document structure**

```jsonc
// Always include all indexed fields
{
  "_id": "order123",
  "username": "bob@email.com",
  "status": "paid",
  "date": null  // Use null for missing values
}
```
{: codeblock}

**Option 2: Create multiple JSON indexes for different document structures**

```jsonc
// Index for documents with all fields
{"fields": ["username", "status", "date"]}

// Index for documents without date
{"fields": ["username", "status"]}
```
{: codeblock}

**Option 3: Use text indexes for flexible field requirements**

```json
{
  "index": {
    "fields": [
      {"name": "username", "type": "string"},
      {"name": "status", "type": "string"},
      {"name": "date", "type": "string"}
    ]
  },
  "type": "text"
}
```
{: codeblock}

A text index will index documents missing the `date` (or other) fields.

## Advanced: partial indexes
{: #advanced-partial-indexes}

**Impact: 50-90% reduction in index size and query latency**

Partial indexes are an optimization technique that pre-filters documents before indexing. They create smaller, faster indexes and reduce storage costs.

### When to use partial indexes
{: #advanced-when-to-use-partial-indexes}

Partial indexes are ideal when:

- You consistently query a subset of documents (for example, only active records).
- Your database contains large amounts of irrelevant data for most queries.
- You want to reduce IBM Cloudant storage costs (pay-per-GB pricing).

### Creating partial indexes
{: #advanced-creating-partial-indexes}

[Use `partial_filter_selector`](https://cloud.ibm.com/docs/apis/cloudant/cloudant-gen2#postfind) to specify which documents to include:

```json
{
  "index": {
    "fields": ["date", "category"]
  },
  "partial_filter_selector": {
    "status": "published"
  },
  "name": "published-articles-index"
}
```
{: codeblock}

This index only includes documents where `status: "published"`, making it much smaller and faster for queries on published content.

The `partial_filter_selector` can include any valid selector, not just simple single field selectors as shown in the example.
{: tip}

### Real-world example: blog system
{: #advanced-partial-indexes-example}

**Scenario**: A blog database with 100,000 articles, where 80,000 are drafts and 20,000 are published.

**Traditional index** (covers all documents):

```json
{"fields": ["category", "publishDate"]}
```
{: codeblock}

**Partial index** (only published articles):

```json
{
  "index": {"fields": ["category", "publishDate"]},
  "partial_filter_selector": {"status": "published"}
}
```
{: codeblock}

Benefits:

- Index size: 80% smaller
- Query speed: 2-3x faster
- Storage cost: Significantly reduced
- Cache efficiency: More relevant data stays in memory

### Ensuring query compatibility with partial indexes
{: #advanced-partial-indexes-ensuring-query-compatibility}

To use the index, your query selector must be **compatible** with the partial filter.

This works:

```jsonc
// Compatible - query includes the partial filter condition
{
  "selector": {
    "status": "published",    // Matches partial filter
    "category": "technology"
  }
}
```
{: codeblock}

This doesn't work:

```jsonc
// Incompatible - query doesn't match partial filter
{
  "selector": {
    "status": "draft",        // Contradicts partial filter
    "category": "technology"
  }
}
```
{: codeblock}

## Advanced: covering indexes
{: #advanced-covering-indexes}

**Impact: 2-5x performance improvement for read-heavy workloads**

Covering indexes are an optimization technique that can answer queries entirely from the index without retrieving the original documents.

### Understanding covering indexes
{: #advanced-understanding-covering-indexes}

A covering index occurs when your index contains all the fields needed to satisfy both the query selector **and** the returned fields:

```jsonc
// Index covers these fields
{
  "index": {
    "fields": ["username", "status", "date", "amount"]
  }
}

// Query only needs indexed fields
{
  "selector": {"username": "bob@email.com", "status": "paid"},
  "fields": ["username", "date", "amount"]  // All fields in index
}
```
{: codeblock}

IBM Cloudant can return results directly from the index without reading the original documents, providing significant performance benefits.

### Designing for coverage
{: #advanced-designing-covering-indexes}

Plan your indexes around common query patterns:

```javascript
// Analyze your query patterns
const commonQueries = [
  {selector: {username: "...", status: "..."}, fields: ["date", "amount"]},
  {selector: {status: "...", date: {...}}, fields: ["username", "amount"]},
  {selector: {username: "..."}, fields: ["status", "date", "total"]}
];

// Design covering index
{
  "index": {
    "fields": ["username", "status", "date", "amount", "total"]
  }
}
```
{: codeblock}
{: node}

The [`_explain` endpoint](https://cloud.ibm.com/docs/apis/cloudant/cloudant-gen2#postexplain) contains `"covering": true` when it finds an index that can cover the query.

## Advanced: index consolidation strategy
{: #advanced-index-consolidation-strategy}

Instead of creating many narrow indexes, consider fewer broader compound indexes that can cover multiple query patterns. Using fewer reduces the time required to build indexes.

**Instead of:**

```json
{"fields": ["username"]}
{"fields": ["status"]}
{"fields": ["date"]}
{"fields": ["username", "date"]}
```
{: codeblock}

**Use:**

```json
{"fields": ["username", "status", "date", "amount"]}
```
{: codeblock}

This single index can efficiently serve queries filtering on:

- `username` only
- `username` and `status`
- `username`, `status`, and `date` range
- All the above combinations, with covering benefits when `amount` is requested

## Production deployment checklist
{: #production-deployment-checklist}

Before deploying queries to production, verify:

### Index strategy review
{: #production-deployment-checklist-index-strategy-review}

- All production queries have appropriate indexes
- Compound indexes follow the range-field-last rule
- Field ordering reflects actual selectivity in your data
- No queries accidentally fall back to `_all_docs`

### Performance validation
{: #production-deployment-checklist-performance-validation}

- Test queries with production-scale data volumes
- Monitor efficiency ratios (aim for >50%)
- Validate execution times meet SLA requirements
- Consider partial indexes for cost optimization

### Monitoring setup
{: #production-deployment-checklist-monitoring-setup}

- Enable `execution_stats` logging
- Set up alerts for poor efficiency ratios
- Monitor index usage patterns
- Plan for query performance regression testing

### Error handling
{: #production-deployment-checklist-error-handling}

- Use `allow_fallback: false` to prevent expensive accidents
- Handle index selection errors gracefully
- Plan for index building time during deployments

## Further reading
{: #further-reading}

The IBM Cloudant blog has further in-depth articles on exploring and improving query performance:

- [Explaining Explain](https://blog.cloudant.com/2024/01/17/Explaining-Explain.html){: external} describes how to interpret and use the output of the `_explain` request.
- [Optimising IBM Cloudant Queries](https://blog.cloudant.com/2020/05/20/Optimising-Cloudant-Queries.html){: external} describes how to improve the performance of queries.
- [Avoiding Query Fallback](https://blog.cloudant.com/2025/02/04/Avoid-Fallback.html){: external} shows a technique to prevent critical queries from falling back to full table scans.

## Conclusion
{: #conclusion}

Optimizing IBM Cloudant Query performance is a systematic process. It starts with avoiding the most expensive mistakes (large database scans) and progresses through increasingly sophisticated techniques. By following this guide, you complete the following tasks:

1. **Foundation**: Create appropriate indexes and avoid large scans
2. **Structure**: Order compound index fields correctly with ranges last
3. **Operators**: Choose efficient query operators
4. **Compatibility**: Understand document structure requirements
5. **Monitoring**: Continuously measure and optimize performance
6. **Advanced**: Use partial indexes and covering patterns

Small changes in index design can produce large performance improvements. A properly optimized query that examines two documents to return two results outperforms an unoptimized query that examines 50,000 documents for the same two results.

The difference between a well-optimized and a poorly optimized query affects more than performance. It determines whether an application scales or fails under load.