not in 查询数据慢的问题

本文介绍两种提高数据库查询效率的方法:一是使用not exists替代not in,二是利用索引进行快速查询。通过具体示例展示了如何应用这两种方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有俩种方法可以提高查询效率, 1、 用not exists 代替  not in  , 这种发法没有改变查询数据的形式,所以可能效果不明显。   2、 利用索引查询, select tbl1.id from table1 tbl1 left join table2 tbl2  on tbl1.id = tbl2.id where tbl2.id = null;  这个是把table2表过滤,查询直接找索引。
 
举例:select tbl1.id from table1 tbl1 where tbl1.id not in (select tbl2.id from table2 tbl2);
使用not exists 代替 not in
select tbl1.id from table1 tbl1 where not exists (select 1 from table2 tbl2 where tbl1.id  = tbl2.id);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值