单表覆盖索引的mysql查询优化策略

时间:2015-07-06 11:17:43

标签: mysql

目前我正在进行mysql查询优化。我的mysql表包含2亿条记录。 在做了很多谷歌后我终于决定使用覆盖索引。

所以,我按此顺序制作了一个索引

alter table table_name add index
     index_name(MODEL, COUNTRY, REGION, NETWORK, E_TUAL,
                ECHS, DEVID, COUNTRY_CODE, SOURCE);

当我运行此查询时,效率与之前的

相比是好的
SELECT E_TUAL,ECHS, DEVID, MODEL, COUNTRY, REGION, COUNTRY_CODE, NETWORK, SOURCE
FROM table_name
WHERE model = 'fox | s453' AND country = 'india' AND
      E_TUAL <= '1435755600' AND
      E_TUAL >= '1433163600'

按E_TUAL DESC排序    限制101 OFFSET 0;

+----+-------------+-----------+------+---------------+--------+---------+-------------+------+-----------------------------+
| id | select_type | table     | type | possible_keys | key    | key_len | ref         | rows | Extra                       |
+----+-------------+-----------+------+---------------+--------+---------+-------------+------+-----------------------------+
|  1 | SIMPLE      | mytable | ref  | genrl  | genrl | 131     | const |  239 | Using where; Using filesort |
+----+-------------+-----------+------+---------------+--------+---------+-------------+------+-----------------------------+

但是这个查询与旧版本(索引之前)相比最差

SELECT E_TUAL,ECHS, DEVID, MODEL,COUNTRY, REGION, COUNTRY_CODE, NETWORK, SOURCE
FROM table_name
WHERE model = 'fox | s453' AND
      country is not null AND
      E_TUAL <= '1435755600' AND
      E_TUAL >= '1433163600'
ORDER BY E_TUAL DESC
LIMIT 101 OFFSET 0;

+----+-------------+-----------+-------+---------------+--------+---------+------+------+-----------------------------+
| id | select_type | table     | type  | possible_keys | key    | key_len | ref  | rows | Extra                       |
+----+-------------+-----------+-------+---------------+--------+---------+------+------+-----------------------------+
|  1 | SIMPLE      | mytable | range | genrl  | genrl | 131     | NULL | 1105 | Using where; Using filesort |
+----+-------------+-----------+-------+---------------+--------+---------+------+------+-----------------------------+

我写国家不是null维护顺序所以mysql优化器使用索引

plz有助于在缺少某些列字段时提高效率 使用索引我该怎么办?

我的英语不好。所以对于任何错误我很抱歉

1 个答案:

答案 0 :(得分:0)

使用覆盖索引进行分配并使用两个单独的索引:

alter table table_name add index index_name(MODEL, COUNTRY, E_TUAL);

第一个查询。和

alter table table_name add index index_name(MODEL, E_TUAL);

覆盖指数可能会带来微小的改善,但会占用大量空间。相反,请关注WHERE子句,然后关注ORDER BY,如果您可以将其用于索引。

注意:对于任一查询,您对列的排序都不是最佳的。