in 和 exist关键字
In
- 在8.0版本中,mysql优化器会去计算走索引要扫描的行数和时间(如果要回表,则还要计算回表的效率)与走全表扫描的时间,从而选择。
- 为了防止发生死锁和重复,mysql优化器选择排序条件后的范围查询。
- 所以,in条件少,(扫描的row少),一般走索引,条件多,则不走索引。但是如果使用到了联合索引,发生覆盖索引查询,一般都会比全表扫描的时间要短。(同理,使用主键索引不用回表一般也会使用主键索引)
- in条件只有一个会转化成条件查询 =,使用到了索引。其次可能用到的是range, index(索引的全表扫描),all。
结论:in一般会走索引,但是当范围过大,回表次数过多,索引会失效(二级索引)。但是如果查询符合覆盖索引或者查询条件为主键索引(不回表效率肯定高)的,那么,一定会用到索引。
子查询
执行顺序不同,in先执行子查询,exists先执行父查询。
子查询优化
大多数的(in)子查询优化为join, 当in数据量大的时候,子查询会产生临时表,导致索引失效,查询性能降低。
注意:优化的SQL有一定的局限性,不是所有的子查询都可以用join优化,如存在union, group by, order by, limit 等一般不用join,当SQL较为简单才可以优化。
- vscode错误:Unable to connect to VS Code server: Error in request.
- 【五期李伟平】CCF-A(AAAI'21)Game of Gradients: Mitigating Irrelevant Clients in Federated Learning
- git bash报错fatal: detected dubious ownership in repository at的解决方法
- 2024-01-13 Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. ==》引用了未使用的方法导致
- ms sql in 优化
- 立案诉调 All In One
- Early lameness detection in dairy cattle based on wearable gait analysis using semi-supervised LSTM-Autoencoder
- [翻译]-Query and Transaction size in MySQL
- 基于正则化的图自编码器在推荐算法中的应用 Application of graph auto-encoders based on regularization in recommendation algorithms
- mybatisplus in 查询超过1000的工具类
本栏目推荐文章