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 请问这张multisim图的原理是什么,这是一个交通灯,是课程要求,明天要进行解析,但是我们组没一个人会,所以急要,今天要
    • ¥15 ansible tower 卡住
    • ¥15 等间距平面螺旋天线方程式
    • ¥15 通过链接访问,显示514或不是私密连接
    • ¥100 系统自动弹窗,键盘一接上就会
    • ¥50 股票交易系统设计(sql语言)
    • ¥15 调制识别中这几个数据集的文献分别是什么?
    • ¥15 请大家看看报错原因,为啥会这样
    • ¥30 Cocos转UWP应用,支付调起后闪退
    • ¥15 STC32G12K128芯片可以接DS1302时钟模块吗