Created
June 7, 2011 18:19
-
-
Save choplin/1012809 to your computer and use it in GitHub Desktop.
Better sql for "Neo4j v.s. MySQL" in http://www.slideshare.net/slidarko/graph-windycitydb2010
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DROP TABLE IF EXISTS t; | |
CREATE TABLE t ( | |
id int PRIMARY KEY | |
,adj int[] | |
); | |
CREATE INDEX _idx_adj on t USING gin (adj); | |
INSERT INTO t VALUES | |
(1, ARRAY[2,3,4,5]) | |
,(2, ARRAY[1]) | |
,(3, ARRAY[1,6]) | |
,(4, ARRAY[1]) | |
,(5, ARRAY[1,7]) | |
,(6, ARRAY[3]) | |
,(7, ARRAY[5]); | |
WITH RECURSIVE r (id, adj, step) AS ( | |
SELECT | |
* | |
,1 | |
FROM | |
t | |
WHERE | |
id = 1 | |
UNION ALL | |
SELECT | |
t.* | |
,r.step + 1 | |
FROM | |
t,r | |
WHERE | |
t.id = ANY(r.adj) AND step < 5 | |
) | |
SELECT | |
* | |
FROM | |
r | |
WHERE | |
step = 5 | |
ORDER BY | |
id | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey,
Have you tried the tests with this improved schema ? is there any notable difference in the benchmarks ?