Search code examples
arangodbaql

How can I help an AQL plan in selecting an index?


I have multiple persistent indexes on a collection. One is a timestamp, and the other is a document id. The timestamp is used to select a range of documents. I need to retrieve documents by a range of time over a list of document ids. The AQL would be something like this:

for x in collection filter time1>=x.timestamp&&time2<x.timestamp&&x.id in [<list of ids>] return x

ArangoDB always chooses the timestamp for the index but in most cases, the id should filter more objects initially.

How can I direct the planner to choose the id index over the timestamp one? Alternatively, is there a way to create a combined index that would reduce or eliminate a linear scan for this type of query?

Thanks.


Solution

  • Ad 1) Use multiple filter lines to help the execution planner:

    FOR x IN collection
      FILTER x.id IN [1,2,3]
      FILTER x.timestamp < @time1 AND
             x.timestamp >= @time2
      RETURN x
    

    Ad 2) This depends on how you create the indices; in javascript it's as simple as:

    db.collection.ensureIndex({type: "hash", fields: ["id", "timestamp]"]});
    

    More details in the index documention