Skip to content

Instantly share code, notes, and snippets.

@choplin
Created June 7, 2011 18:19
Show Gist options
  • Save choplin/1012809 to your computer and use it in GitHub Desktop.
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
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
;
@anodynos
Copy link

anodynos commented Jul 9, 2011

Hey,

Have you tried the tests with this improved schema ? is there any notable difference in the benchmarks ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment