M
M
maksimsokolov2019-11-10 17:19:47
opencart
maksimsokolov, 2019-11-10 17:19:47

How to optimize a slow query to the Opencart DB?

Hello! We have a problem with the site.
Categories take a long time to open because of this request:
Info: SELECT p.product_id, (SELECT AVG(rating) AS total FROM oc_review r1 WHERE r1.product_id = p.product_id AND r1.status = '1' GROUP BY r1.product_id) AS rating, (SELECT price FROM oc_product_discount pd2 WHERE pd2.product_id = p.product_id AND pd2.customer_group_id = '1' AND pd2.quantity = '1' AND ((pd2.date_start = '0000-00-00' OR pd2.date_start < NOW()) AND (pd2. date_end = '0000-00-00' OR pd2.date_end > NOW())) ORDER BY pd2.priority ASC, pd2.price ASC LIMIT 1) AS discount, (SELECT price FROM oc_product_special ps WHERE ps.product_id = p.product_id AND ps.customer_group_id = '1' AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW ())) ORDER BY ps.priority ASC, ps.price ASC LIMIT 1) AS special FROM oc_category_path cp LEFT JOIN oc_product_to_category p2c ON (cp.category_id = p2c.category_id) LEFT JOIN oc_product p ON (p2c.product_id = p.product_id) LEFT JOIN oc_product_description pd ON (p.product_id = pd. product_id) LEFT JOIN oc_product_to_store p2s ON (p.product_id = p2s.product_id) WHERE pd.language_id = '1' AND p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '0' AND cp.path_id = '6562' GROUP BY p.product_id ORDER BY (CASE WHEN special IS NOT NULL THEN special WHEN discount IS NOT NULL THEN discount ELSE p.price END) ASC, LCASE(pd.name) ASC LIMIT 0.102product_id) LEFT JOIN oc_product_to_store p2s ON (p.product_id = p2s.product_id) WHERE pd.language_id = '1' AND p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '0' AND cp.path_id = '6562' GROUP BY p.product_id ORDER BY (CASE WHEN special IS NOT NULL THEN special WHEN discount IS NOT NULL THEN discount ELSE p.price END) ASC, LCASE(pd.name) ASC LIMIT 0.102product_id) LEFT JOIN oc_product_to_store p2s ON (p.product_id = p2s.product_id) WHERE pd.language_id = '1' AND p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '0' AND cp.path_id = '6562' GROUP BY p.product_id ORDER BY (CASE WHEN special IS NOT NULL THEN special WHEN discount IS NOT NULL THEN discount ELSE p.price END) ASC, LCASE(pd.name) ASC LIMIT 0.102
This is a CMS problem, this CMS has a constant problem, since it uses unoptimized sql queries. In particular, this one takes 11 seconds and iterates over 224 thousand rows:
****************************** 1. row ******** *******************
id: 1
select_type: PRIMARY
table: p2s
partitions: NULL
type: ref
possible_keys: PRIMARY,product_id,store_id
key: store_id
key_len: 4
ref: const
rows: 224043
filtered: 100.00
Extra: Using index; using temporary; Using filesort It is
necessary to optimize this query, since the selection is made from the oc_product_to_store table in which there are 2 fields and indexing does not help. How to deal with it?
Opencart 3, is on the VPS.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
web-mechanic, 2019-12-02
@amfetamine

You are clearly doing something wrong, the request is not so tricky, it is standard, in fact you need to look at least at the copies of the database, otherwise you can only guess what the problem is

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question