[英]Database challenge: How to Update 1 million records efficiently?
我正在努力更新庞大的数据库,但由于进行大量更新/比较,我的wamp / Heidisql一直崩溃。
我有两个数据库表:主表“ member_all”(包含300万条记录)和子表:“ mobile_results”(包含9000条记录)。 表的数据库结构如下所示:
主表(“ member_all”)
id int(11),
name varchar(255),
phoneWork varchar(255),
phoneMobile varchar(255),
phoneMobileNetwork varchar(255)
表中的数据如下所示:
id name phoneWork phoneMobile phoneMobileNetwork
1 bill 061090999990 0789867676 Null
3 billsaasa 06109094399990 076689867676 Null
子表:(“ mobile_results”)
id int(11) autoincrement,
phoneMobile varchar(255),
phoneMobileNetwork varchar(255)
mobile_results中的数据如下所示:
id phoneMobile phoneMobileNetwork
8789 0789867676 Orange
238789 076689867676 O2
我所有9000个移动电话号码的移动网络数据都存储在“ mobile_results”中,但是当我尝试比较这两个表时,我卡住了,我的wamp / Heidi sql崩溃了吗?
我的问题是:
如何有效地使用“ mobile_results”中的“ phoneMobileNetwork”值填充“ member_all”?
这是我尝试过的查询:
查询1
我用limit限制了查询的速度。这很慢,并且需要1个星期才能比较mobile_results的9,000条记录。
update member_all,mobile_results
set member_all.phoneMobileNetwork=mobile_results.phoneMobileNetwork
where member_all.phoneMobile in
(SELECT phoneMobile FROM mobile_results limit 1,10);
查询2
update member_all,mobile_results
set member_all.phoneMobileNetwork=mobile_results.phoneMobileNetwork
where member_all.phoneMobile in
(SELECT phoneMobile FROM mobile_results where id <10);
同样不利于大量记录。
请帮助我如何高效地一次更新记录“ member_all”表。
谢谢您在这方面的帮助。
您能尝试一下吗?我认为使用Exist会更快
update member_all
set phoneMobileNetwork=
(select phoneMobileNetwork from mobile_results where
member_all.phoneMobile=mobile_results.phoneMobile)
WHERE EXISTS
(
select 1 from mobile_results where
member_all.phoneMobile=mobile_results.phoneMobile);
您可以尝试在更新中加入表格。 无EXISTS
,没有IN
。 像这样:
update member_all AS ma
JOIN mobile_results ms on ma.phoneMobile=ms.phoneMobile
SET member_all.phoneMobileNetwork=mobile_results.phoneMobileNetwork
参考:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.