【发布时间】:2012-01-10 19:06:07
【问题描述】:
这个月我在两份不同的工作中遇到了同样的问题:
Version 1: User 1 & User 2 are friends
Version 2: Axis 1 & Axis 2 when graphed should have the quadrants colored...
问题是,我没有看到一种优雅的方式,使用 RDBMS 来存储和查询这些信息。
有两种明显的方法:
方法一:
store the information twice (i.e. two db rows rows per relationship):
u1, u2, true
u2, u1, true
u..n, u..i, true
u..i, u..n, true
have rules to always look for the inverse on updates:
on read, no management needed
on create, create inverse
on delete, delete inverse
on update, update inverse
Advantage: management logic is always the same.
Disadvantage: possibility of race conditions, extra storage (which is admittedly cheap, but feels wrong)
方法二:
store the information once (i.e. one db row per relationship)
u1, u2, true
u..n, u..i, true
have rules to check for corollaries:
on read, if u1, u2 fails, check for u2, u1
on create u1, u2: check for u2, u1, if it doesn't exist, create u1, u2
on delete, no management needed
on update, optionally redo same check as create
Advantage: Only store once
Disadvantage: Management requires different set of cleanup depending on the operation
我想知道是否有第三种方法可以遵循“使用 f(x,y) 的键,其中 f(x,y) 对于每个 x,y 组合都是唯一的,而 f(x,y) === f(y,x)"
我的直觉告诉我,应该有一些按位运算的组合可以满足这些要求。类似于两列的东西:
key1 = x && y key2 = x + y
我希望那些在数学系花更多时间而在社会学系花更少时间的人已经看到了这种可能性或不可能性的证据,并且可以提供一个快速的“[你这个白痴]它很容易证明(im)possible, see this link"(名字调用可选)
任何其他优雅的方法也将受到欢迎。
谢谢
【问题讨论】:
-
你不能使用基于图形的数据库吗?例如 Neo4j。这将允许您完全捕获您的图形关系。您还可以使用 Mongo 并将其全部存储在 Json 对象中。
-
这甚至不是“面向文档”又名“NoSQL”数据库的优雅支持。
-
@steve 是的,Neo4j 是一个真正的选择(尽管我不是他们许可证的忠实粉丝)。 Neo4j 对此进行了抽象,但他们必须以某种方式解决基本问题。我想知道怎么做。
标签: database database-design social-networking bit-manipulation bitmask