码迷,mamicode.com
首页 > 数据库 > 详细

Mysql 使用临时表比较数据差异以及 临时表的优化

时间:2020-05-09 19:00:55      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:auto   mamicode   表格   color   查询   HERE   har   class   and   

-- 创建内存级别带索引的临时表
CREATE TEMPORARY TABLE atest(
id int(11) NOT NULL AUTO_INCREMENT,
pid bigint(20) Default 0,
sid bigint(20) Default 0,
KEY index_pid (pid),
KEY index_sid (sid)
)  ENGINE =MEMORY DEFAULT CHARSET=utf8;


CREATE TEMPORARY TABLE btest(
pid bigint(20) Default 0,
sid bigint(20) Default 0,
KEY index_pid (pid),
KEY index_sid (sid)
)  ENGINE =MEMORY DEFAULT CHARSET=utf8;

insert into atest select id ,pid ,sid from tb_parent_student;
insert into btest select pid ,sid from  tb_child join tb_parent on pid=pid;

-- 使用普通方式创建默认临时表方法 create temporary table atest(
select id ,pid ,sid from tb_parent_student); create temporary table btest(select pid ,sid from tb_child join tb_parent on pid=pid); select *From atest; select *From btest; -- Exists 比较两个结果集的差异信息 select *From atest where not Exists (select *From btest where atest.pid=btest.pid and atest.sid=btest.sid); select *From btest where not Exists (select *from atest where atest.pid=btest.pid and atest.sid=btest.sid); -- left join select *From atest m left join btest as a on m.pid=a.pid and m.sid=a.sid; select *From atest m left join btest as a on m.pid=a.pid and m.sid=a.sid;

 其中:atest 和btest 两个临时表格的数据都有近二十万数据。

使用普通方式创建默认临时表执行比较结果集语句耗时(其实还没执行完我受不了了直接断开了,后来实际测试大致执行了38分钟!):

技术图片

使用内存级别加索引方式穿件临时表执行比较结果耗时:

技术图片

知道优化后查询会快很多,但是没想到能快这么多。

 

Mysql 使用临时表比较数据差异以及 临时表的优化

标签:auto   mamicode   表格   color   查询   HERE   har   class   and   

原文地址:https://www.cnblogs.com/chongyao/p/12859190.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!