MYSQL EXPLAIN解析一 EXTRA中的USING INDEX,USING INDEX CONDITION

using index :使用覆盖索引的时候就会出现

using where:在查找使用索引的情况下,需要回表去查询所需的数据

using index condition:查找使用了索引,但是需要回表查询数据

using index & using where:查找使用了索引,但是需要的数据都在索引列中能找到,所以不需要回表查询数据

 

查慢查询记录

# Time: 181020 11:53:11
# User@Host: zabbix[zabbix] @ localhost []
# Thread_id: 117743  Schema: zabbix  QC_hit: No
# Query_time: 2.857652  Lock_time: 0.000108  Rows_sent: 576  Rows_examined: 1728

SELECT itemid,COUNT(*) AS count,AVG(value) AS avg,MIN(value) AS min,MAX(value) AS max,round(1786* MOD(CAST(clock AS UNSIGNED)+158820,172800)/(172800),0) AS i,MAX(clock) AS clock FROM history_uint WHERE itemid='59886' AND clock>='1539834780' AND clock<='1540007580' GROUP BY itemid,round(1786* MOD(CAST(clock AS UNSIGNED)+158820,172800)/(172800),0);

对zabbix的history_uint表进行优化,优化前结果是

优化后是

MariaDB [zabbix]> explain SELECT itemid,COUNT(*) AS count,AVG(value) AS avg,MIN(value) AS min,MAX(value) AS max,round(1746* MOD(CAST(clock AS UNSIGNED)+67286,86400)/(86400),0) AS i,MAX(clock) AS clock FROM history_uint WHERE itemid='42794' AND clock>='1531199914' AND clock<='1531286314' GROUP BY itemid,round(1746* MOD(CAST(clock AS UNSIGNED)+67286,86400)/(86400),0);
+------+-------------+--------------+-------+----------------+----------------+---------+------+------+--------------------------------------------------------+
| id   | select_type | table        | type  | possible_keys  | key            | key_len | ref  | rows | Extra                                                  |
+------+-------------+--------------+-------+----------------+----------------+---------+------+------+--------------------------------------------------------+
|    1 | SIMPLE      | history_uint | range | history_uint_1 | history_uint_1 | 12      | NULL |  285 | Using index condition; Using temporary; Using filesort |
+------+-------------+--------------+-------+----------------+----------------+---------+------+------+--------------------------------------------------------+
1 row in set (0.00 sec)


MariaDB [zabbix]> explain SELECT itemid,COUNT(*) AS count,AVG(value) AS avg,MIN(value) AS min,MAX(value) AS max,round(1746* MOD(CAST(clock AS UNSIGNED)+67286,86400)/(86400),0) AS i,MAX(clock) AS clock FROM history_uint WHERE itemid='42794' AND clock>='1531199914' AND clock<='1531286314' GROUP BY itemid,round(1746* MOD(CAST(clock AS UNSIGNED)+67286,86400)/(86400),0)\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: history_uint
         type: range
possible_keys: history_uint_1
          key: history_uint_1
      key_len: 12
          ref: NULL
         rows: 285
        Extra: Using index condition; Using temporary; Using filesort
1 row in set (0.00 sec)


MariaDB [zabbix]> show index from history_uint;
+--------------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table        | Non_unique | Key_name       | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+--------------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| history_uint |          1 | history_uint_1 |            1 | itemid      | A         |          19 |     NULL | NULL   |      | BTREE      |         |               |
| history_uint |          1 | history_uint_1 |            2 | clock       | A         |    38955665 |     NULL | NULL   |      | BTREE      |         |               |
+--------------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.30 sec)


MariaDB [zabbix]> show columns from history_uint;
+--------+---------------------+------+-----+---------+-------+
| Field  | Type                | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+-------+
| itemid | bigint(20) unsigned | NO   | MUL | NULL    |       |
| clock  | int(11)             | NO   |     | 0       |       |
| value  | bigint(20) unsigned | NO   |     | 0       |       |
| ns     | int(11)             | NO   |     | 0       |       |
+--------+---------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)


MariaDB [zabbix]> select count(distinct itemid),count(distinct clock),count(distinct value) from history_uint;
+------------------------+-----------------------+-----------------------+
| count(distinct itemid) | count(distinct clock) | count(distinct value) |
+------------------------+-----------------------+-----------------------+
|                  10065 |                730331 |               1492694 |
+------------------------+-----------------------+-----------------------+
1 row in set (2 min 24.30 sec)


MariaDB [zabbix]> alter table history_uint add index idx_i_c_v(itemid,clock,value);


Query OK, 0 rows affected (6 min 27.53 sec)

Records: 0  Duplicates: 0  Warnings: 0

 

MariaDB [zabbix]> alter table history_uint drop index history_uint_1;
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0


MariaDB [zabbix]> SHOW INDEX FROM history_uint;
+--------------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table        | Non_unique | Key_name  | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+--------------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| history_uint |          1 | idx_i_c_v |            1 | itemid      | A         |          16 |     NULL | NULL   |      | BTREE      |         |               |
| history_uint |          1 | idx_i_c_v |            2 | clock       | A         |    33357351 |     NULL | NULL   |      | BTREE      |         |               |
| history_uint |          1 | idx_i_c_v |            3 | value       | A         |    33357351 |     NULL | NULL   |      | BTREE      |         |               |
+--------------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

3 rows in set (0.09 sec)

 

MariaDB [zabbix]> explain SELECT itemid,COUNT(*) AS count,AVG(value) AS avg,MIN(value) AS min,MAX(value) AS max,round(1746* MOD(CAST(clock AS UNSIGNED)+67286,86400)/(86400),0) AS i,MAX(clock) AS clock FROM history_uint WHERE itemid='42794' AND clock>='1531199914' AND clock<='1531286314' GROUP BY itemid,round(1746* MOD(CAST(clock AS UNSIGNED)+67286,86400)/(86400),0)\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: history_uint
         type: range
possible_keys: idx_i_c_v
          key: idx_i_c_v
      key_len: 12
          ref: NULL
         rows: 284
        Extra: Using where; Using index; Using temporary; Using filesort
1 row in set (0.00 sec)


MariaDB [zabbix]> explain SELECT itemid,COUNT(*) AS count,AVG(value) AS avg,MIN(value) AS min,MAX(value) AS max,round(1746* MOD(CAST(clock AS UNSIGNED)+67286,86400)/(86400),0) AS i,MAX(clock) AS clock FROM history_uint WHERE itemid='42794' AND clock>='1531199914' AND clock<='1531286314' GROUP BY itemid,round(1746* MOD(CAST(clock AS UNSIGNED)+67286,86400)/(86400),0);
+------+-------------+--------------+-------+---------------+-----------+---------+------+------+-----------------------------------------------------------+
| id   | select_type | table        | type  | possible_keys | key       | key_len | ref  | rows | Extra                                                     |
+------+-------------+--------------+-------+---------------+-----------+---------+------+------+-----------------------------------------------------------+
|    1 | SIMPLE      | history_uint | range | idx_i_c_v     | idx_i_c_v | 12      | NULL |  284 | Using where; Using index; Using temporary; Using filesort |
+------+-------------+--------------+-------+---------------+-----------+---------+------+------+-----------------------------------------------------------+

1 row in set (0.00 sec)

MariaDB [zabbix]> select SQL_NO_CACHE itemid,COUNT(*) AS count,AVG(value) AS avg,MIN(value) AS min,MAX(value) AS max,round(1786* MOD(CAST(clock AS UNSIGNED)+158820,172800)/(172800),0) AS i,MAX(clock) AS clock FROM history WHERE itemid='59920' AND clock>='1539834780' AND clock<='1540007580' GROUP BY itemid,round(1786* MOD(CAST(clock AS UNSIGNED)+158820,172800)/(172800),0);

1785 rows in set (0.03 sec)

由原来的接近三秒优化到0.03秒

 

# Query_time: 101.392177  Lock_time: 0.000073  Rows_sent: 1  Rows_examined: 46443695

SET timestamp=1540089186;

select count(distinct itemid),count(distinct clock),count(distinct value) from history_uint;

MariaDB [zabbix]> select count(distinct itemid),count(distinct clock),count(distinct value) from history_uint;

+------------------------+-----------------------+-----------------------+

| count(distinct itemid) | count(distinct clock) | count(distinct value) |

+------------------------+-----------------------+-----------------------+

|                  10141 |                970077 |               1697310 |

+------------------------+-----------------------+-----------------------+

1 row in set (1 min 41.39 sec)

MariaDB [zabbix]> explain select  count(distinct itemid),count(distinct clock),count(distinct value) from history_uint;

+------+-------------+--------------+-------+---------------+-----------+---------+------+----------+-------------+

| id   | select_type | table        | type  | possible_keys | key       | key_len | ref  | rows     | Extra       |

+------+-------------+--------------+-------+---------------+-----------+---------+------+----------+-------------+

|    1 | SIMPLE      | history_uint | index | NULL          | idx_i_c_v | 20      | NULL | 47769538 | Using index |

+------+-------------+--------------+-------+---------------+-----------+---------+------+----------+-------------+

1 row in set (0.00 sec)

MariaDB [zabbix]> select SQL_NO_CACHE  count(distinct itemid),count(distinct clock),count(distinct value) from history_uint;

+------------------------+-----------------------+-----------------------+

| count(distinct itemid) | count(distinct clock) | count(distinct value) |

+------------------------+-----------------------+-----------------------+

|                  10141 |                971915 |               1696308 |

+------------------------+-----------------------+-----------------------+

1 row in set (50.51 sec)

估索引前用了101秒,用了索引只要50秒

你可能感兴趣的:(数据库)