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
x
with2n
rows, indices onfrom_node
andto_node
, lookup on one column - Table
y
withn
rows, indices onnode_a
andnode_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 Server 2016更改对象所有者 2022-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01