由于客户数据量越来越大,在实践中让我发现mysql的exists与inner join 和 not exists与 left join 性能差别惊人。
我们一般在做数据插入时,想插入不重复的数据,或者盘点数据在一个表,另一个表否有存在相同的数据会用not exists和exists,例如:
insert into t1(a1) select b1 from t2 where not exists(select 1 from t1 where t1.id = t2.r_id);
如果t1的数据量很大时,性能会非常慢。经过实践,用以下方法能提高很多。
insert into t1(a1) select b1 from t2 left join (select distinct t1.id from t1 ) t1 on t1.id = t2.r_id where t1.id is null;
select * from t1 where exists(select 1 from t2 where t1.id=t2.r_id);
替换为:
select t1.* from t1 inner join (select distinct r_id from t2) t2 on t1.id= t2.r_id
这是实践的得出的结果。不知否有其他更好的方法,或则这个只是特例而已。
相关推荐
NOT IN、JOIN、IS NULL、NOT EXISTS效率对比 语句一:select count(*) from A where A.a not in (select a from B) 语句二:select count(*) from A left join B on A.a = B.a where B.a is null 语句三:select ...
SQL语句优化——in,not in,exists,not exists, left join...on博客所需SQL语句.txt欢迎下载!
mysql多表查询和EXISTS查询性能对比
SQL语句优化——in,not in,exists,not exists, left join...on博客所需SQL语句2.txt,欢迎下载!
exists 和 not exists的详细解释
mysql exists与not exists实例详解 tableA |column1 | column1 |column3 | tableb |column1 | column1 |column3 | 要查询 tableA 的数据,条件是是 tableA.column1 不在 tableB 的 tableB.column2 中 也就是要得到...
搞懂mysql的exists的使用,搞懂mysql的exists的使用。
MySQL优化之in,exists,not in,not exists的区别in与existsin查询过程结论:exists查询过程:结论:not in与not existsnot in查询过程:结论:not exists查询过程:结论: 首先我们使用两个用户表作为实例 insert ...
基本数据插入 except和intersect和exists和not exists和union和union all sql server
关于Exists,not Exists.in ,not in效率的说明。关于Exists,not Exists.in ,not in效率的说明。关于Exists,not Exists.in ,not in效率的说明。关于Exists,not Exists.in ,not in效率的说明。关于Exists,not Exists.in...
总结:exists 和not exists语句强调是否返回结果集,不要求知道返回什么,与in的区别就是,in只能返回一个字段值,exists允许返回多个字段 提醒:文章中提供了exists和not exists的常用示例,已经经过本人测试,...
oracle in和exists性能解析
里面自己根据网上的资源整理出来的一份sql中in,exists,not in,not exists的使用方法以及注意事项等,有助于初学的朋友们借鉴。
非上传者作品。本人只是学习的时候百度到的比较的文档,拿出来和大家分享一下,希望对大家有所帮助!
启动MySQL出错,查看了下状态,发现提示MySQL is not running,but lock exists: 一个网友说可能和log文件有关,于是将log文件给移除了,再重启MySQL终于OK了找了下资料,基本上都是说: 代码如下: # chown -R mysql...
sql语句优化之用EXISTS替代IN、用NOT EXISTS替代NOT IN的语句
使用EXISTS(或NOT EXISTS)关键字引入一个子查询时,子查询实际上不产生任何数据;它只返回TRUE或FALS值。 指定一个子查询,检测行的存在。 如果子查询包含行,则返回 TRUE IN效率要差(返回子查询)
PostgreSQL IN vs EXISTS vs ANYALL vs JOIN性能分析