Я пытался оптимизировать свой SQL-запрос, и я столкнулся с поведением, которое не могу понять.
Примечание: если я использую JOIN, он также проверяет все записи таблицы2
Версия - 5.6
QUERY 1 (медленно)
-- Joining 2 table using subqueries.
-- Even If I'm using JOIN also it examines all the records of table2
SELECT
*
FROM
table2
WHERE
table1_id IN (SELECT
id
FROM
table1
WHERE
idx_column = 'value');
-- taking more than 20 seconds
Объясните
+---+-----------+---------------+------+-----------------------------------+----------+---------+---------------+------+------------------------------------------------------------------+
|id |select_type|table | type | possible_keys | key | key_len | ref | rows | Extra |
+---+-----------+---------------+------+-----------------------------------+----------+---------+---------------+------+------------------------------------------------------------------+
|1 |SIMPLE |table1 |ref |PRIMARY,idx_column_idx | PRIMARY |4 | |2 |Using index condition; Start temporary |
|1 |SIMPLE |table2 |ALL |NULL | NULL | NULL | NULL |1129137|Using where; End temporary; Using join buffer (Block Nested Loop)|
+---+-----------+---------------+------+-----------------------------------+----------+---------+---------------+------+------------------------------------------------------------------+
QUERY 2 (Быстро)
-- Separately run the first subquery.
SELECT
id
FROM
table1
WHERE
idx_column = 'value';
---
SELECT
*
FROM
table2
WHERE
table1_id IN ({{Selected Id's from above query}});
-- taking less than a second
Объясните
+---+-----------+---------------+------+-----------------------------------+----------+---------+---------------+------+------------------------------------------------------------------+
|id |select_type|table | type | possible_keys | key | key_len | ref | rows | Extra |
+---+-----------+---------------+------+-----------------------------------+----------+---------+---------------+------+------------------------------------------------------------------+
|1 |SIMPLE |table2 |ref |PRIMARY,table1_id | table1_id|48 | const |2 |Using index condition |
+---+-----------+---------------+------+-----------------------------------+----------+---------+---------------+------+------------------------------------------------------------------+
Объясните @ scaisedge'sЗапрос
+---+------------+--------------------+------+---------------+----------------+---------+------+-------+---------------------------------------------------+
|id |select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+---+------------+--------------------+------+---------------+----------------+---------+------+-------+---------------------------------------------------+
|1 |PRIMARY |<derived2> |ALL |NULL | NULL |NULL |NULL |2 |NULL |
|1 |PRIMARY |table2 |ALL |NULL | NULL |NULL |NULL |1129137|Using where; Using join buffer (Block Nested Loop) |
|2 |DERIVED |table1 |ref |idx_column_IDX | idx_column_IDX | 33 | const|2 |Using index condition |
+---+------------+--------------------+------+---------------+----------------+---------+------+-------+---------------------------------------------------+