我有这两个查询,根据我的理解,这两个查询应该基本相同。一个正在对我的边缘集合进行过滤,并且效果非常好,而另一个查询由于未利用正确的索引而正在深度1的图形遍历,并且效果很差。
我有一个accounts
集合和一个transfers
集合,并且在transfers._to
和transfers.quantity
上有一个组合索引。
这是过滤器查询:
FOR transfer IN transfers
FILTER transfer._to == "accounts/testaccount" && transfer.quantity > 100
RETURN transfer
哪个正确使用了组合索引:
Execution plan:
Id NodeType Est. Comment
1 SingletonNode 1 * ROOT
6 IndexNode 18930267 - FOR transfer IN transfers /* skiplist index scan */
5 ReturnNode 18930267 - RETURN transfer
Indexes used:
By Type Collection Unique Sparse Selectivity Fields Ranges
6 skiplist transfers false false 10.11 % [ `_to`, `quantity` ] ((transfer.`_to` == "accounts/testaccount") && (transfer.`quantity` > 100))
Optimization rules applied:
Id RuleName
1 use-indexes
2 remove-filter-covered-by-index
3 remove-unnecessary-calculations-2
另一方面,这是我的图形遍历查询:
FOR account IN accounts
FILTER account._id == "accounts/testaccount"
FOR v, e IN 1..1 INBOUND account transfers
FILTER e.quantity > 100
RETURN e
仅使用组合索引中的_to
来过滤入站边缘,但无法使用quantity
:
Execution plan:
Id NodeType Est. Comment
1 SingletonNode 1 * ROOT
9 IndexNode 1 - FOR account IN accounts /* primary index scan */
5 TraversalNode 9 - FOR v /* vertex */, e /* edge */ IN 1..1 /* min..maxPathDepth */ INBOUND account /* startnode */ transfers
6 CalculationNode 9 - LET #7 = (e.`quantity` > 100) /* simple expression */
7 FilterNode 9 - FILTER #7
8 ReturnNode 9 - RETURN e
Indexes used:
By Type Collection Unique Sparse Selectivity Fields Ranges
9 primary accounts true false 100.00 % [ `_key` ] (account.`_id` == "accounts/testaccount")
5 skiplist transfers false false n/a [ `_to`, `quantity` ] base INBOUND
Traversals on graphs:
Id Depth Vertex collections Edge collections Options Filter conditions
5 1..1 transfers uniqueVertices: none, uniqueEdges: path
Optimization rules applied:
Id RuleName
1 use-indexes
2 remove-filter-covered-by-index
3 remove-unnecessary-calculations-2
但是,当我想使用图遍历时,有没有一种方法可以正确利用此组合索引?
编辑:我正在使用ArangoDB 3.4.2
答案 0 :(得分:1)
顶点中心索引(在边缘上创建并包括'_from'或'_to'属性的索引)通常在遍历时使用遍历,而不是在边缘本身上进行遍历。 (假设优化器当然找不到更好的计划)
因此,在您的查询中,尝试执行以下操作:
FOR account IN accounts
FILTER account._id == "accounts/testaccount"
FOR v, e IN 1..1 INBOUND account transfers
FILTER p.edges[*].quantity ALL > 100
RETURN e
您可以找到有关此索引类型here的文档