MySQL解释显示"使用临时"

时间:2017-08-28 22:12:11

标签: mysql optimization key explain

我正在尝试优化mySQL查询,我遇到了一个奇怪的问题。

如果我运行以下查询:

SELECT `survey_visit`.`id` 
FROM `survey_visit` 
LEFT JOIN `campaign` ON `survey_visit`.`campaign_alphanumeric` = `campaign`.`alphanumeric` 
ORDER BY `visit_date` DESC 
LIMIT 5

EXPLAIN显示我在survey_visit表中读了5行。但是,如果我按如下方式进行第二次加入:

SELECT `survey_visit`.`id` 
FROM `survey_visit` 
LEFT JOIN `campaign` ON `survey_visit`.`campaign_alphanumeric` = `campaign`.`alphanumeric` 
LEFT JOIN `client` ON `campaign`.`fk_clientID` = `client`.`id`
ORDER BY `visit_date` DESC 
LIMIT 5

然后它使用临时表,使用filesort并读取所有记录。请注意,问题非常特定于加入"客户端"表。我试过加入其他表格,一切正常。我检查了索引,一切似乎都很好。

非常感谢任何建议。

以下是关于第二个查询的EXPLAIN:

[0] => Array
    (
        [id] => 1
        [select_type] => SIMPLE
        [table] => survey_visit
        [type] => ALL
        [possible_keys] => 
        [key] => 
        [key_len] => 
        [ref] => 
        [rows] => 1564
        [Extra] => Using temporary; Using filesort
    )

[1] => Array
    (
        [id] => 1
        [select_type] => SIMPLE
        [table] => campaign
        [type] => eq_ref
        [possible_keys] => alphanumeric
        [key] => alphanumeric
        [key_len] => 12
        [ref] => bpharma.survey_visit.campaign_alphanumeric
        [rows] => 1
        [Extra] => 
    )

[2] => Array
    (
        [id] => 1
        [select_type] => SIMPLE
        [table] => client
        [type] => index
        [possible_keys] => PRIMARY,id_UNIQUE
        [key] => PRIMARY
        [key_len] => 4
        [ref] => 
        [rows] => 2
        [Extra] => Using where; Using index; Using join buffer (Block Nested Loop)
    )

编辑: 这是查询的EXPLAIN 1.谢谢

[0] => Array
    (
        [id] => 1
        [select_type] => SIMPLE
        [table] => survey_visit
        [type] => index
        [possible_keys] => 
        [key] => survey_visit_visit_date_index
        [key_len] => 6
        [ref] => 
        [rows] => 5
        [Extra] => 
    )

[1] => Array
    (
        [id] => 1
        [select_type] => SIMPLE
        [table] => campaign
        [type] => eq_ref
        [possible_keys] => campaign_alphanumeric_uindex
        [key] => campaign_alphanumeric_uindex
        [key_len] => 12
        [ref] => bpharma.survey_visit.campaign_alphanumeric
        [rows] => 1
        [Extra] => Using index
    )

0 个答案:

没有答案