简体   繁体   English

数据库挑战:如何有效地更新一百万条记录?

[英]Database challenge: How to Update 1 million records efficiently?

I am struggling to update my massive database but my wamp/Heidisql keeps crashing due to this large update/comparisons. 我正在努力更新庞大的数据库,但由于进行大量更新/比较,我的wamp / Heidisql一直崩溃。

I have two database tables: main table "member_all" (contains 3 million records) and child table:"mobile_results" (contains 9,000 records). 我有两个数据库表:主表“ member_all”(包含300万条记录)和子表:“ mobile_results”(包含9000条记录)。 The database structure of tables look like this: 表的数据库结构如下所示:

Main Table ("member_all") 主表(“ member_all”)

id int(11),
name varchar(255),
phoneWork varchar(255),
phoneMobile  varchar(255),
phoneMobileNetwork varchar(255)

Data in table looks like this: 表中的数据如下所示:

id name      phoneWork      phoneMobile   phoneMobileNetwork 
1  bill      061090999990   0789867676    Null
3  billsaasa 06109094399990 076689867676  Null

Child Table : ("mobile_results") 子表:(“ mobile_results”)

id int(11) autoincrement,
phoneMobile varchar(255),
phoneMobileNetwork  varchar(255)

Data in mobile_results looks like this: mobile_results中的数据如下所示:

id     phoneMobile  phoneMobileNetwork
8789   0789867676   Orange     
238789 076689867676 O2

All my mobile network data for 9,000 mobile number is stored in "mobile_results" but when i try to compare both these table ,i get stuck and my wamp/Heidi sql crashes? 我所有9000个移动电话号码的移动网络数据都存储在“ mobile_results”中,但是当我尝试比较这两个表时,我卡住了,我的wamp / Heidi sql崩溃了吗?

My question is : 我的问题是:

How can i populate "member_all" with "phoneMobileNetwork" values from "mobile_results" efficiently? 如何有效地使用“ mobile_results”中的“ phoneMobileNetwork”值填充“ member_all”?

Here are the queries i have tried: 这是我尝试过的查询:

Query 1 查询1

i divided my query using limit .This is slow and would also take 1 week to compare 9,000 records from mobile_results. 我用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);

Query 2 查询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);

Same not good for large number of records. 同样不利于大量记录。

PLEASE help me how can i update records my "member_all" table efficiently in one go. 请帮助我如何高效地一次更新记录“ member_all”表。

I would appreciate you help in this regard. 谢谢您在这方面的帮助。

Can you just try this.I think using Exist will be faster 您能尝试一下吗?我认为使用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);

You could try joing the tables on the update. 您可以尝试在更新中加入表格。 No EXISTS , no IN . EXISTS ,没有IN Like this: 像这样:

update member_all AS ma
  JOIN mobile_results ms on ma.phoneMobile=ms.phoneMobile
  SET member_all.phoneMobileNetwork=mobile_results.phoneMobileNetwork

Reference: 参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM