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 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试