neo4j 如何使用模式理解来减少与收集相比的数据库命中

xoshrz7s  于 2023-03-02  发布在  其他
关注(0)|答案(1)|浏览(148)

我一直在尝试使用模式解析进行优化,但越来越困惑。
这是我最初的疑问:

MATCH (a:Actor)-[:ACTED_IN]->(m:Movie)
WHERE 2000 <= m.year <= 2005 AND a.born.year >= 1980
RETURN a.name AS Actor, a.born AS Born,
collect(DISTINCT m.title) AS Movies ORDER BY Actor

分析时,我得到:

Cypher version: , planner: COST, runtime: PIPELINED. 41944 total db hits in 152 ms.

我试着改写成:

profile MATCH (a:Actor)
WHERE a.born.year >= 1980
// Add a WITH clause to create the list using pattern comprehension
with a
match (a)-[:ACTED_IN]-(m:Movie)
where 2000 <= m.year <= 2005
// filter the result of the pattern comprehension to return only lists with elements
// return the Actor, Born, and Movies
return a.name as Actor, a.born as Born, [(a)-[:ACTED_IN]-(m) | m.title]  as Movies
order by a

分析时,我得到:

Cypher version: , planner: COST, runtime: PIPELINED. 47879 total db hits in 47 ms.

我又试了一次改写:

profile MATCH (a:Actor)
WHERE a.born.year >= 1980
// Add a WITH clause to create the list using pattern comprehension
// filter the result of the pattern comprehension to return only lists with elements
// return the Actor, Born, and Movies
with a, [ (a)-[:ACTED_IN]-(m:Movie)  where 2000 <= m.year <= 2005 | m.title] as Movies
return a.name as Actor, a.born as Born, Movies
order by a

并且当配置文件获得:

Cypher version: , planner: COST, runtime: PIPELINED. 59251 total db hits in 6 ms.

每一次的性能都比前一次差。我可以查看查询计划来了解差异。与使用collect语句的初始查询相比,是否有一种方法可以使用模式理解来实际减少DB命中?

balp4ylt

balp4ylt1#

请向我们显示您上次查询的配置文件结果;我在电影数据库中测试了它,它与原始查询相比运行良好(46 ms与原始:120 db点击)。另外,检查Actor.born.year是否有索引。

profile MATCH (a:Person)-[:ACTED_IN]->(m:Movie)
WHERE 2000 <= m.released <= 2005 AND a.born >= 1980
RETURN a.name AS Actor, a.born AS Born,
collect(DISTINCT m.title) AS Movies ORDER BY Actor

planner: COST, runtime: PIPELINED. 120 total db hits in 9 ms

profile MATCH (a:Person)
WHERE  a.born >= 1980
RETURN a.name AS Actor, a.born AS Born,
[(a)-[:ACTED_IN]-(m:Movie) where 2000 <= m.released <= 2005 | m.title] AS Movies ORDER BY Actor

planner: COST, runtime: PIPELINED. 43 total db hits in 6 ms

相关问题