S
S
Sergey Erin2021-07-17 13:12:15
MySQL
Sergey Erin, 2021-07-17 13:12:15

Why is OUTER JOIN faster than INNER JOIN?

I synchronize prices in the database, I use a temporary table for this

CREATE TEMPORARY TABLE `tmp_products` (
  `main_price` decimal(15,4) NOT NULL DEFAULT '0.0000',
  `sale_price` decimal(15,4) NOT NULL DEFAULT '0.0000',
  `product_id` int(11) DEFAULT NULL,
  `option_value_id` int(11) DEFAULT NULL,
  `price` decimal(15,4) NOT NULL DEFAULT '0.0000'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

When updating product options, I execute the following request
UPDATE `oc_product_option_value` `pov`
INNER JOIN `tmp_products` `tmp` 
    ON `tmp`.`product_id` =  `pov`.`product_id` 
    AND `tmp`.`option_value_id` =  `pov`.`option_value_id`
SET  `pov`.`price` = `tmp`.`price`;


But the problem is that the execution time reaches 5 minutes (there are only about 1700 options), and if you replace the connection with a LEFT JOIN request, it will be completed in 1 minute. I solved the problem by adding an index INDEX (`product_id`, `option_value_id`), I just would like to understand the reason for such a difference, while the inner join should work faster than the outer one. Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lazy @BojackHorseman MySQL, 2021-07-17
Tag

because the execution plans are different. compare plans, there is an answer

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question