Create and manage indexes in DQL to improve query performance by reducing lookup times, especially for large datasets or frequently accessed records.
Operation | Description |
---|---|
CREATE INDEX | Creates a simple on a specified field |
DROP INDEX | Removes an existing index |
USE INDEX | Force a query to use a specific index |
USE DIRECTIVES | Advanced control over index usage and query optimization |
SELECT from system:indexes | Lists all indexes in the database |
execute
SDK operation. Using any of these commands through registerObserver
or registerSubscription
will fail with an “invalid query not supported” response.color
field in the cars
collection:
IF NOT EXISTS
clause allows the statement to execute successfully even if an index with the same name already exists on the collection. This is useful for initialization scripts that may run multiple times.
IF NOT EXISTS
clause only checks if an index with the same name exists - it does not verify the field definition. If an index with the same name already exists (even on a different field), the CREATE INDEX IF NOT EXISTS will succeed without creating a new index or modifying the existing one. To change an index definition, you must explicitly DROP the existing index first before creating the new one.CREATE INDEX
operation returns an empty result set (no items) upon successful completion. Successful execution without errors indicates that the index was created successfully - if there are any issues (such as duplicate index names or invalid syntax), an error will be thrown. To verify that your index was created successfully, you can query the system:indexes
collection.system:indexes
collection:
system:indexes
collection follows this schema:
color_idx
index from the cars
collection:
IF EXISTS
clause allows the statement to execute successfully even if the index doesn’t exist. This is useful for cleanup scripts that may run multiple times or when you’re unsure if an index exists.
IF EXISTS
clause, attempting to drop a non-existent index will raise an error.USE INDEX
clause is the simple way to force a query to use a specific index. It appears after the collection name in SELECT statements:
USE DIRECTIVES
syntax which allows fine-grained control over query optimization using JSON directives:
Directive | Type | Default | Description |
---|---|---|---|
#index | string, array, or null | auto-select | Specifies which index to use |
#prefer_order | boolean | false | Prefer indexes that optimize ORDER BY over WHERE |
#disable_index_scan | boolean | false | Disable all index scans (global only) |
#index
directive accepts different value types.
Value | Behavior |
---|---|
"index_name" | Use the specified index if available |
null or "" | Force collection scan (no indexes) |
[] | Auto-select from available indexes |
ix2
being used and not ix1
.
system:indexes
to see if the index existssystem:indexes
DROP INDEX IF EXISTS
for safety)API/Feature | Index Support |
---|---|
execute SDK API | ✅ Supported |
registerObserver SDK API | ✅ Supported |
registerSubscription SDK API | ❌ Not Supported |
Big Peer HTTP API | ❌ Not Supported |
SDKs with in-memory storage | ❌ Not Supported |
system:indexes
collection to track your indexesCREATE INDEX ... 'WHERE status = 'active'
)UPPER(field1)
)OR
/ AND
clauses