0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GraphDBを学ぶ(6) CypherQLでneo4jの内部結合

Posted at

TL;DR

  • Cypher QLでノード間結合を行うクエリを学びます
  • 前回のneo4j環境を使用して,簡単な内部結合を行うCypherQLクエリを検証します

CypherQLでの結合処理

  • TPC-Hでは,基本的にテーブル間の内部結合(inner join)が行われます
    • ベンチマーククエリでは結合処理を明示せず,各レコードの外部キーと一致する主キーを持つ別テーブルのレコードを検索する,という書き方になっています

最も基本的な結合処理(SQL)

  • 以下のようなSQL文で検討します
select N_NAME, R_NAME
  from nation, region
  where nation.N_REGIONKEY = region.R_REGIONKEY;
  • 「nationテーブルの全レコードについて,外部キー(N_REGIONKEY)に基づき,対応するregionテーブルのレコードを結合し,N_NAMEとR_NAMEを表示する」というクエリです
    • 実行結果は,以下のようになります
    • 「国名と,各国が存在する地域の名前を,すべて出力する」
MariaDB [sf001]>
select N_NAME, R_NAME
  from nation, region
  where N_REGIONKEY = R_REGIONKEY;

+----------------+-------------+
| N_NAME         | R_NAME      |
+----------------+-------------+
| ALGERIA        | AFRICA      |
.....<snip>.....
| SAUDI ARABIA   | MIDDLE EAST |
+----------------+-------------+
25 rows in set (0.00 sec)

結合処理(CypherQL)

  • 前節のSQLクエリを,Cypherで書き直すと,以下のようになります
    • 「nationラベルのノードをすべて取り出し,関係するregionノードとともに,名前(N_NAMEとR_NAME)を出力する」
neo4j>
match(n:nation)-[:AT]->(r:region)
return n.N_NAME, r.R_NAME;

┌──────────────┬───────────────────┐
 n.N_NAME      r.R_NAME          
╞══════════════╪═══════════════════╡
 MOZAMBIQUE    AFRICA            
 MOROCCO       AFRICA            
.....<snip>.....
 IRAN          MIDDLE EAST       
 EGYPT         MIDDLE EAST       
└──────────────┴───────────────────┘
25 rows returned in 26ms (first row after 24ms, rendered after 28ms)

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?