今天写了一条数据更新操作的SQL语句
如下
update shop as t1
set
t1.STREET=
(select distinct (concat(t2.DSCP," ",coalesce(t2.STREET)))
from mall as t2
where t1.MALL_CD=t2.CODE),
t1.STREET_LOC_1=
(select distinct (concat(t2.DSCP_LOC_1," ",coalesce(t2.STREET_LOC_1)))
from mall as t2
where t1.MALL_CD=t2.CODE),
t1.STREET_LOC_2=
(select distinct (concat(t2.DSCP_LOC_2," ",coalesce(t2.STREET_LOC_2)))
from mall as t2
where t1.MALL_CD=t2.CODE);
当时也没考虑很多【数据量比较大】,结果把语句执行好才意识到,效率太低了,一位前辈告诉我其实那个更新语句可以如下所示,效率会提高很多。
update shop as sh, mall as ma set sh.STREET=concat(ma.DSCP,' ',ma.STREET),sh.STREET_LOC_1=concat(ma.DSCP_LOC_1,' ',ma.STREET_LOC_1),sh.STREET_LOC_2=concat(ma.DSCP_LOC_2,' ',ma.STREET_LOC_2) where sh.MALL_CD=ma.CODE
对照比较发现,的确如此。看来以后要注意了!
如下
update shop as t1
set
t1.STREET=
(select distinct (concat(t2.DSCP," ",coalesce(t2.STREET)))
from mall as t2
where t1.MALL_CD=t2.CODE),
t1.STREET_LOC_1=
(select distinct (concat(t2.DSCP_LOC_1," ",coalesce(t2.STREET_LOC_1)))
from mall as t2
where t1.MALL_CD=t2.CODE),
t1.STREET_LOC_2=
(select distinct (concat(t2.DSCP_LOC_2," ",coalesce(t2.STREET_LOC_2)))
from mall as t2
where t1.MALL_CD=t2.CODE);
当时也没考虑很多【数据量比较大】,结果把语句执行好才意识到,效率太低了,一位前辈告诉我其实那个更新语句可以如下所示,效率会提高很多。
update shop as sh, mall as ma set sh.STREET=concat(ma.DSCP,' ',ma.STREET),sh.STREET_LOC_1=concat(ma.DSCP_LOC_1,' ',ma.STREET_LOC_1),sh.STREET_LOC_2=concat(ma.DSCP_LOC_2,' ',ma.STREET_LOC_2) where sh.MALL_CD=ma.CODE
对照比较发现,的确如此。看来以后要注意了!
本文通过对比两种不同的SQL更新语句,展示了如何通过优化SQL语句结构来显著提升数据更新操作的效率。作者分享了一个实例,说明了使用内联连接代替子查询可以大幅减少执行时间。
650

被折叠的 条评论
为什么被折叠?



