1.说说场景
新的需求,需要给一个表(A,80多万数据)添加一个新的字段。这个字端可以根据A表的字段,关联B表(80多万数据)来获取。(80w *80w)。由此可想而知,数据多么庞大。
基于以上场景。
2.写sql
update A a set regist_custno =
(select max(b.regist_custno) fromB b
where b.cust_no=a.cust_no and a.channel_code=b.channel_code and b.CUST_NO!='' and b.cust_no is not null)
3.问题
先在测试环境进行了测试,由于这两张表数据不重要,测试数据基本和生产相同。发现执行到断开连接,仍然没有执行完成(300s)
4.优化
创建A表和B表的关联项的索引 ,以上面的sql为例
create index channel_custno on A(cust_no,channel_code);
create index channel_custnoI on B(cust_no,channel_code);
5.再次尝试
执行了大概1到2秒之间,执行成功。
6.删除索引,以免后续数据维护麻烦
drop index channel_custno;
drop index channel_custnoI;
7.总结
只想说,索引的作用,无法估量吧。