Query directives provide fine-grained control over how Ditto’s query planner executes your DQL statements. You can use directives to force specific index usage, control index scan behavior, and tune query performance.Documentation Index
Fetch the complete documentation index at: https://docs.ditto.live/llms.txt
Use this file to discover all available pages before exploring further.
Directive Syntax
Directives can be specified in three ways:1. Comment-Style Directives
Include directives as a special comment at the beginning of your query:DQL
DQL
2. USE DIRECTIVES Clause
Specify collection-level directives using theUSE DIRECTIVES clause:
DQL
3. USE INDEX Clause
A shorthand for specifying index directives:DQL
Directive Scope
Directives can be global (affecting the entire statement) or per-collection (affecting a specific collection instance):- Comment-style directives can specify both global and per-collection directives
- USE DIRECTIVES can only specify collection-level directives
- USE INDEX can only specify the index directive for a collection
Global Directives
Global directives affect the entire query statement.#disable_index_scan
Type:boolean (default: false)
Prevents the planner from considering index scans for all collections in the statement.
DQL
#prefer_order
Type:boolean (default: false)
Indicates whether ordering should be prioritized when considering indices. Useful when you want the planner to prefer indices that can satisfy both filtering and ordering requirements.
DQL
#intersects
Type:integer (default: 0)
Indicates the maximum number of indices to consider when generating an intersect scan (combining multiple index scans with AND logic).
DQL
#max_scans
Type:integer (default: 5)
The overall maximum number of index scans permitted when planning a statement.
DQL
#max_inlist
Type:integer (default varies)
Controls the maximum number of items in an IN list that will be pushed down to index scans.
DQL
#max_serial_ranges
Type:integer (default varies)
Controls the maximum number of distinct value ranges for a single index that will be scanned serially.
DQL
#profile
Type:boolean (default: false)
Directs the Query engine to append the system:completed_requests entry to the result set as the final result. Only works for SELECT statements.
DQL
Using
#profile is equivalent to using the PROFILE statement.#disable_special
Type:boolean (default: false)
Disables special-case query optimizations such as aggregate short-circuits.
DQL
#disable_intersect_eo
Type:boolean (default: false)
Disables intersect early-out optimization, forcing the planner to evaluate all intersect scan children.
DQL
#reprepare
Type:boolean (default: false)
Disables caching of the statement plan in the shared statement cache, forcing the statement to always be prepared.
DQL
Per-Collection Directives
Per-collection directives apply to a specific collection instance in your query. In comment-style directives, use the collection alias as the field name.#prefer_order (per-collection)
Same as the global directive but overrides the value for a specific collection.DQL
#intersects (per-collection)
Same as the global directive but overrides the value for a specific collection.DQL
#index
Type:string, array of strings, or null
Specifies the index to use or list of possible indices to pick from when planning index access.
- String value: Forces a specific index (e.g.,
"ix_color") - Array of strings: Provides a list of candidate indices for the planner to choose from (e.g.,
["ix1", "ix2", "ix3"]) - Empty string or
null: Indicates no index scan should be generated and a collection scan should be used - Empty array
[]: Indicates the planner can choose from all available indices (overrides#disable_index_scanfor this collection)
DQL
Using Collection Aliases
When using comment-style directives with collection aliases, specify the alias (not the collection name) in the directive:DQL
Combining Directives
You can combine multiple directives in a single query:DQL
USE DIRECTIVES Examples
TheUSE DIRECTIVES clause is limited to collection-level directives:
DQL
USE INDEX Examples
TheUSE INDEX clause is a shorthand for the #index directive:
DQL
Directive Behavior
- Any elements in the directives JSON that aren’t understood are silently ignored
- The JSON must be syntactically correct, or a parser error will be reported
- If a specified index doesn’t exist or isn’t applicable to the statement, the directive is ignored and regular planning takes place using all available candidate indices
Default Directives
You can set default directives for all queries in your Ditto instance using theDQL_DEFAULT_DIRECTIVES system parameter. These directives apply to every statement automatically, while individual statement directives can override them.
Setting Default Directives:
DQL
- Development: Set
#repreparetotrueduring development to always use fresh query plans - Production: Once statements and indices are finalized, unset
#reprepareto benefit from the statement cache - Global Tuning: Apply performance tuning directives across all queries without modifying each statement
DQL
Default directives are merged with statement-level directives, with statement-level directives taking precedence when there’s a conflict.
See Also
- EXPLAIN - View query execution plans
- PROFILE - Profile query performance
- Indexing - Creating and managing indices
- Access Paths - Understanding query access paths