doumaque6551 2014-09-22 10:40
浏览 445
已采纳

Mysql根据另一个表更新一个表列大量数据

I am stuck to update one column of table by comparing with another table in php/Mysql. I have tried to speed up the process by indexing the table columns, optimizing the query etc but unable to speed up the process.

In my php based application there is two table (table A and table B) , I want to update one column of table A by comparing with table B (with two column - name & sku).

Previously above process has taken max 15 mints to update 28k products. But now both table (table A and table B) have 60k rows. Now it's taking more than two hours. I have used below query

mysql_query("UPDATE tableA a 
            JOIN tableB b ON a.product_code_sku = b.sku 
            SET a.is_existing_product = '1'") or die(mysql_error());

mysql_query("UPDATE tableA a 
       JOIN tableB b ON a.product_name = b.product_name 
       SET a.is_existing_product = '1'") or die(mysql_error()); 

Above query was very slow after that I have changed the updating process like below

$query_result = mysql_query("SELECT t1.`id`,t2.`product_id` FROM `tableA` t1,
                     `tableB` t2 where (t1.product_code_sku = t2.sku
                      or t1.product_name = t2.product_name)") or die (mysql_error());
while($result_row = mysql_fetch_array($query_result))    
{
    mysql_query("UPDATE `tableA` SET is_existing_product = '1' 
               where id = '".$result_row['id']."' ") or die (mysql_error());  
}

But all of my efforts are in vain.

Please advice me how to make the process faster.

  • 写回答

1条回答 默认 最新

  • doujian4752 2014-09-22 10:58
    关注

    Your first update query and the second update query is doing two different thing. The second query is slower because you are using a OR for comparison.

    You can consider to create a temporary table to compare and insert, the update back to tableA.

    First and all, you should examine the execution for the two join queries, like

    desc select a.id
    from tableA a 
    join tableB b ON a.product_code_sku = b.sku;
    

    If this is the reason why the update is slow, you should optimize the query. Otherwise, you can try the below:

    For instance (assuming ID the primary key),

    // make sure the columns are in the same data type
    create table tmp_sku (
      id .. // just the primary key, make sure is using the same data type as in tableA
    );
    
    // do a insert into this temporary table
    insert into tmp_sku select a.id
    from tableA a 
    join tableB b ON a.product_code_sku = b.sku;
    
    // now we have list of matches,
    // then do a insert .. duplicate key update
    // by comparing the primary id
    insert into tableA (id, is_existing_product)
    select tmp_sku.id, 1 from tmp_sku
    on duplicate key set is_existing_product = 1;
    
    // repeat for the product name
    truncate tmp_sku;
    insert into tmp_sku
    select a.id
    from tableA a 
    join tableB b ON a.product_name = b.product_name;
    
    // repeat the duplicate .. update
    insert into tableA (id, is_existing_product)
    select tmp_sku.id, 1 from tmp_sku
    on duplicate key set is_existing_product = 1;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#stm32#的问题:寻找一块开发版,作为智能化割草机的控制模块和树莓派主板相连,要求:最低可控制 3 个电机(两个驱动电机,1 个割草电机),其次可以与树莓派主板相连电机照片如下:
  • ¥15 Mac(标签-IDE|关键词-File) idea
  • ¥15 潜在扩散模型的Unet特征提取
  • ¥15 iscsi服务无法访问,如何解决?
  • ¥15 感应式传感器制作的感应式讯响器
  • ¥15 如何使用SC92F8003固件库解析私有协议数据?
  • ¥15 如何在音频中嵌入字符串(水印)信息进行传递
  • ¥30 plc怎么以设计说明书申请软著
  • ¥15 硬盘识别不了,需要初始化,可我的数据怎么办
  • ¥15 lvm2被mask了,怎么unmask都没用(标签-ubuntu|关键词-apt)