Skip to Content
Neo4j: The Definitive Guide
book

Neo4j: The Definitive Guide

by Luanne Misquitta, Christophe Willemsen
July 2025
Intermediate to advanced
410 pages
6h 21m
Japanese
O'Reilly Media, Inc.
Content preview from Neo4j: The Definitive Guide

第8章 高度なグラフ・パターン 高度なグラフパターン

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

この章では、Neo4jのエキスパートとして知っておくと便利な、高度なグラフパターンを探求する。ここでのトピックは、エンティティの解決、より効率的なクエリ、ノードの次数の扱い、最近の定量化パスパターンと同様に、セキュリティのためのモデリングテクニックに触れる。前の章では、サブクエリの使い方をいくつか見てきた。

クエリを試すには、chapter5 データベースを使い続けるか、GitHub リポジトリのREADMEに従って再作成する。

サブクエリー

Cypherのサブクエリは、外部クエリのネストされたスコープ内で実行されるネストされたクエリである。CALL サブクエリは、アウタークエリから届いた行ごとに実行される。これは重要な点である。副問い合わせはそれ自身のスコープで演算子として実行されるため、次の行に移る前に、その行の実行中に作成されたデータ構造を保持する必要がなく、メモリのオーバーヘッドを削減することができる。サブクエリは、グラフからの読み取りとグラフへの書き込みの両方で使用される。

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

このグラフには約1,300万トラックがあり、このクエリはそれらをすべてトラバースして、それらが登録されているプレイリストを集めようとしている。このクエリのEXPLAIN を実行する(PROFILE を実行しようとすると、クエリが実際に実行されるまでに非常に長い時間待たされる可能性がある)。図8-1に示すように、計画はEagerAggregation 演算子を示している(イーガー演算については第5章を参照のこと)。

図8-1. 全てのトラックとそのプレイリストを返すクエリのEXPLAIN プラン

COLLECT 集計が正しくプロセスされるように、MATCH はすべてのトラックとプレイリストに対して完全に実行されなければならない。これらの集計結果は、演算子が完了するまでメモリに保持され、その後、結果のストリーミングが進行する。これはヒープにストレスを与え、潜在的なOut of Memory 例外やガベージコレクション(GC)休止の増加につながる可能性がある。このタイプのクエリは、サブクエリの威力を実証するのに理想的である。クエリの結果(実際のトラックとプレイリスト)には興味がないので、カウントだけを返すようにクエリを単純化した:

 //002-count.cypher MATCH ...
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

Elasticsearch in Action, Second Edition

Elasticsearch in Action, Second Edition

Madhusudhan Konda
Fluent Python ―Pythonicな思考とコーディング手法

Fluent Python ―Pythonicな思考とコーディング手法

Luciano Ramalho, 豊沢 聡, 桑井 博之, 梶原 玲子

Publisher Resources

ISBN: 9798341664760