我有以下 mysql 查询,运行大约需要 3 分钟。它确实有 2 个子查询,但表的行数很少。在进行解释时,看起来“使用临时”可能是罪魁祸首。显然,数据库似乎正在为所有三个查询创建一个临时表,如下面的“使用临时”名称中所述。
让我感到困惑的是 MySQL 文档说,使用临时性通常是由 group by 和 order by 引起的,我都没有使用。子查询是否会导致隐含的 group by 或 order by?无论 group by 或 order by 是否都需要导致临时表的子查询?有关如何重组此查询以便 MySQL 可以更有效地处理它的任何建议?MySQL设置中的任何其他调整想法?
mysql> explain
SELECT DISTINCT COMPANY_ID, COMPANY_NAME
FROM COMPANY
WHERE ID IN (SELECT DISTINCT ID FROM CAMPAIGN WHERE CAMPAIGN_ID IN (SELECT
DISTINCT CAMPAIGN_ID FROM AD
WHERE ID=10 AND (AD_STATUS='R' OR AD_STATUS='T'))
AND (STATUS_CODE='L' OR STATUS_CODE='A' OR STATUS_CODE='C'));
+----+--------------------+----------+------+---------------+------+---------+------+------+------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+----------+------+---------------+------+---------+------+------+------------------------------+
| 1 | PRIMARY | COMPANY | ALL | NULL | NULL | NULL | NULL | 1207 | Using where; Using temporary |
| 2 | DEPENDENT SUBQUERY | CAMPAIGN | ALL | NULL | NULL | NULL | NULL | 880 | Using where; Using temporary |
| 3 | DEPENDENT SUBQUERY | AD | ALL | NULL | NULL | NULL | NULL | 264 | Using where; Using temporary |
+----+--------------------+----------+------+---------------+------+---------+------+------+------------------------------+
谢谢!菲尔