简体   繁体   English

ArangoDB中的多重排序

[英]Multiple sorting in ArangoDB

My webapp needs to display several sorted lists of document attributes in a graph. 我的Web应用程序需要在图形中显示文档属性的几个排序列表。 These are hours , cycles , and age . 这些是hourscyclesage

I have an AQL query that beautifully traverses the graph and gets me all the data my app needs in 2 ms. 我有一个AQL查询,可以很好地遍历图形,并在2毫秒内获取我的应用程序所需的所有数据。 I'm very impressed! 我印象深刻! But I need it sorted for each graph. 但是我需要对每个图形进行排序。 The query currently returns an array of json objects that contain all three of the attributes and the id for which they apply. 该查询当前返回一个json对象数组,其中包含所有三个属性以及它们适用的ID。 Awesome. 太棒了 The query also very easily sorts on one of the attributes. 该查询还可以很容易地对属性之一进行排序。

My problem is: I need to have a sorted list of all three, and would prefer not to query the database three times since the data is all in the same documents my traversal returned. 我的问题是:我需要对所有这三个列表进行排序,并且不希望对数据库进行三次查询,因为数据全部在我遍历返回的同一文档中。

I would like to return three sorted arrays of json objects: one containing hours and the id, one containing cycles and the id, and one containing age and the id. 我想返回json对象的三个排序数组:一个包含hours和id,一个包含cycles和id,一个包含age和id。 This way, my graphs can easily display all three graphs without client-side sorting. 这样,我的图就可以轻松显示所有三个图,而无需客户端排序。

HTTP requests themselves are time consuming although the database is very fast, which is why I'd like to pull all three at once, as the data itself is small. 尽管数据库非常快,但HTTP请求本身却很耗时,这就是为什么我想一次提取所有三个请求的原因,因为数据本身很小。

My current query is a simple graph traversal: 我当前的查询是一个简单的图遍历:

for v, e, p in outbound startNode graph 'myGraph'
    filters & definitions...
    sort v.hours desc
    return {"hours": v.hours, "cycles": v.cycles, "age": v.age, "id": v.id}

Is there an easy way I can tell Arango to return me this structure? 有什么简单的方法可以告诉Arango将此结构还给我?

{
 [
  {
   "id": 47,
   "hours": 123
  },
  {
   "id": 23,
   "hours": 105
  }...
 ],
 [
  {
   "id": 47,
   "cycles": 18
  },
  {
   "id": 23,
   "cycles": 5
  }...
 ],
 [
  {
   "id": 47,
   "age": 4.2
  },
  {
   "id": 23,
   "age": 0.9
  }
 ]
}

Although the traversal is fast, I would prefer if I didn't have to re-traverse the graph three times to do it, if possible. 尽管遍历速度很快,但如果可能的话,我宁愿不需要三遍遍遍图来进行遍历。

My solution: 我的解决方案:

let data = (for v, e, p in outbound startNode graph 'myGraph'
                filters & definitions...
                return {"hours": v.hours, "cycles": v.cycles, "age": v.age, "id": v.id})
let byHours = (for thing in data
                   sort thing.hours desc
                   return {"hours": thing.hours, "id": thing.id})
let byCycles = (for thing in data
                    sort thing.cycles desc
                    return {"cycles": thing.cycles, "id": thing.id})
let byAge = (for thing in data
                 sort thing.age desc
                 return {"age": thing.age, "id": thing.id})
return {"hours": byHours, "cycles": byCycles, "age": byAge}

I'm not sure how this compares against your solution performance-wise, but the most obvious solution would be to traverse once and then create three sorted results like this: 我不确定这与您的解决方案性能相比如何,但是最明显的解决方案是遍历一次,然后创建三个排序的结果,如下所示:

LET nodes = (
  FOR v, e, p IN OUTBOUND startNode GRAPH 'myGraph'
  FILTER ...
  RETURN v
)
RETURN {
  hours: (
    FOR n IN nodes
    SORT n.hours DESC
    RETURN KEEP(n, ['hours', 'id'])
  ),
  cycles: (
    FOR n IN nodes
    SORT n.cycles DESC
    RETURN KEEP(n, ['cycles', 'id'])
  ),
  age: (
    FOR n IN nodes
    SORT n.age DESC
    RETURN KEEP(n, ['age', 'id'])
  )
}

This would traverse the graph only once but sort the result three times. 这样只会遍历图形一次,但对结果进行三遍排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM