Most performant query to get subset of documents by keys in AQL?

60 views
Skip to first unread message

Dave Challis

unread,
Jul 31, 2019, 4:57:28 AM7/31/19
to ArangoDB
Is there a preferred or most performant way of querying for a subset of documents in AQL? I know there's a separate API for fetching bulk documents, but this is something I need to do in the middle of a more complex AQL query. I know I can use either:

FOR doc IN myCollection
  FILTER doc
._key == "1" || doc._key == "2" || doc._key == "3" // etc. for 997 more keys
  RETURN doc

Or I could fetch documents directly:

RETURN [ DOCUMENT("myCollection/1"), DOCUMENT("myCollection/2"), DOCUMENT("myCollection/3"), ... ]

Is either of these preferred? Or is there another method I might be missing for doing this in AQL?

Kerry Hormann

unread,
Jul 31, 2019, 7:47:13 PM7/31/19
to ArangoDB
I would think the most efficient way to do this is by using an array to define the list of keys.  The array could be another collection, a static array, or even a bindVariable.

Here's an example, retrieving a few documents from the list of collections:

LET keys = [ '_apps', '_fishbowl', '_jobs', '_routing' ]
FOR c IN COLLECTIONS()
    FILTER c.name in keys
    RETURN c

The biggest caveat here is to make sure the filter clause is covered by an index.  I'm using "c.name" here, which is NOT indexed (this list is pretty small), but "_key" is always indexed, so you should be OK.

Using this pattern, you could also write your query like this:

FOR doc IN myCollection
  FILTER doc
._key in [ '1', '2, '3' /* etc. for 997 more keys */ ]
  RETURN doc


Or this:

FOR doc IN myCollection
  FILTER doc
._key in @keys /* @keys is bound at runtime to your array */
  RETURN doc



Dave Challis

unread,
Aug 1, 2019, 11:16:23 AM8/1/19
to ArangoDB
Many thanks, that does look a lot easier to write queries for too.
Reply all
Reply to author
Forward
0 new messages