S
S
stoletvobed9112016-05-05 12:11:15
MySQL
stoletvobed911, 2016-05-05 12:11:15

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

Help to optimize it, please. What should be done?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
R
Rsa97, 2016-05-05
@stoletvobed911

For all subordinate tables, provide a relationship through FOREIGN KEY ON DELETE CASCADE and delete only the entry from `order`

A
Alexander N++, 2016-05-05
@sanchezzzhak

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.

N
nozzy, 2016-05-05
@nozzy

This suggests INNER JOIN instead of LEFT JOIN.

S
Serdonda, 2016-05-05
@Serdonda

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.

A
Anton Tikhomirov, 2016-05-06
@Acuna

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 question

Ask a Question

731 491 924 answers to any question