Answer the question
In order to leave comments, you need to log in
Slow SQL query, how to optimize it?
Good day!
A friend asked me to do something to stop the cart in OpenCart from slowing down.
I'm a so-so programmer, actually, but with the help of profiling, I realized that the following query is executed for almost 10 seconds:
DELETE
order,
order_product,
order_option,
order_download,
order_total,
order_voucher
FROM order
LEFT JOIN order_product ON order_product.order_id = `order`.order_id
LEFT JOIN order_option ON order_option.order_id = `order`.order_id
LEFT JOIN order_download ON order_download.order_id = `order`.order_id
LEFT JOIN order_total ON order_total.order_id = `order`.order_id
LEFT JOIN order_voucher ON order_voucher.order_id = `order`.order_id
WHERE
order.order_id = '718707' AND order.order_status_id = 0
Answer the question
In order to leave comments, you need to log in
For all subordinate tables, provide a relationship through FOREIGN KEY ON DELETE CASCADE and delete only the entry from `order`
Make with explain at you LEFT JOIN tables do not pass a case?
Well, if hard deletion is needed, then I would make
cascade delete mysql using the database tools.
When deleting a record from the order, everything connected will be deleted.
Did you forget about indexes?
The very first - put down indexes on all related fields - 100% execution time will drop to less than tenths of a second.
And what prevents from each table to delete the necessary lines by separate requests? After all, this is literally a fraction of a second for each request, even if there are no indexes at all. Well and yes, it is necessary to put down indexes in any case.
PS Well, in general, as much as I see the wilds of OpenCart, I'm more and more inclined to think that its development began when the developer was at school... It's all sad...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question