I have a table imp_acc_act_itm that contains around 36 Millions records, and the following query runs very fast and returns only 21 records from this table in less than 5ms
SELECT SKU
FROM imp_acc_act_itm AS ii
WHERE exists
(
SELECT 1
FROM acc_opr_rule_skus AS rs
WHERE rs.object IN (2006)
AND ii.sku = rs.value
);
Now when trying to insert those record into another table, or creating a temporary table from it, it takes more than a minute to finish:
CREATE TEMPORARY TABLE temp_table AS
SELECT SKU
FROM imp_acc_act_itm AS ii
WHERE exists
(
SELECT 1
FROM acc_opr_rule_skus AS rs
WHERE rs.object IN (2006)
AND ii.sku = rs.value
);
Anyone has an explanation for that?
Please note that I am using MariaDB-10.3.14 with InnoDB Engine. And here is the Explain of the Select Query:
+------+-------------+-------+------+--------------------------------------------------------------------+--------------------------+---------+----------------------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +------+-------------+-------+------+--------------------------------------------------------------------+--------------------------+---------+----------------------+------+-------------+ | 1 | PRIMARY | rs | ref | PRIMARY,FK__acc_opr_rule_skus__object,FK__acc_opr_rule_skus__value | PRIMARY | 4 | const | 1 | Using index | | 1 | PRIMARY | ii | ref | FK__imp_acc_act_itm__sku | FK__imp_acc_act_itm__sku | 4 | eshop_fadel.rs.value | 238 | Using index | +------+-------------+-------+------+--------------------------------------------------------------------+--------------------------+---------+----------------------+------+-------------+