时间:2021-07-01 10:21:17 帮助过:25人阅读
in查询就是先将子查询条件的记录全都查出来,假设结果集为B,共有m条记录,然后再将子查询条件的结果集分解成m个,再进行m次查询。
- <span style="color: #0000ff;">select</span> <span style="color: #808080;">*</span> <span style="color: #0000ff;">from</span> <span style="color: #ff00ff;">user</span> <span style="color: #0000ff;">where</span> <span style="color: #ff00ff;">user_id</span> <span style="color: #808080;">in</span> (<span style="color: #800000; font-weight: bold;">1</span>, <span style="color: #800000; font-weight: bold;">2</span>, <span style="color: #800000; font-weight: bold;">3</span><span style="color: #000000;">);
- #等效于
- </span><span style="color: #0000ff;">select</span> <span style="color: #808080;">*</span> <span style="color: #0000ff;">from</span> <span style="color: #ff00ff;">user</span> <span style="color: #0000ff;">where</span> <span style="color: #ff00ff;">user_id</span> <span style="color: #808080;">=</span> <span style="color: #800000; font-weight: bold;">1</span> <span style="color: #808080;">or</span> <span style="color: #ff00ff;">user_id</span> <span style="color: #808080;">=</span> <span style="color: #800000; font-weight: bold;">2</span> <span style="color: #808080;">or</span> <span style="color: #ff00ff;">user_id</span> <span style="color: #808080;">=</span> <span style="color: #800000; font-weight: bold;">3</span>;
因为索引,in主要用到了外表的索引,exist用的是子查询的索引
5.5以后的MySQL版本在exists匹配查询结果时使用的是Block Nested-Loop(Block嵌套循环,引入join buffer,类似于缓存功能)开始对查询效率产生显著影响,尤其针对<font color=red>子查询结果集很大</font>的情况下能显著改善查询匹配效率:
通过以上分析,很容易得出下面的结论:
MySQL(七)|MySQL中In与Exists的区别(1)
MySQL(八)|MySQL中In与Exists的区别(2)
(转)MySQL中In与Exists的区别
标签:效果 strong 重要 判断 可靠 href lock 相关 掌握