duan0065626385 2015-09-02 21:26
浏览 90
已采纳

加速OpenCart中的MySQL查询

I don't know that this is a good question or not, but I have this query in one of my module:

SELECT * FROM `product` WHERE upc IN (
                        SELECT `upc` FROM `product` WHERE `upc` <> '' GROUP BY `upc` HAVING COUNT(*) > 1) 
                    ORDER BY `upc`

the product table is quite big and this query takes about 20 mins to be executed.

I am not a big MySQL expert, but it is possible to speed up this query?

My second heavy query is an UPDATE query:

UPDATE `product` SET `quantity` = '0' WHERE `sku` IN ( SELECT `id_xml_prd` FROM `ixml_prd_map` WHERE `id_xml` = '8' );

Current indexes on the product table:

enter image description here

And on the ixml_prd_map:

enter image description here

  • 写回答

1条回答 默认 最新

  • doulai5585 2015-09-02 21:31
    关注

    You can modify your query with WHERE EXISTS like below instead of having a IN clause with a subquery.

    SELECT * FROM `product` p 
    WHERE EXISTS (
    SELECT 1 FROM 
    `product` 
    WHERE `upc` <> '' AND `upc` = p.`upc`
    GROUP BY `upc` 
    HAVING COUNT(*) > 1) 
    ORDER BY `upc`;
    

    Also you would want to have index on upc column.

    Perform a normal update join query rather than having a IN subquery

    UPDATE `product` p
    JOIN `ixml_prd_map` i ON p.`sku` = i.`id_xml_prd` 
    AND i.`id_xml` = '8'
    SET p.`quantity` = '0';
    

    Also for this UPDATE query to be faster have an index on id_xml_prd and id_xml column ON ixml_prd_map table.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题