Skip to Content
Neo4j:权威指南
book

Neo4j:权威指南

by Luanne Misquitta, Christophe Willemsen
July 2025
Beginner to intermediate
410 pages
5h 15m
Chinese
O'Reilly Media, Inc.
Content preview from Neo4j:权威指南

第 8 章 高级图形模式 高级图形模式

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

本章探讨了作为Neo4j专家需要了解的高级图模式。这里的主题涉及安全建模技术、实体解析、更高效的查询、节点度处理以及最新的量化路径模式。在前面的章节中,你已经看到了一些子查询的用法,现在你将学到更多关于它们的知识。

要尝试查询,请继续使用chapter5 数据库,或者按照 GitHub 仓库中的README重新创建数据库。

子查询

Cypher中的子查询是在外部查询的嵌套范围内执行的嵌套查询。CALL 子查询执行来自外部查询的每一条记录。这一点很重要:由于子查询在自己的范围内运行,因此它在执行一条记录时不需要保留任何已创建的数据结构,然后再执行下一条传入的记录,从而减少了内存开销。从图中读取数据和向图中写入数据时都会使用子查询。

调用子查询

第 5 章中,您导入了一组大型数据,生成了一个包含 1 800 万个节点和 1 亿个关系的图形。该图非常适合用来检验使用子查询的效果。查询的第一个版本要求 Neo4j 返回图中的每首曲目以及它们所在的播放列表,它使用了带有路径扩展的正则MATCH

//001-explain-all-tracks.cypher
EXPLAIN
MATCH (t:Track)-[:ON_PLAYLIST]->(p:Playlist)
RETURN t as track, COLLECT(p) as playlists

这个图中大约有 1300 万条曲目,这个查询将遍历所有曲目,收集它们所在的播放列表。运行该查询的EXPLAIN (不要尝试PROFILE ,否则可能会等待很长时间才能真正执行查询)。计划如图 8-1 所示,显示了EagerAggregation 操作符(有关急迫操作的复习,请参阅第 5 章)。

图 8-1. 返回所有曲目及其播放列表的查询的EXPLAIN 计划

MATCH 必须在所有曲目和播放列表上完全执行,以便正确处理COLLECT 聚合。这些聚合结果会保留在内存中,直到操作符执行完毕,然后再开始结果流。这会对堆造成压力,导致潜在的Out of Memory 异常或更多的垃圾回收 (GC) 暂停。这种类型的查询是演示子查询功能的理想选择。由于查询结果(实际曲目和播放列表)并不重要,我们简化了查询,只返回一个计数:

//002-count.cypher
MATCH (t:Track)-[:ON_PLAYLIST]->(p:Playlist)
WITH t as track, COLLECT(p) as playlists
RETURN count(*)

在运行此查询之前,请检查一下neo4j.conf 中的内存设置。如果设置较宽松,可以减少内存,以便更容易看到效果。我们使用的设置是

dbms.memory.heap.max_size=1G
server.memory.heap.initial_size=512m 
dbms.memory.transaction.total.max=256m 
db.memory.transaction.max=16m 

运行查询并监控debug.log。你会开始看到停止的 GC 停顿,如 这些:

 WARN [o.n.k.i.c.VmPauseMonitorComponent] ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

基于scikit-learn和PyTorch的实践机器学习

基于scikit-learn和PyTorch的实践机器学习

Aurélien Géron
企业级Java开发中的应用人工智能 (Chinese Edition)

企业级Java开发中的应用人工智能 (Chinese Edition)

Alex Soto Bueno, Markus Eisele, Natale Vinto

Publisher Resources

ISBN: 9798341664852