MYSQL update join慢SQL

执行5分钟还没执行完;

explain update ind_shop t1 join
(
select shop_id,
(case when `value`='男装' then 1
when `value`='女装' then 2
when `value`='男女装' then 3 else 0 end)
shop_sex
from tmp1014
where brand='未定' and `value`='男女装'
) t2
on t1.shop_id=t2.shop_id
set t1.shop_sex = t2.shop_sex
where t1.shop_id=t2.shop_id;

执行计划如下:

tmp1014为临时表,数据量为3W左右;

ind_shop表数据库33W,shop_id列是唯一索引列;

很明显执行计划有问题;为啥没有走索引?

步骤1.字段类型不一致

查询发现:

tmp1014表shop_id列为bigint

ind_shop表shop_id列为varchar

调整后,执行计划无变化;

步骤2.怀疑统计信息不准

重新收集统计信息后,执行计划无变化;

show index from ind_shop;(无异常)

这时候有点僵住;

仔细查看表结构,发现字符集不一致,tmp1014表为utf8,ind_shop表为utf8mb4;

重新调整表的字符集,得到了正确的执行计划;

执行时间为秒级别;

原文地址:https://www.cnblogs.com/muzisanshi/p/15406329.html