Chinaunix首页 | 论坛 | 博客
  • 博客访问: 568767
  • 博文数量: 86
  • 博客积分: 2581
  • 博客等级: 少校
  • 技术积分: 793
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-05 20:09
文章分类

全部博文(86)

文章存档

2009年(86)

我的朋友

分类: Mysql/postgreSQL

2009-10-18 15:33:58

GRAPH engine 是mysql 5.1的是存储引擎插件,主要用于数据的分层显示。在论坛中时常看到很多网友,有这样的需求:“列出某条数据的所有子数据”,“计算某条数据是另一条数据的第几层子节点”等等。这种情况下的树形数据结构就可以用GRAPH engine作为解决方案。
网址:

简介:
The GRAPH engine is a computational engine allowing hierarchies and more complex graph structures to be handled in a relational fashion. In a nutshell, tree structures and friend-of-a-friend style searches can now be done using standard SQL syntax, and results joined onto other tables. The GRAPH engine is based on an original idea by Open Query founder , and has been developed in-house with Antony Curtis. For a simple example, see this blog entry.

For the earlier proof of concept we had valuable support from MySQL and MariaDB core developers such as Sergei Golubchik (Sun/MySQL), Timour Katchaounov (Sun/MySQL), Igor Babaev (Monty Program).

应用的小例子:

-- insert a few entries with connections (and multiple paths)
insert into foo (origid, destid) values (1,2), (2,3), (2,4), (4,5), (3,6), (5,6);
-- a regular table to join on to
insert into people values (1,"pearce"),(2,"hunnicut"),(3,"potter"),
(4,"hoolihan"),(5,"winchester"),(6,"mulcahy");
-- find us the shortest path from pearce (1) to mulcahy (6) please
select group_concat(people.name order by seq) as path
from foo join people on (foo.linkid=people.id)
where latch=1 and origid=1 and destid=6;
+--------+--------+--------------------------------+
| origid | destid | path                           |
+--------+--------+--------------------------------+
|      1 |      6 | pearce,hunnicut,potter,mulcahy |
+--------+--------+--------------------------------+
-- find us all people we can get to from potter (3)
select origid,group_concat(people.name order by seq) as destinations
from foo join people on (foo.linkid=people.id)
where latch=1 and origid=3;
+--------+----------------+
| origid | destinations |
+--------+----------------+
| 3 | mulcahy,potter |
+--------+----------------+

-- find us all people from where we can get to hoolihan (4)
select origid,group_concat(people.name order by seq) as origins
from foo join people on (foo.linkid=people.id)
where latch=1 and destid=4;
+--------+--------------------------+
| origid | origins |
+--------+--------------------------+
| 4 | hoolihan,hunnicut,pearce |
+--------+--------------------------+

So, there you have it. A graph (in this case a simple unidirectional tree, aka hierarchy) that looks like a table to us, as do the resultsets that have been computed.

阅读(2205) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~