Neo4j 按查询性能排序不佳
Neo4j poor order by query performance
我有一个复杂的密码,当我不使用“order by”时,我得到了相当快的响应,但是当我使用“order by”时,它非常慢。我的订单属性上有一个 b 树索引(电影的得分是 PageRank 算法得分)。我添加了密码。
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
此外,如果我添加更多字段,例如流派、标题、导演名称,查询速度会更快。
您需要向规划人员表明您的 m.score 字段是数字,因此请从索引中提取它。 IE。 where m.score > 0
您应该在查询计划中看到它。
您的查询看起来也很复杂,而且是生成的。但实际上并没有考虑到总是“假”的表达式只能从查询部分中删除,例如WHERE NOT [] = []
我有一个复杂的密码,当我不使用“order by”时,我得到了相当快的响应,但是当我使用“order by”时,它非常慢。我的订单属性上有一个 b 树索引(电影的得分是 PageRank 算法得分)。我添加了密码。
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
此外,如果我添加更多字段,例如流派、标题、导演名称,查询速度会更快。
您需要向规划人员表明您的 m.score 字段是数字,因此请从索引中提取它。 IE。 where m.score > 0
您应该在查询计划中看到它。
您的查询看起来也很复杂,而且是生成的。但实际上并没有考虑到总是“假”的表达式只能从查询部分中删除,例如WHERE NOT [] = []