Neo4j 这两个密码查询之间有什么区别?
我有点不知所措 在我的数据库中,我的关系如下:Neo4j 这两个密码查询之间有什么区别?,neo4j,cypher,neo4j-spatial,Neo4j,Cypher,Neo4j Spatial,我有点不知所措 在我的数据库中,我的关系如下: (u:User)-[r1:LISTENS_TO]->(a:Artist)<-[r2:LISTENS_TO]-(u2:User) 我很沮丧。看起来这并不需要20秒 后来我又回到这个问题上来,并尝试从一开始就调试它 我开始分解查询,我注意到我得到的结果要快得多。如果没有Neo4J空间查询,我只需1.5秒就可以得到结果 我最终添加了一些内容,并以以下查询结束: START u=node(553314), u2=node:userLocati
(u:User)-[r1:LISTENS_TO]->(a:Artist)<-[r2:LISTENS_TO]-(u2:User)
我很沮丧。看起来这并不需要20秒
后来我又回到这个问题上来,并尝试从一开始就调试它
我开始分解查询,我注意到我得到的结果要快得多。如果没有Neo4J空间查询,我只需1.5秒就可以得到结果
我最终添加了一些内容,并以以下查询结束:
START u=node(553314), u2=node:userLocations("withinDistance:[38.89037,-77.03196,80.467]")
OPTIONAL MATCH
pMutualArtists=(u:User)-[ar1:LISTENS_TO]->(a:Artist)<-[ar2:LISTENS_TO]-(u2:User)
WHERE
u2:User
WITH u2, COUNT(DISTINCT pMutualArtists) AS mutualArtists
ORDER BY mutualArtists DESC
LIMIT 10
RETURN u2.username, mutualArtists
为了证明我连续运行了两次,得到了非常不同的结果:
neo4j-sh (?)$ START u=node(553314), u2=node:userLocations("withinDistance:[38.89037,-77.03196,80.467]")
>
> OPTIONAL MATCH
> pMutualArtists=(u:User)-[ar1:LISTENS_TO]->(a:Artist)<-[ar2:LISTENS_TO]-(u2:User)
> WHERE
> u2:User
>
> WITH u2, COUNT(DISTINCT pMutualArtists) AS mutualArtists
> ORDER BY mutualArtists DESC
> LIMIT 10
> RETURN u2.username, mutualArtists
> ;
+------------------------------+
| u2.username | mutualArtists |
+------------------------------+
| "573904765" | 644 |
| "28600291" | 601 |
| "1092510304" | 558 |
| "1367963461" | 521 |
| "1508790199" | 455 |
| "1335360028" | 447 |
| "18200866" | 444 |
| "1229430376" | 435 |
| "748318333" | 434 |
| "5612902" | 431 |
+------------------------------+
10 rows
4240 ms
neo4j-sh (?)$ START me=node(553314), other=node:userLocations("withinDistance:[38.89037,-77.03196,80.467]")
>
> OPTIONAL MATCH
> pMutualArtists=(me:User)-[ar1:LISTENS_TO]->(a:Artist)<-[ar2:LISTENS_TO]-(other:User)
> WHERE
> other:User
>
> WITH other, COUNT(DISTINCT pMutualArtists) AS mutualArtists
> ORDER BY mutualArtists DESC
> LIMIT 10
> RETURN other.username, mutualArtists;
+--------------------------------+
| other.username | mutualArtists |
+--------------------------------+
| "573904765" | 644 |
| "28600291" | 601 |
| "1092510304" | 558 |
| "1367963461" | 521 |
| "1508790199" | 455 |
| "1335360028" | 447 |
| "18200866" | 444 |
| "1229430376" | 435 |
| "748318333" | 434 |
| "5612902" | 431 |
+--------------------------------+
10 rows
20418 ms
neo4j sh(?)
>
>选配
>pMutualArtists=(u:User)-[ar1:LISTENS\u TO]->(a:Artist)其中
>u2:用户
>
>对于u2,将(不同的pMutualArtists)计数为mutualArtists
>共同主义者的命令描述
>限制10
>返回u2.username,mutualArtists
> ;
+------------------------------+
|u2.username |共同主义者|
+------------------------------+
| "573904765" | 644 |
| "28600291" | 601 |
| "1092510304" | 558 |
| "1367963461" | 521 |
| "1508790199" | 455 |
| "1335360028" | 447 |
| "18200866" | 444 |
| "1229430376" | 435 |
| "748318333" | 434 |
| "5612902" | 431 |
+------------------------------+
10排
4240毫秒
neo4j sh(?)$START me=node(553314),other=node:userLocations(“withindance:[38.89037,-77.03196,80.467]”)
>
>选配
>pMutualArtists=(me:User)-[ar1:LISTENS\u TO]->(a:Artist)其中
>其他:用户
>
>对于其他,将(不同的pmutualarists)计数为mutualArtists
>共同主义者的命令描述
>限制10
>返回other.username,mutualArtists;
+--------------------------------+
|other.username |互惠主义者|
+--------------------------------+
| "573904765" | 644 |
| "28600291" | 601 |
| "1092510304" | 558 |
| "1367963461" | 521 |
| "1508790199" | 455 |
| "1335360028" | 447 |
| "18200866" | 444 |
| "1229430376" | 435 |
| "748318333" | 434 |
| "5612902" | 431 |
+--------------------------------+
10排
20418毫秒
除非我发疯了,否则这两个查询之间唯一的区别就是节点的名称(我将“me”改为“u”,将“other”改为“u2”)
为什么这会导致5倍的改善
如果有人对此有任何见解,我将永远感激
谢谢
-亚当
编辑8.1.14 根据@ulkas的建议,我尝试简化查询 结果是:
START u=node(553314), u2=node:userLocations("withinDistance:[38.89037,-77.03196,80.467]")
OPTIONAL MATCH pMutualArtists=(u:User)-[ar1:LISTENS_TO]->(a:Artist)<-[ar2:LISTENS_TO]-(u2:User)
RETURN u2.username, COUNT(DISTINCT pMutualArtists) as mutualArtists
ORDER BY mutualArtists DESC
LIMIT 10
START u=node(553314),u2=node:userLocations(“withindance:[38.89037,-77.03196,80.467]”)
可选匹配pMutualArtists=(u:User)-[ar1:LISTENS\u TO]->(a:Artist)(a:Artist)这听起来像是看到了缓存的效果。第一次访问时,缓存未填充。由于缓存中已存在节点/关系,因此命中同一图形的后续查询速度会快得多。在之后使用可选匹配
,其中other:User
没有意义,因为结束节点other
(u2
)必须匹配。尝试在不使用可选匹配
和where
的情况下执行查询,并且在不使用最后一个的情况下使用
,只需
START me=node(553314), other=node:userLocations("withinDistance[38.89037,-77.03196,80.467]")
MATCH
pMutualArtists=(me:User)-[ar1:LISTENS_TO]->(a:Artist)<-[ar2:LISTENS_TO]-(other:User)
RETURN other.username, count(DISTINCT pMutualArtists) as mutualArtists
ORDER BY mutualArtists DESC
LIMIT 10
START me=node(553314),other=node:userLocations(“withindance[38.89037,-77.03196,80.467]”)
匹配
pMutualArtists=(me:User)-[ar1:LISTENS\u TO]->(a:Artist)感谢您的回复。这正是我所想的。但是我可以在一行中多次运行长查询(与我一起运行的查询和其他查询)并获得相同的结果,而在一行中多次运行其他查询也会产生相同的结果。此外,这些结果会在服务器重新启动之间持续存在。感谢您的回复。我同意“WHERE”和“WITH”条款是没有必要的。这些都是我一直在尝试的东西。但是,对于我的特定用例,“可选匹配”是必要的,因为我需要返回没有共同艺术家的其他用户,而“匹配”不会返回。我将编辑我上面的帖子,并根据你的评论告诉你我尝试了什么;博士…同样的问题。我明白了,我也很困惑为什么会这样。看这个问题。
neo4j-sh (?)$ START u=node(553314), u2=node:userLocations("withinDistance:[38.89037,-77.03196,80.467]")
>
> OPTIONAL MATCH
> pMutualArtists=(u:User)-[ar1:LISTENS_TO]->(a:Artist)<-[ar2:LISTENS_TO]-(u2:User)
> WHERE
> u2:User
>
> WITH u2, COUNT(DISTINCT pMutualArtists) AS mutualArtists
> ORDER BY mutualArtists DESC
> LIMIT 10
> RETURN u2.username, mutualArtists
> ;
+------------------------------+
| u2.username | mutualArtists |
+------------------------------+
| "573904765" | 644 |
| "28600291" | 601 |
| "1092510304" | 558 |
| "1367963461" | 521 |
| "1508790199" | 455 |
| "1335360028" | 447 |
| "18200866" | 444 |
| "1229430376" | 435 |
| "748318333" | 434 |
| "5612902" | 431 |
+------------------------------+
10 rows
4240 ms
neo4j-sh (?)$ START me=node(553314), other=node:userLocations("withinDistance:[38.89037,-77.03196,80.467]")
>
> OPTIONAL MATCH
> pMutualArtists=(me:User)-[ar1:LISTENS_TO]->(a:Artist)<-[ar2:LISTENS_TO]-(other:User)
> WHERE
> other:User
>
> WITH other, COUNT(DISTINCT pMutualArtists) AS mutualArtists
> ORDER BY mutualArtists DESC
> LIMIT 10
> RETURN other.username, mutualArtists;
+--------------------------------+
| other.username | mutualArtists |
+--------------------------------+
| "573904765" | 644 |
| "28600291" | 601 |
| "1092510304" | 558 |
| "1367963461" | 521 |
| "1508790199" | 455 |
| "1335360028" | 447 |
| "18200866" | 444 |
| "1229430376" | 435 |
| "748318333" | 434 |
| "5612902" | 431 |
+--------------------------------+
10 rows
20418 ms
START u=node(553314), u2=node:userLocations("withinDistance:[38.89037,-77.03196,80.467]")
OPTIONAL MATCH pMutualArtists=(u:User)-[ar1:LISTENS_TO]->(a:Artist)<-[ar2:LISTENS_TO]-(u2:User)
RETURN u2.username, COUNT(DISTINCT pMutualArtists) as mutualArtists
ORDER BY mutualArtists DESC
LIMIT 10
START me=node(553314), other=node:userLocations("withinDistance:[38.89037,-77.03196,80.467]")
OPTIONAL MATCH pMutualArtists=(me:User)-[ar1:LISTENS_TO]->(a:Artist)<-[ar2:LISTENS_TO]-(other:User)
RETURN other.username, COUNT(DISTINCT pMutualArtists) as mutualArtists
ORDER BY mutualArtists DESC
LIMIT 10
START me=node(553314), other=node:userLocations("withinDistance[38.89037,-77.03196,80.467]")
MATCH
pMutualArtists=(me:User)-[ar1:LISTENS_TO]->(a:Artist)<-[ar2:LISTENS_TO]-(other:User)
RETURN other.username, count(DISTINCT pMutualArtists) as mutualArtists
ORDER BY mutualArtists DESC
LIMIT 10