二个表a,b。a表有10万行以上,b表差不多也是10万行左右。如果b表是正确的(即使重复)要把相同名字的人用b表的数据来填充a表,我用了如下命令:update a, b
set a.id=b.id, a.address=b.address, a.gender=b.gender
where b.name LIKE a.name;上边是一个想象的表,在真实情况下,update大概4,5个字段,用时30分钟以上。假设id,name字段都索引过了。实际我要处理几千万数据(a表),而b表仍旧只有10万行。如果按照上边的命令估计至少要30*100分钟以上,显然耗时太长。请问有无办法来优化?谢谢!

解决方案 »

  1.   

    Mysql]两表(多表)关联update的写法: update customers a
       set    city_name=(select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id)
       where  exists (select 1
                      from   tmp_cust_city b
                      where  b.customer_id=a.customer_id
                     )   -- update 超过2个值
       update customers a 
       set    (city_name,customer_type)=(select b.city_name,b.customer_type
                                         from   tmp_cust_city b
                                         where  b.customer_id=a.customer_id)
       where  exists (select 1
                      from   tmp_cust_city b
                      where  b.customer_id=a.customer_id
                     )