生成行组合的最快方法
我们有一个计划表,每个计划都有许多服务。我们希望有一种快速的方法来查找不包含重复服务但包含某些服务的计划组合。
例如表格计划
id | service_1 | service_2 | ...
---------------------------------
1 | true | true | ...
2 | true | false | ...
3 | false | true | ...
例如包含 service_1 和 service_2 的有效组合
更新
如果有 2 项服务并且我需要这两项服务,我们将组合最多 2 行(或计划),因为每行中至少可以包含 1 项服务。
id | service_1 | service_2 | id | service_1 | service_2 |
---------------------------------------------------------
1 | true | true |NULL| NULL | NULL |
2 | true | false | 3 | false | true |
更新
它目前通过积极的修剪或行自行连接来工作。查询是根据服务数量动态生成的。它创建了有效连接条件的排列,使得发布不切实际。
目前,成本按计划数量 ^ 服务数量的顺序排列。
我最感兴趣的是解决这个问题的其他方法,不一定是对当前方法的改进。
We have a table of plans and each plan has many services. We would like a fast way of finding the combinations of plans that do not contain duplicate services but as a combinations contain certain services.
e.g. table of plans
id | service_1 | service_2 | ...
---------------------------------
1 | true | true | ...
2 | true | false | ...
3 | false | true | ...
e.g. valid combinations containing service_1 and service_2
UPDATE
If there were 2 services and I required both of them we would combine up to 2 rows (or plans) as they could contain at minimum 1 service in each.
id | service_1 | service_2 | id | service_1 | service_2 |
---------------------------------------------------------
1 | true | true |NULL| NULL | NULL |
2 | true | false | 3 | false | true |
UPDATE
It currently works by self left joining itself with aggressive pruning or rows. The query is dynamically generated based on the number of services. It creates the permutations of valid join conditions making it not practical to post.
Currently the cost is in the order of number of plans ^ number of services.
I'm mostly interested in other ways of solving this not necessarily improvements to the current way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这似乎工作正常
设置数据
查询
结果
This seems to work ok
Setup data
The query
Result
正如我在评论中提到的,确定“重复”的规则尚不清楚。然而,从它的声音来看,你只是在做按位与。
但是,您会注意到在此解决方案中我得到了 id=3 的行。出于同样的原因,id=3 是 id=2 的“重复”,反之亦然。
如果这是不正确的,我们需要更清晰的数据和一些更好的样本数据来说明什么是“重复”和什么不是“重复”的边缘情况。
更新
鉴于cyberwiki在评论中所述,如果每个计划所寻求的是另一个计划,当组合起来时仅提供一次所有服务,那么所寻求的是一个二进制的恭维,它将产生所有1 的。我们可以通过查找所有计划来做到这一点,这些计划在与当前计划进行异或时会产生所有计划:
再次注意,id=3 将显示在结果中,因为正如 id=3 与 id=2 完美匹配一样,反之亦然也是如此。
As I mentioned in comments, the rule by which one determines a "duplicate" is unclear. However, from the sounds of it, you are simply doing a bitwise AND.
However, you will note in this solution that I get a row for id=3. For the same reason that id=3 is a "duplicate" for id=2, the reverse is also true.
If this is not correct, we need far more clarity and some better sample data that illustrates the edge cases of what is and is not a "duplicate".
Update
Given what cyberwiki has stated in comments, if what is being sought for each plan is another plan which when combined provide all services exactly once, then what is being sought is a binary one's compliment that will produce all 1's. We can do that by finding all plans which when XOR'd to the current plan produce all ones:
Again, note that id=3 will show in the result because just as id=3 is perfect match for id=2, the reverse is also true.