早就听说 MySQL 中的 in/not in 操作巨慢,今天在工作中真实体验了一把。10 万条数据,下面的 sql 语句跑了半小时还没完成。
delete from activity_student where student_id not in (select sid from students)
解决办法是通过临时表创建索引,用空间换时间避免频繁读取原表信息。
delete from activity_student where student_id not in (select * from (select sid from students) tmp)
改成上面的 sql 语句后,40 秒结束战斗!
网上说 not exist 比 not in 执行效率高,后续测试一下再发出来。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。