为什么不同的执行sql时间?

时间:2016-06-01 17:06:58

标签: mysql sql distinct explain

这里有2个mysql查询...这个很简单,但选择不同的字段 首先在第二个 mytable_planing 上选择 mytable_city ,两个字段都在前面连接表。但查询执行时间差异很大

SELECT SQL_NO_CACHE
DISTINCT `mytable_city`.`NAME` AS `FILTER_CITY`
FROM `b_iblock_element` `mytable` 
    LEFT JOIN `b_iblock_element_property` `mytable_planing_id` 
        ON `mytable`.`ID` = `mytable_planing_id`.`IBLOCK_ELEMENT_ID`
        AND `mytable_planing_id`.`IBLOCK_PROPERTY_ID` = 70
    INNER JOIN `b_iblock_element` `mytable_planing` 
        ON `mytable_planing_id`.`VALUE_HASH` = `mytable_planing`.`XML_HASH`
    LEFT JOIN `b_iblock_element_property` `mytable_housetype_id` 
        ON `mytable_planing`.`ID` = `mytable_housetype_id`.`IBLOCK_ELEMENT_ID`
        AND `mytable_housetype_id`.`IBLOCK_PROPERTY_ID` = 46
    INNER JOIN `b_iblock_element` `mytable_housetype` 
        ON `mytable_housetype_id`.`VALUE_HASH` = `mytable_housetype`.`XML_HASH`
    LEFT JOIN `b_iblock_element_prop_s4` `mytable_street_id` 
        ON `mytable_street_id`.`PROP_60_HASH` = `mytable_housetype`.`XML_HASH`
    INNER JOIN `b_iblock_element` `mytable_street` 
        ON `mytable_street_id`.`IBLOCK_ELEMENT_ID` = `mytable_street`.`ID`
    LEFT JOIN `b_iblock_section` `mytable_city` 
        ON `mytable_street`.`IBLOCK_SECTION_ID` = `mytable_city`.`ID`
    INNER JOIN `b_iblock_element_property` `mytable_archive` 
        ON `mytable`.`ID` = `mytable_archive`.`IBLOCK_ELEMENT_ID`
        AND `mytable_archive`.`IBLOCK_PROPERTY_ID` = 321
    WHERE `mytable`.`IBLOCK_ID` = 7
        AND (`mytable_archive`.`VALUE_NUM` IS NULL OR `mytable_archive`.`VALUE_NUM` = 0)
        AND (`mytable_city`.`NAME` IS NOT NULL AND LENGTH(`mytable_city`.`NAME`) > 0)

时间:1.82977秒

EXPLAIN

id  select_type     table               type    possible_keys                   key                     key_len     ref                                                     rows    Extra
1   SIMPLE      mytable_housetype_id    ref     IBLOCK_PROPERTY_ID              IBLOCK_PROPERTY_ID              4   const                                                   4   Using where; Using temporary
1   SIMPLE      mytable_planing         eq_ref  PRIMARY                         PRIMARY                         4   tesserbd.mytable_housetype_id.IBLOCK_ELEMENT_ID         1    
1   SIMPLE      mytable_housetype       ref     XML_HASH                        XML_HASH                        96  tesserbd.mytable_housetype_id.VALUE_HASH                1   Using index
1   SIMPLE      mytable_planing_id      ref     VALUE_HASH                      VALUE_HASH                      96  tesserbd.mytable_planing.XML_HASH                       15  Using where
1   SIMPLE      mytable_archive         ref     ix_iblock_element_property_1    ix_iblock_element_property_1    8   tesserbd.mytable_planing_id.IBLOCK_ELEMENT_ID,const     1   Using where
1   SIMPLE      mytable_street_id       ref     PROP_60_HASH                    PROP_60_HASH                    96  tesserbd.mytable_housetype_id.VALUE_HASH                185 Using where
1   SIMPLE      mytable                 eq_ref  PRIMARY                         PRIMARY                         4   tesserbd.mytable_planing_id.IBLOCK_ELEMENT_ID           1   Using where
1   SIMPLE      mytable_street          eq_ref  PRIMARY                         PRIMARY                         4   tesserbd.mytable_street_id.IBLOCK_ELEMENT_ID            1    
1   SIMPLE      mytable_city            eq_ref  PRIMARY                         PRIMARY                         4   tesserbd.mytable_street.IBLOCK_SECTION_ID               1   Using where

第二部分有不同的选择和某些领域的位置

SELECT SQL_NO_CACHE
    DISTINCT `mytable_planing`.`NAME` AS `FILTER_CITY`
FROM `b_iblock_element` `mytable` 
    LEFT JOIN `b_iblock_element_property` `mytable_planing_id` 
        ON `mytable`.`ID` = `mytable_planing_id`.`IBLOCK_ELEMENT_ID`
        AND `mytable_planing_id`.`IBLOCK_PROPERTY_ID` = 70
    INNER JOIN `b_iblock_element` `mytable_planing` 
        ON `mytable_planing_id`.`VALUE_HASH` = `mytable_planing`.`XML_HASH`
    LEFT JOIN `b_iblock_element_property` `mytable_housetype_id` 
        ON `mytable_planing`.`ID` = `mytable_housetype_id`.`IBLOCK_ELEMENT_ID`
        AND `mytable_housetype_id`.`IBLOCK_PROPERTY_ID` = 46
    INNER JOIN `b_iblock_element` `mytable_housetype` 
        ON `mytable_housetype_id`.`VALUE_HASH` = `mytable_housetype`.`XML_HASH`
    LEFT JOIN `b_iblock_element_prop_s4` `mytable_street_id` 
        ON `mytable_street_id`.`PROP_60_HASH` = `mytable_housetype`.`XML_HASH`
    INNER JOIN `b_iblock_element` `mytable_street` 
        ON `mytable_street_id`.`IBLOCK_ELEMENT_ID` = `mytable_street`.`ID`
    LEFT JOIN `b_iblock_section` `mytable_city` 
        ON `mytable_street`.`IBLOCK_SECTION_ID` = `mytable_city`.`ID`
    INNER JOIN `b_iblock_element_property` `mytable_archive` 
        ON `mytable`.`ID` = `mytable_archive`.`IBLOCK_ELEMENT_ID`
        AND `mytable_archive`.`IBLOCK_PROPERTY_ID` = 321
    WHERE `mytable`.`IBLOCK_ID` = 7
        AND (`mytable_archive`.`VALUE_NUM` IS NULL OR `mytable_archive`.`VALUE_NUM` = 0)
        AND (`mytable_planing`.`NAME` IS NOT NULL AND LENGTH(`mytable_planing`.`NAME`) > 0)

时间:0.00206秒

EXPLAIN

id  select_type     table               type    possible_keys                   key                     key_len     ref                                                     rows    Extra
1   SIMPLE      mytable_housetype_id    ref     IBLOCK_PROPERTY_ID              IBLOCK_PROPERTY_ID              4   const                                                   4   Using where; Using temporary
1   SIMPLE      mytable_planing         eq_ref  PRIMARY                         PRIMARY                         4   tesserbd.mytable_housetype_id.IBLOCK_ELEMENT_ID         1   Using where
1   SIMPLE      mytable_housetype       ref     XML_HASH                        XML_HASH                        96  tesserbd.mytable_housetype_id.VALUE_HASH                1   Using index; Distinct
1   SIMPLE      mytable_planing_id      ref     VALUE_HASH                      VALUE_HASH                      96  tesserbd.mytable_planing.XML_HASH                       15  Using where; Distinct
1   SIMPLE      mytable                 eq_ref  PRIMARY,ix_iblock_element_1     PRIMARY                         4   tesserbd.mytable_planing_id.IBLOCK_ELEMENT_ID           1   Using where; Distinct
1   SIMPLE      mytable_archive         ref     ix_iblock_element_property_1    ix_iblock_element_property_1    8   tesserbd.mytable_planing_id.IBLOCK_ELEMENT_ID,const     1   Using where; Distinct
1   SIMPLE      mytable_street_id       ref     PROP_60_HASH                    PROP_60_HASH                    96  tesserbd.mytable_housetype_id.VALUE_HASH                185 Using where; Distinct
1   SIMPLE      mytable_street          eq_ref  PRIMARY                         PRIMARY                         4   tesserbd.mytable_street_id.IBLOCK_ELEMENT_ID            1   Distinct
1   SIMPLE      mytable_city            eq_ref  PRIMARY                         PRIMARY                         4   tesserbd.mytable_street.IBLOCK_SECTION_ID               1   Using index; Distinct

仅在额外的情况下有所不同 - 首先没有明显的

为什么呢?

我尝试在不同的字段和字段组合上添加索引 - 但没有给出结果。

我将不胜感激任何帮助

2 个答案:

答案 0 :(得分:0)

在第二个解释计划显示

Here’s an example: this abbreviation

#page>div.logo+ul#navigation>li*5>a{Item $}
...can be transformed into

<div id="page">
    <div class="logo"></div>
    <ul id="navigation">
        <li><a href="">Item 1</a></li>
        <li><a href="">Item 2</a></li>
        <li><a href="">Item 3</a></li>
        <li><a href="">Item 4</a></li>
        <li><a href="">Item 5</a></li>
    </ul>
</div>

...with just a single key stroke. In many editors (such as Eclipse, Sublime Text 2, Espresso etc.) plugins will also generate proper tabstop marks so you can quickly traverse between important places of generated code with the Tab key.

在第一个

1   SIMPLE mytable_city  ........  Using index; Distinct

这意味着在第二个你访问一些列存储(也)索引..在第一个不... .. / / p>

使用和索引与否对性能/执行时间非常重要

首先选择

  

1 SIMPLE mytable_city Using where

mytable_city

在你选择的第二个

  

`mytable_city`.`NAME` AS `FILTER_CITY`

mytable_planingmytable_planing AS NAME

您为不同的表选择日期..

答案 1 :(得分:0)

我找到了解决方案。只需添加几个条件。全部