Adrian
Adrian

Reputation: 2656

Speed up MySQL query in OpenCart

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

Upvotes: 0

Views: 961

Answers (1)

Rahul
Rahul

Reputation: 77926

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.

Upvotes: 3

Related Questions