简体   繁体   English

Neo4j Cypher 1.9.5查询速度超过2个索引

[英]Neo4j Cypher 1.9.5 Query Slow with more than 2 indexes

I have a Cypher 1.9.5 query in Neo4j which when performed with three indexes just hangs. 我在Neo4j中有一个Cypher 1.9.5查询,当用三个索引执行时,它就会挂起。 If I alter the query to use two indexes and a where clause then it works (still slow!) 如果我改变查询以使用两个索引和where子句然后它工作(仍然很慢!)

In this altered sample I am looking for toys whose names start with 'tc_', in smallboxes whose names start with '2', which in turn are in bigboxes whose names start with 'p' 在这个改变的样本中,我正在寻找名称以'tc_'开头的玩具,在名称以'2'开头的小盒子中,而这些小盒子的名字以'p'开头

With 3 indexes this hangs 有3个索引就会挂起

START b=node:BigBox('name:p*'), s=node:SmallBox('name:2*'), ts=node:Toys('name:tc_*')
MATCH b-[:SMALLBOX]->s, s-[:TOYS]->ts
RETURN count(ts)

But these work 但这些工作

START s=node:SmallBox('name:2*'), ts=node:Toys('name:tc_*')
MATCH b-[:SMALLBOX]->s, s-[:TOYS]->ts
WHERE b.name =~ '(?i)p.*'
RETURN count(ts)

START b=node:BigBox('name:p*'), ts=node:Toys('name:tc_*')
MATCH b-[:SMALLBOX]->s, s-[:TOYS]->ts
WHERE s.name =~ '(?i)2.*'
RETURN count(ts)

The last two give the answer that the first would have. 最后两个给出了第一个答案。

What do I need to do to allow more than two indexes in the START clause? START子句中允许两个以上的索引需要做什么? Note that I more than 200,000 toys in 90-100 small boxes, which in turn are in 5 big boxes. 请注意,我在90-100个小盒子中有超过20万个玩具,而这些小盒子又分为5个大盒子。

You are committing a cardinal sin called cartesian product. 你犯的是一种叫做笛卡尔积的主要罪。

What's happening is you're taking all three sets of results of your index lookups: b , s , and ts , and for each b, it's finding all of the s's, and for each set of b+s, it's finding all of the b+s+ts. 发生的事情是你正在获取索引查找的所有三组结果: bsts ,并且对于每个b,它找到所有的s,并且对于每组b + s,它找到所有的b + S + TS。 Then, for each of the combinations, it's finding a match. 然后,对于每个组合,它找到匹配。

The better way to solve this problem is to pick the smallest set in your start clause-- b , then use traversals to find the potential s s and ts s that match. 更好的办法来解决这个问题是要选择你开始clause--最小集b ,然后使用遍历查找潜在的s S和ts匹配秒。 So: 所以:

START b=node:BigBox('name:p*')
MATCH b-[:SMALLBOX]->s
WHERE s.name =~ "2.*" 
WITH b, s
MATCH s-[:TOYS]->ts
WHERE ts.name =~ "tc_.*"
RETURN count(ts)

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

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