简体   繁体   English

Neo4j 查询性能排序不佳

[英]Neo4j poor order by query performance

I have a complex cypher, When I don't use "order by" I get a pretty fast response but when I use "order by" it is incredibly slow.我有一个复杂的 cypher,当我不使用“order by”时,我得到了非常快的响应,但是当我使用“order by”时,速度非常慢。 I have an b tree index on my order attribute(score of the movie which is PageRank algorithm score).我的订单属性上有一个 b 树索引(电影的得分是 PageRank 算法得分)。 I added the cypher.我添加了 cypher。

MATCH (m:Movie)
WHERE m.release > '0' AND m.imdbVoteAverage > 0 AND
CASE WHEN NOT [] = [] THEN any(title in [] WHERE toLower(m.title) CONTAINS title OR toLower(m.originalTitle) CONTAINS title) ELSE TRUE END
WITH m AS m
MATCH (m)-[:HAS_GENRE]->(genre: Genre)
WHERE CASE WHEN NOT ['komedi'] = [] THEN any(genreName in ['komedi'] WHERE toLower(genre.name) CONTAINS genreName) ELSE TRUE END
MATCH (m)<-[acted:ACTED_IN]-(actor: Person)
WHERE CASE WHEN NOT [] = [] THEN any(actorName in [] WHERE toLower(actor.name) CONTAINS actorName) ELSE TRUE END AND
CASE WHEN NOT [] = [] THEN any(characterName in [] WHERE any(cname in acted.characterNames WHERE toLower(cname) CONTAINS characterName)) ELSE TRUE END
MATCH (m) -[:HAS_KEYWORDS]->(keyword: Keyword)
WHERE CASE WHEN NOT [] = [] THEN any(keywordName in [] WHERE toLower(keyword.name) CONTAINS keywordName) ELSE TRUE END
MATCH (m)<-[:PRODUCED]-(producer: Person)
WHERE CASE WHEN NOT [] = [] THEN any(producerName in [] WHERE toLower(producer.name) CONTAINS producerName) ELSE TRUE END
MATCH (m)<-[:DIRECTED]-(director: Person)
WHERE CASE WHEN NOT [] = [] THEN any(directorName in [] WHERE toLower(director.name) CONTAINS directorName) ELSE TRUE END
MATCH (m)<-[:WRITTEN]-(writer: Person)
WHERE CASE WHEN NOT [] = [] THEN any(writerName in [] WHERE toLower(writer.name) CONTAINS writerName) ELSE TRUE END
MATCH (m)<-[:PRODUCED_COMPANY]-(productionCompany: ProductionCompany)
WHERE CASE WHEN NOT [] = [] THEN any(producedCompanyName in [] WHERE toLower(productionCompany.name) CONTAINS producedCompanyName) ELSE TRUE END
RETURN DISTINCT m ORDER BY m.score DESC LIMIT 10 

Also If I add more field such as genre, title, directorName the query works much faster.此外,如果我添加更多字段,例如流派、标题、导演名称,则查询的工作速度会更快。

You need to indicate to the planner that your m.score field is numeric, so pulls that from the index.您需要向计划者表明您的 m.score 字段是数字的,因此请从索引中提取它。 Ie where m.score > 0where m.score > 0

You should see it in your query plans.您应该在查询计划中看到它。

Your query looks also really convoluted, and generated.您的查询看起来也很复杂,并且生成了。 But actually not taking into account that always "false" expressions can just be left out from the query parts eg WHERE NOT [] = []但实际上没有考虑到总是“错误”的表达式可以从查询部分中省略,例如WHERE NOT [] = []

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

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