Neo4j graph database เร็วแค่ไหน แปลจากบางส่วนของหนังสือ Neo4j in action

enter image description here

Neo4่j เร็วแค่ไหน

รายละเอียดของตัวอย่าง join ข้อมูลหลายชั้น จากหนังสือ [Neo4j in action][2] ท่านใดสนใจเรียนรู้เพิ่มเติม ไปอุดหนุนหนังสือของผู้เขียนได้เลยนะครับ

เป็นตัวอย่างเป็นระบบ social network ที่ต้องการ query เพื่อนของเพื่อนของเพื่อน เป็นต้น

ตัวอย่าง crate table และขอละ index statement for simplicity

create table t_user (
id bigint not null,
name varchar(255) not null,
primary key (id)
);
create table t_user_friend (
id bigint not null,
user_1 bigint not null,
user_2 bigint not null,
primary key (id)
);

ตัวอย่าง query join เพื่อหาเพื่อนของเพื่อใน case ที่ 1 ถึง 5 ใน MySQL database

-- 1) All friends of a user

select count(distinct uf.*) from t_user_friend uf where uf.user_1 = ?

-- 2) All friends of friends of a user

select count(distinct uf2.*) from t_user_friend uf1
inner join t_user_friend uf2 on uf1.user_1 = uf2.user_2
where uf1.user_1 = ?

-- 3) All friend of friends of friends of a user

select count(distinct uf3.*) from t_user_friend uf1
inner join t_user_friend uf2 on uf1.user_1 = uf2.user_2
inner join t_user_friend uf3 on uf2.user_1 = uf3.user_2
where uf1.user_1 = ?

-- 4) All friends of friend of friends of friends of a user

select count(distinct uf4.*) from t_user_friend uf1
inner join t_user_friend uf2 on uf1.user_1 = uf2.user_2
inner join t_user_friend uf3 on uf2.user_1 = uf3.user_2
inner join t_user_friend uf4 on uf3.user_1 = uf4.user_2
where uf1.user_1 = ?

-- 5) All friends of friends of friend of friends of friends of a user

select count(distinct uf5.*) from t_user_friend uf1
inner join t_user_friend uf2 on uf1.user_1 = uf2.user_2
inner join t_user_friend uf3 on uf2.user_1 = uf3.user_2
inner join t_user_friend uf4 on uf3.user_1 = uf4.user_2
inner join t_user_friend uf5 on uf4.user_1 = uf5.user_2
where uf1.user_1 = ?

เปรียบเทียบความเร็วในการ query

เวลาของที่ใช้จากข้อมูลตัวอย่างทีมี 1,000,000 users แบบที่ 1 ไม่มี cost ของการ join แบบที่ 2 ถึง 5 ใช้เวลา ดังนี้

MySQL database query result แบบที่, เวลาที่ใช้ (seconds), จำนวน row count ที่ได้

แบบที่ 2, 0.016, ~2,500
แบบที่ 3, 30.267, ~125,000
แบบที่ 4, 1,543.505, ~600,000
แบบที่ 5, unfinished, N/A

ข้อมูลเดียวกัน แต่เก็บใน Neo4J database

แบบที่ 2, 0.01 ~2,500
แบบที่ 3, 0.168 ~110,000
แบบที่ 4, 1.359 ~600,000
แบบที่ 5, 2.132 ~800,000

สรุป

จะเห็นว่า SQL query ในแบบที่ 5 ใช้เวลาไปมากว่า 1,543.505 (~17 เกือบ 18 วัน) และไม่สามารถได้ข้อมูลกลับมาได้ ไม่เสร็จนั่นเอง ส่วน Neo4j ใช้เวลาเพียง 2.132 วินาที