这个月我在两份不同的工作中遇到了同样的问题:
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)"
我的直觉告诉我,应该有一些按位运算的组合可以满足这些要求。类似于两列的东西:
键1 = x && y 键2 = x + y
我希望那些在数学系花费更多时间而在社会学系花费较少时间的人已经看到了这种可能性或不可能性的证据,并且可以提供一个快速的“[你白痴]它很容易证明(im)可能,请参阅此链接“(名称调用可选)
任何其他优雅的方法也将受到欢迎。
谢谢