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有效地存储无向图边
基础教程推荐
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
- oracle区分大小写的原因? 2021-01-01
- 在多列上分布任意行 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
