Mais conteúdo relacionado
Semelhante a Pgxユーザー勉強会#5 パスクエリを使ったトラバース (20)
Pgxユーザー勉強会#5 パスクエリを使ったトラバース
- 1. ULS
Copyright © 2011-2017 UL Systems, Inc. All rights reserved.
Proprietary & Confidential Powered by
PGX ユーザー勉強会 #5
パスクエリを使ったトラバース
2017/11/02
- 2. ULS
Copyright © 2011-2017 UL Systems, Inc. All rights reserved.
Proprietary & Confidential Powered by 1
自己紹介
– 名前: 田上 悠樹
– 職業: ITエンジニア
– 所属: ウルシステムズ株式会社
– 直近の活動: ビジネスデータのグラフモデル化
(販売、生産、商品データなど)
- 3. ULS
Copyright © 2011-2017 UL Systems, Inc. All rights reserved.
Proprietary & Confidential Powered by 2
本日の題材に対するモチベーション
※ パスクエリの詳しいPGQL仕様はこちら↓
https://docs.oracle.com/cd/E56133_01/latest/reference/pgql-specification.html
従来の基幹用データベース
RDB(Oracle Database, MySQL) 主体。
異なるレコード同士を『JOIN』を利用して柔軟に紐づけて、価値
のある一まとまりのデータセットを取得する。
グラフデータベースで『JOIN』どう表現するか?
ノード間のエッジをトラバースすることで、価値のある一まとまり
のデータセットを取得する。
柔軟にトラバースする方法の1つとして、パスクエリを本日紹介。
- 4. ULS
Copyright © 2011-2017 UL Systems, Inc. All rights reserved.
Proprietary & Confidential Powered by 3
Example#1 起点 から到達可能なノード
– パスを使って到達可能なノードをトラバース
クエリ: あるノード r から到達可能な ノード d の集合
PATH example_path := () –[:eg_label]-> ()
SELECT d
WHERE (r with xxx) -/:example_path */-> (d)
クエリ: あるノード r から到達可能な ノード同士の隣接集合
PATH example_path := () -[:eg_label]-> ()
SELECT s, d
WHERE
(r with xxx) -/:example_path */-> (s) –[:eg_label]-> (d)
一覧として結果取得
したい場合
グラフとして結果取得
したい場合
- 5. ULS
Copyright © 2011-2017 UL Systems, Inc. All rights reserved.
Proprietary & Confidential Powered by 4
Example#1 Demo
クエリ: 渋谷駅 から到達可能な 東急線の駅
PATH rail_path := () -[:東急]- ()
SELECT s, d
WHERE (r with name = ‘渋谷’) -/:rail_path */-> (s) -[:東急]- (d)
無向グラフのケース
vis.js 利用
http://visjs.org/
- 6. ULS
Copyright © 2011-2017 UL Systems, Inc. All rights reserved.
Proprietary & Confidential Powered by 5
Example#2 起点+経由点 から到達可能なノード
– 複数箇所にパスを記述してノードをトラバース
クエリ: あるノード r から あるノード v を経由して到達可能なノード同士の隣接集合
PATH example_path := () -[:eg_label]-> ()
SELECT s, d
WHERE (r with xxx) -/:example_path */-> (v with xxx)
-/:example_path */-> (s) –[:eg_label]-> (d)
- 7. ULS
Copyright © 2011-2017 UL Systems, Inc. All rights reserved.
Proprietary & Confidential Powered by 6
Example#2 Demo
クエリ: 渋谷駅 から田園調布駅 を経由して到達可能な東急線の駅
PATH rail_path := () -[:東急]-> ()
SELECT s, d
WHERE (r with name =‘渋谷’) -/:rail_path */-> (v with name =‘田園調布’)
-/:rail_path */-> (s) –[:東急]-> (d)
パスクエリを
複数回適用
グラフの有向性の関係で東急目黒線を表示できなかったのが、残念。。
- 8. ULS
Copyright © 2011-2017 UL Systems, Inc. All rights reserved.
Proprietary & Confidential Powered by 7
Example#3 複数の異なるパスで到達可能なノード
– 異なる複数種類のパスを定義して、ノードをトラバース
クエリ: あるノード r から 複数の異なるパスで共に到達可能なノード cの集合
PATH example_path1 := () –[:eg_label1]-> ()
PATH example_path2 := () –[:eg_label2]-> ()
SELECT c
WHERE
(r with xxx) -/:example_path1 */-> (c),
(r) -/:example_path2 */-> (c)
- 9. ULS
Copyright © 2011-2017 UL Systems, Inc. All rights reserved.
Proprietary & Confidential Powered by 8
Example#3 Demo
クエリ: 渋谷駅 から 東急線Only と 東京メトロOnly で共に到達可能な駅
PATH rail_path1 := () –[:東急]-> ()
PATH rail_path2 := () –[:東京メトロ]-> ()
SELECT c
WHERE
(r with name =‘渋谷’) -/:example_path1 */-> (c),
(r) -/:example_path2 */-> (c)
日常で有用な意味は
特にありません 笑
緑点: 上記の c
青線: 東急
橙線: 東京メトロ
- 10. ULS
Copyright © 2011-2017 UL Systems, Inc. All rights reserved.
Proprietary & Confidential Powered by 9
まとめ
– パスクエリに対する考察
RDBにおける『 JOIN 』に対して、グラフはノード間のエッジをトラ
バースして価値のある一まとまりのデータセットを取得できる。
単一のPath句で表現できる事には限界があるが、複数種類のパスを複数
箇所で組み合わせることで、トラバースの表現力が高まる。
PGQL1.1 から更にパスクエリの表現が豊かに!!