Mysql SQL 优化案例1

今天公司同事反馈一个SQL语句删除数据删除了一个小时,还没有删除完,强制中断。表中只有几百条数据。

sql 反馈如下:

DELETE t FROM o.`AI_AD_U_L` t WHERE EXISTS (SELECT 1 FROM o.`AI_AD_U_L_TEMP` AS a WHERE a.`ca_id`=t.`ca_id`);

第一步:

查看表上的索引: show index from  AI_AD_U_L; show index from I_AD_U_L_TEMP

 发现关联字段上a.ca_id 上没有索引

第二步:

在表a 上创建索引

alter table `AI_AD_U_L_TEMP` add index idx_ca_id(ca_id);

扫描二维码关注公众号,回复: 5750985 查看本文章

第三步:

开启profiling:

show variables like '%pro%';

set profiling=1;

第四步:

执行SQL;

 耗时98秒。

第五步:

 查看执行时长:

show profiles;

show profile for query N;

 没有哪一步消耗时间特别长。

set profiling=0;

粗略记录,以备忘。

猜你喜欢

转载自www.cnblogs.com/halberd-lee/p/10643431.html