为什么mysql查询执行计划在添加新列后发生更改?

gojuced7  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(389)

我有一个表格和一些数据如下:

-- MySQL dump 10.13  Distrib 5.7.17, for macos10.12 (x86_64)
--
-- Host: localhost    Database: testmysql
-- ------------------------------------------------------
-- Server version   5.7.17-log

--
-- Table structure for table `t_strange_index`
--

DROP TABLE IF EXISTS `t_strange_index`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;

CREATE TABLE `t_strange_index` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `status` tinyint(4) NOT NULL,
  `create_time` bigint(20) NOT NULL,
  PRIMARY KEY (`id`) USING BTREE,
  KEY `idx_time_status` (`create_time`,`status`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=21
       DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=COMPACT;

/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `t_strange_index`
--

LOCK TABLES `t_strange_index` WRITE;
/*!40000 ALTER TABLE `t_strange_index` DISABLE KEYS */;
INSERT INTO `t_strange_index` VALUES
        (1,1,1532745820825),(2,1,1532745864183),(3,1,1532745895207),
        (4,1,1532746773225),(5,1,1532746773225),(6,1,1532746773225),
        (7,1,1532746822078),(8,1,1532746822078),(9,1,1532746822078),
        (10,1,1532746979836),(11,1,1532746979836),(12,1,1532746979836),
        (13,9,1532763766641),(14,10,1532764510924),(15,10,1532765436500),
        (16,20,1532777350303),(17,9,1532777818806),(18,10,1532782628840),
        (19,10,1532782711973),(20,10,1532784164740);
/*!40000 ALTER TABLE `t_strange_index` ENABLE KEYS */;
UNLOCK TABLES;

然后获取此sql的查询执行计划:

explain select * from t_strange_index
      where create_time >= 1532746822078
        and create_time <= 1532746979836
        and status = 1;

结果:

id | select_type | table           | partitions | type  | possible_keys   | key             | key_len | ref  | rows | filtered | Extra                    
  1 | SIMPLE      | t_strange_index | NULL       | range | idx_time_status | idx_time_status | 9       | NULL |    6 |    10.00 | Using where; Using index

但在我添加一个新列之后,

alter table t_strange_index add column `new column` bigint(20) NOT NULL default 123;

执行计划变更:

id | select_type | table           | partitions | type | possible_keys   | key  | key_len | ref  | rows | filtered | Extra       
  1 | SIMPLE      | t_strange_index | NULL       | ALL  | idx_time_status | NULL | NULL    | NULL |   20 |     5.00 | Using where

它不再使用索引了。
有人能告诉我为什么会这样吗?谢谢。

ssm49v7z

ssm49v7z1#

覆盖指数
你应该注意你原来的解释额外有使用索引。这是覆盖指数的指标。
覆盖索引是包含查询所需的所有列的索引。
添加新列时,idx\u time\u状态不再是覆盖索引(因为您选择*而新列不在索引中),mysql必须返回原始数据。这就是为什么mysql认为不使用索引更有效。

igetnqfo

igetnqfo2#

“覆盖指数”只是故事的一部分。
这是向后的:

KEY `idx_time_status` (`create_time`,`status`) USING BTREE

开始索引 = 测试,然后以范围结束。也就是说,此索引更适合您的查询:

INDEX(status, create_time)

这样就不必用 status != 1 ; 索引会过滤掉它们。
但这并不能解释为什么要避开索引而切换到表扫描。解释这一点的原因是,范围可能超过了表的20%。优化器查看统计信息,并讨论使用索引(当只需要几行时)还是忽略索引并简单地扫描表(当需要很多行时)会更快。
如果您的测试用例在给定的范围内有更多的行,那么它可能具有不同的 EXPLAIN .
查看我的索引生成器食谱

相关问题