简体   繁体   English

使用DESC与ASC时Neo4j Cypher的执行时间太慢

[英]Neo4j Cypher execution time when using DESC vs ASC too slow

In my current nodejs application, I'm using Neo4j as the database and use its REST API. 在当前的nodejs应用程序中,我使用Neo4j作为数据库并使用其REST API。 By running my unit tests, I discovered that I have two very slow running tests and tracked it down to the cypher query being the culprit for the bad performance itself. 通过运行单元测试,我发现我有两个运行速度非常慢的测试,并将其跟踪到密码查询是造成不良性能本身的元凶。 Now while the performance is still quite slow, at least the first case runs in a somewhat acceptable time for the use case on my local dev machine in about 300ms. 现在,尽管性能仍然很慢,但至少第一种情况在我的本地开发机上的用例运行的时间大约是300毫秒。 However, the second query adds a whole second until I get the result (just two rows in my case) back. 但是,第二个查询会加上一整秒,直到我得到结果(在我的情况下只有两行)。 The only difference is that I'm using DESC instead of ASC ordering of the brand name. 唯一的区别是,我使用的是DESC而不是品牌名称的ASC排序。

Query 1 查询1

MATCH (Distributor {slug: "some-dist"})-[:SELLS]->(product:Product)-[:IS|:BELONGS_TO*0..5]->(Category {slug: "electrical"}),
(product)-[:HAS_BRAND]->(brand:Brand)
RETURN DISTINCT brand ORDER BY brand.name ASC

Query 2 查询2

MATCH (Distributor {slug: "some-dist"})-[:SELLS]->(product:Product)-[:IS|:BELONGS_TO*0..5]->(Category {slug: "electrical"}),
(product)-[:HAS_BRAND]->(brand:Brand)
RETURN DISTINCT brand ORDER BY brand.name DESC

Some data about the graph: 有关图的一些数据:

  • 4 Distributor nodes 4个分发服务器节点
  • ~600 Brand nodes 约600个品牌节点
  • ~1500 Category nodes 约1500个类别节点
  • ~30000 Product nodes 〜30000个产品节点

All slug properties have a constraint to be unique. 所有段塞属性都有唯一的约束。 The name property of the Brand name is indexed, too. 品牌名称的名称属性也已建立索引。

Why is it, that the second query is so much slower and how do I fix it? 为什么会这样,第二个查询却慢得多,我该如何解决?

I made the mistake and did not use the colon on the first node to specify its label. 我弄错了,没有在第一个节点上使用冒号来指定其标签。 I actually created the internal rule that every node should have exactly one Label. 我实际上创建了一个内部规则,即每个节点都应该有一个标签。

Matching by label is fast and the sorting of the result does not really have an impact, wether it's ascending or descending order. 按标签匹配是快速的,并且结果的排序实际上是没有影响的,无论是升序还是降序。 It still does not explain the noticed behavior, but let this be a reminder to use labels in your cypher queries. 它仍然不能解释所注意到的行为,但是要提醒您在密码查询中使用标签。

MATCH (:Distributor {slug: "some-dist"})-[:SELLS]->(product:Product)-[:IS|:BELONGS_TO*0..5]->(Category {slug: "electrical"}),
(product)-[:HAS_BRAND]->(brand:Brand)
RETURN DISTINCT brand ORDER BY brand.name DESC

does the trick for me. 为我做the俩。

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

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