耗时查询:
mysql> select * from channel_details where channel like "%" and type=29 and `year`>=2017 limit 10;
+---------+------+-------+---------------+-------------+---------+------+-----+---------------------+
| id | year | month | point_of_time | channel | ratings | type | age | time_at |
+---------+------+-------+---------------+-------------+---------+------+-----+---------------------+
| 7124305 | 2017 | 1 | 02:00 | ??????????? | 0 | 29 | 40 | 2017-02-15 15:03:54 |
| 7124306 | 2017 | 1 | 02:01 | ??????????? | 0 | 29 | 40 | 2017-02-15 15:03:54 |
| 7124307 | 2017 | 1 | 02:02 | ??????????? | 0 | 29 | 40 | 2017-02-15 15:03:54 |
| 7124308 | 2017 | 1 | 02:03 | ??????????? | 0 | 29 | 40 | 2017-02-15 15:03:54 |
| 7124309 | 2017 | 1 | 02:04 | ??????????? | 0 | 29 | 40 | 2017-02-15 15:03:54 |
| 7124310 | 2017 | 1 | 02:05 | ??????????? | 0 | 29 | 40 | 2017-02-15 15:03:54 |
| 7124311 | 2017 | 1 | 02:06 | ??????????? | 0 | 29 | 40 | 2017-02-15 15:03:54 |
| 7124312 | 2017 | 1 | 02:07 | ??????????? | 0 | 29 | 40 | 2017-02-15 15:03:54 |
| 7124313 | 2017 | 1 | 02:08 | ??????????? | 0.001 | 29 | 40 | 2017-02-15 15:03:54 |
| 7124314 | 2017 | 1 | 02:09 | ??????????? | 0.001 | 29 | 40 | 2017-02-15 15:03:54 |
+---------+------+-------+---------------+-------------+---------+------+-----+---------------------+
10 rows in set (16.10 sec)
mysql> explain select * from channel_details where channel like "%" and type=29 and `year`>=2017 limit 10;
+----+-------------+-----------------+------------+------+---------------+------+---------+-------+---------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-----------------+------------+------+---------------+------+---------+-------+---------+----------+-------------+
| 1 | SIMPLE | channel_details | NULL | ref | year,type | type | 4 | const | 4969150 | 5.56 | Using where |
+----+-------------+-----------------+------------+------+---------------+------+---------+-------+---------+----------+-------------+
1 row in set, 1 warning (0.01 sec)
不耗时查询:
mysql> explain select * from channel_details where channel like "%" and type=29 limit 10;
+----+-------------+-----------------+------------+------+---------------+------+---------+-------+---------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-----------------+------------+------+---------------+------+---------+-------+---------+----------+-------------+
| 1 | SIMPLE | channel_details | NULL | ref | type | type | 4 | const | 4969150 | 11.11 | Using where |
+----+-------------+-----------------+------------+------+---------------+------+---------+-------+---------+----------+-------------+
1 row in set, 1 warning (0.09 sec)
mysql> select * from channel_details where channel like "%" and type=29 limit 10;
+----+------+-------+---------------+-------------+---------+------+-----+---------------------+
| id | year | month | point_of_time | channel | ratings | type | age | time_at |
+----+------+-------+---------------+-------------+---------+------+-----+---------------------+
| 1 | 2016 | 9 | 02:00 | ??????????? | 0 | 29 | 40 | 2016-12-22 22:59:24 |
| 2 | 2016 | 9 | 02:01 | ??????????? | 0 | 29 | 40 | 2016-12-22 22:59:24 |
| 3 | 2016 | 9 | 02:02 | ??????????? | 0 | 29 | 40 | 2016-12-22 22:59:24 |
| 4 | 2016 | 9 | 02:03 | ??????????? | 0 | 29 | 40 | 2016-12-22 22:59:24 |
| 5 | 2016 | 9 | 02:04 | ??????????? | 0 | 29 | 40 | 2016-12-22 22:59:24 |
| 6 | 2016 | 9 | 02:05 | ??????????? | 0 | 29 | 40 | 2016-12-22 22:59:24 |
| 7 | 2016 | 9 | 02:06 | ??????????? | 0 | 29 | 40 | 2016-12-22 22:59:24 |
| 8 | 2016 | 9 | 02:07 | ??????????? | 0 | 29 | 40 | 2016-12-22 22:59:24 |
| 9 | 2016 | 9 | 02:08 | ??????????? | 0 | 29 | 40 | 2016-12-22 22:59:24 |
| 10 | 2016 | 9 | 02:09 | ??????????? | 0 | 29 | 40 | 2016-12-22 22:59:24 |
+----+------+-------+---------------+-------------+---------+------+-----+---------------------+
10 rows in set (0.11 sec)
明明都是走了type索引,为什么时间差别这么大?