给定一个具有 10 个 bar_entities 的 foo 记录,并查找具有这 10 个实体中的至少 2 个的所有 foo 记录将导致 45 个可能的相等值 10!/(2!*(10-2)!)=45。
这可以在 10_C_(2-1)=10 次读取中推断出来。
SELECT * from table WHERE bar="1" AND bar in ["2", "3", "4", "5", "6", "7", "8", "9", "0"]
SELECT * from table WHERE bar="2" AND bar in ["3", "4", "5", "6", "7", "8", "9", "0"]
SELECT * from table WHERE bar="3" AND bar in ["4", "5", "6", "7", "8", "9", "0"]
etc.
要将其减少到一次读取,需要在添加 foo 记录时填充一个单独的表,该表具有给定记录的所有 2 个组合。
Say you had
foo_table
foo1 [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
foo2 [1, 3, 4]
foo3 [1, 2, a]
foo4 [b, 6, c]
foo_combo_2_table
Parent Combination
foo1 12
foo1 13
... and all 45 foo1 combinations each in its own row
foo2 13
foo2 14
foo2 34
foo3 12
foo3 1a
foo3 2a
etc.
Now you can do a
indexes = SELECT __KEY__ from foo_combo_2_table WHERE combination IN [12, 13, 14, 15, ... all 45]
keys = [k.parent() for k in indexes] # you would need to filter for duplicates
这样你就不会遇到任何爆炸性的索引问题。
如果您还想做任何 3 个或任何 4 个实体而不是每个实体,则需要创建一个 foo_combo_n_table 或执行 10_C_(n-1) 次读取。