mysql - 使用带有“IN 子句”和“=”的查询
问题描述
需要建议,因为由于性能改进,我需要将所有循环查询转换为 IN 子句。我有以下可能因迭代而异的条件..
Where recordID=2323 and (origin=626 or destination=319);
Where recordID=2323 and (origin=319 or destination=789);
Where recordID=2323 and (origin=567 or destination=989);
Where recordID=2323 and (origin=767 or destination=626);
为了提高性能,我想像这样将这些查询转换为 IN 子句。
Where recordID=2323 and (origin IN(626,319,567,767) or destination IN (319, 789, 989, 626));
我的问题是两种方法的结果计数是否相同?是否有任何其他方法或任何其他方式来做到这一点。
问题是我的最终计数与每种方法相比并不相似。
解决方案
不幸的是,没有很好的方法来优化具有or
不同列上的条件的查询。如果查询很简单,您可以使用union all
:
select t.*
from t
where recordID = 2323 and origin = 626
union all
select t.*
from t
where recordID = 2323 and destination = 319 and origin <> 626;
然后,此版本的查询可以使用两个索引:
(recordID, origin)
(recordID, destination, origin)
编辑:
如果您不关心性能,那么您的方法in
很好 - 并且等同于您的原始逻辑。
推荐阅读
- javascript - 如何在保持数据有序状态的同时继续接收数据?
- javascript - 通过在服务器上运行但使用最终用户机器上的默认打印队列的代码打印文档
- r - 为什么 Shiny.onInputChange 在 R Shiny 中有一个不稳定的行为?
- java - 获得差异。azure 托管代理上的 chrome 驱动程序错误
- java - 如何让本地多人java游戏在线运行?
- java - 尽管有`when()`方法它返回null
- clojure - Clojure 规范的“conformer”函数,它是什么?
- python - 使您的类的实例可索引的 Python 方法(添加 [])
- javascript - 在发布为 npm 包之前使用 npm 链接测试组件时出现重复的 ReactJS 导入问题
- xml - Xsl-fo 如何呈现整个报告以及单个元素