gpt4 book ai didi

mysql - MySQL 和 Neo4J 中的 friend 的 friend 的 friend 的 friend 的...关系的比较

转载 作者:行者123 更新时间:2023-11-29 14:09:02 26 4
gpt4 key购买 nike

为了了解使用 Neo4J 建立 friend 关系的优势,我在 MySQL 数据库上创建了一张用于 Persons 的表(“Persons”,20900 个数据集):

id     | name
--------------
1 | Peter
2 | Max
3 | Sam
... | ...
20900 | Rudi

和一张关系表(“友谊”,每个人有 50 到 100 个 friend ):

personen_id_1 | personen_id_2
-------------------------
1 | 2
1 | 3
2 | 56
... | ...
20900 | 201

因此,大约有 120 万个关系。

现在我想要 id=1 的人的 friend 的 friend 的 friend 的 friend ,所以我制作了一个这样的查询:

select distinct P.name
from Friendships f
join Friendships f2 ON f.personen_id_2 = f2.personen_id_1
join Friendships f3 ON f2.personen_id_2 = f3.personen_id_1
join Friendships f4 ON f3.personen_id_2 = f4.personen_id_1
join Persons P ON f4.personen_id_2 = P.id
where f.personen_id_1 = 1

用户 ID 1 的查询花费了大约 30 秒

在 Neo4J 中,我为每个人创建了一个具有一个名称属性的节点(20900 个节点)。所有节点的连接都等于 MySQL 中的 Friendships 表,因此有 120 万个关系。

为了在这里获得相同的 frinedset,我输入了 gremlin:

gremlin> g.v(1).outE.inV.loop(2){ it.loops <= 4 }.name.dedup.map()

这大约需要 1 分钟。我根本没想到会这样!

那么我的比较正确吗?如果是,如何修改此示例以显示使用 neo4j 完成此任务的优势?

最佳答案

我对 Gremlin 不太熟悉,但我生成了一个类似大小的数据集(统计数据如下)并在 Cypher 中运行了等效的查询:

START person=node:user(name={name})
MATCH person-[:FRIEND]-()-[:FRIEND]-()-[:FRIEND]-()-[:FRIEND]-friend
RETURN friend.name AS name

我对数据集运行了 1000 次,每次都选择不同的用户作为起点。在运行测试之前我没有预热缓存,所以这是从头开始的。平均响应时间:33 毫秒。

在 MacBook Pro、2.2 GHz Intel Core i7、8 GB RAM、4 GB 堆上运行

以下是图表统计数据:

+----------------------------------------------+
| user | 20900 |
+----------------------------------------------+
| | Average | High | Low |
+----------------------------------------------+
| FRIEND |
+----------------------------------------------+
| OUTGOING | 74 | 100 | 48 |
| incoming | 74 | 123 | 31 |
+----------------------------------------------+

+----------------------------------------------+
| _UNKNOWN | 1 |
+----------------------------------------------+
| | Average | High | Low |
+----------------------------------------------+

+----------------------------------------------+
| Totals |
+----------------------------------------------+
| Nodes | 20901 |
| Relationships | 1565787 |
+----------------------------------------------+
| FRIEND | 1565787 |
+----------------------------------------------+

关于mysql - MySQL 和 Neo4J 中的 friend 的 friend 的 friend 的 friend 的...关系的比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13820313/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com