Skip to main content
You can preface a DQL statement with ADVISE to analyse it and receive advice on which indexes would benefit it. The statement is planned but never executed, and the result is a single document describing the indexes the planner recommends.
The statement isn’t executed when using ADVISE, only the parsing and planning stages take place. No documents are read or modified.
ADVISE is the mechanism the planner points you to when a query cannot run efficiently β€” or at all β€” without index support. For example, a join whose inner leg has no index on its join key fails with an error that explicitly suggests running ADVISE on the query. ADVISE Syntax Diagram

Which statements can be advised

ADVISE can preface most statements β€” anything except a nested EXPLAIN, PROFILE, or ADVISE. Advice is only produced for statements that scan a collection: Any other statement β€” including an INSERT with literal VALUES β€” is accepted but returns an outcome of no advice available for statement.

The advice document

The result is a single document with an advice field containing: Each recommendation combines the indexable clauses of a statement into as few composite indexes as possible. Keys are ordered as equality keys first, then ORDER BY keys, then range keys, followed by projected fields when a covering index is worthwhile. Aliased ORDER BY fields are resolved back to the underlying document field.

Examples

A statement with nothing to index on returns an outcome rather than suggestions:
DQL
produces:
An equality predicate is advised as a single-key index:
DQL
produces:
Equality and range predicates on the same collection are combined into one composite index, with the equality key ordered ahead of the range key:
DQL
produces:

ADVISE AND PROVISION

Adding the optional AND PROVISION clause makes ADVISE also create the suggested indexes as part of the same statement:
DQL
The recommendations that were created successfully are reported under createdIndexes (in place of suggestedIndexes):
Any index that could not be created is reported under failedIndexes instead, each entry carrying an error describing why creation failed.
ADVISE is currently supported on the small peer only.