Arangodb: AQL optimizer: index utilized if sorting expression features ternary operator?

Created on 17 Nov 2017  路  1Comment  路  Source: arangodb/arangodb

This question was brought up in #3700

FOR x in [{a:1},{a:2},{a:3},{a:4},{a:5}]
FOR y in [{b:'a', a:1},{b:'b', a:2},{b:'c', a:3},{b:'d', a:4},{b:'e', a:5}]
FILTER y.a == x.a
SORT (@sortBy == 'x' ? x.a : y.b) DESC
RETURN {x: x.a, y: y.b}

It's unclear if the optimizer is smart enough in that case to use an index for sorting if available. Please analyze, and if it's not the case, label this as feature request.

1 Question 2 Fixed 3 Optimizer performance

Most helpful comment

arangod> db.f1.ensureIndex({"type":"skiplist", "fields" : ["v"]})
arangod> q=`FOR x in f1 SORT (@sortBy == 'x' ? x.v : y.b) DESC RETURN x`
arangod> db._explain(q,{sortBy: "x" })
Query string:
 FOR x in f1 SORT (@sortBy == 'x' ? x.v : x.b) DESC RETURN x

Execution plan:
 Id   NodeType        Est.   Comment
  1   SingletonNode      1   * ROOT
  6   IndexNode          0     - FOR x IN f1   /* reverse skiplist index scan */
  5   ReturnNode         0       - RETURN x

Indexes used:
 By   Type       Collection   Unique   Sparse   Selectivity   Fields    Ranges
  6   skiplist   f1           false    false       100.00 %   [ `v` ]   *

Optimization rules applied:
 Id   RuleName
  1   use-indexes
  2   use-index-for-sort
  3   remove-unnecessary-calculations-2


arangod> 

YES!

>All comments

arangod> db.f1.ensureIndex({"type":"skiplist", "fields" : ["v"]})
arangod> q=`FOR x in f1 SORT (@sortBy == 'x' ? x.v : y.b) DESC RETURN x`
arangod> db._explain(q,{sortBy: "x" })
Query string:
 FOR x in f1 SORT (@sortBy == 'x' ? x.v : x.b) DESC RETURN x

Execution plan:
 Id   NodeType        Est.   Comment
  1   SingletonNode      1   * ROOT
  6   IndexNode          0     - FOR x IN f1   /* reverse skiplist index scan */
  5   ReturnNode         0       - RETURN x

Indexes used:
 By   Type       Collection   Unique   Sparse   Selectivity   Fields    Ranges
  6   skiplist   f1           false    false       100.00 %   [ `v` ]   *

Optimization rules applied:
 Id   RuleName
  1   use-indexes
  2   use-index-for-sort
  3   remove-unnecessary-calculations-2


arangod> 

YES!

Was this page helpful?
0 / 5 - 0 ratings