MySQL storing undirected graph edges efficiently(MySQL有效地存储无向图边)
问题描述
我想存储无向图的边(例如,给朋友).要存储和检索节点 a 的所有朋友,可以使用:
I want to store undirected graph edges (for example, for friends). To store and retrieve all friends of node a, one can use:
每条边创建两行,每个节点查询一列:
Create two rows per edge, query on one column per node:
+--------------------------+
| id | from_node | to_node |
+--------------------------+
| 1 | a | b |
| 2 | b | a |
+--------------------------+
SELECT * FROM `x` WHERE from_node = a
每条边创建一行,使用OR:
Create one row per edge, use OR:
+--------------------------+
| id | node_a | node_b |
+--------------------------+
| 1 | a | b |
+--------------------------+
SELECT * FROM `y` WHERE node_a = a OR node_b = a
哪种方法可以提高查找效率?
Which makes for more efficient lookups?
- 表
x具有2n行,from_node和to_node上的索引,一列查找 - 表
y具有n行,node_a和node_b上的索引,使用OR
- Table
xwith2nrows, indices onfrom_nodeandto_node, lookup on one column - Table
ywithnrows, indices onnode_aandnode_b, lookup on both columns usingOR
推荐答案
如果你优化一切,那么 X 将是最快的,假设你从磁盘读取数据并查询一个人的朋友.那是因为您可以在磁盘上排列您的数据,以便它们被排序以匹配一个索引,即您正在查询的索引.所以,对于一个人,你只需要做一次磁盘搜索.Y 需要对两个索引进行查询,因此可能意味着多次搜索以检索朋友,即使对于一个人也是如此(并且磁盘访问时间通常支配简单查询).
if you optimise everything, then X will be fastest, assuming that you read data from disk and are querying for friends of a single person. that's because you can arrange your data on disk so that they are ordered to match one index, which is the one you are querying. so, for a single person, you only need to do one disk seek. Y requires queries on two indices, so may imply multiple seeks to retrieve friends, even for a single person (and disk access time usually dominates simple queries).
请参阅维基百科的聚集索引(以及mysql 手册)
see clustered indices at wikipedia (and the mysql manual)
如果你有幸知道数据总是在内存中,那么它们可能都足够快"(即使数据在磁盘上,它们也可能足够快——我不是说 X 是最好的设计,只有它才能最有效).
if you are lucky enough to know that data will always be in memory then they will likely both be "fast enough" (and even if the data are on disk they may be fast enough - i am not saying X is the best design, only that it can be made most efficient).
这篇关于MySQL有效地存储无向图边的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MySQL有效地存储无向图边
基础教程推荐
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 无法解决整理冲突 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 在 SQL 中连接多个表 2021-01-01
