mysql在执行计划中显示索引,但实际上没有使用它

h43kikqp  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(484)

我有一个有2000万行的表和一个需要10秒的查询。

select id from entity 
where (entity.class_id = 67 and entity.name like '%321%' )
order by id desc

在执行计划中有一个索引,但它并没有真正被使用。

explain extended select id from entity 
where (entity.class_id = 67 and entity.name like '%321%' ) 
order by id desc

| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
|  1 | SIMPLE | entity | ref | constraint_unique_class_legacy_id,entity_tag,entity_class_modification_date_int_idx,entity_name_idx | entity_class_modification_date_int_idx | 8 | const | 288440 | 100.00 | Using where; Using filesort |

如果刷新状态并运行此查询,则处理程序将显示已完成扫描

Handler_read_next: 20318800

但如果我给出一个使用“explain extended”中的索引的提示,那么就没有完整的扫描,查询也不会在250ms内完成。

select id from entity 
use index (entity_class_modification_date_int_idx) 
where (entity.class_id = 67 and entity.name like '%321%' ) 
order by id desc

只扫描了166k个实体

Handler_read_next: 165894

为什么我要提示使用已经在执行计划中的索引?
如果我向order by添加+0,查询也会在250ms内完成。

select id from entity 
where (entity.class_id = 67 and entity.name like '%321%' ) 
order by id + 0 desc

“explain extended”在每种情况下都显示相同的执行计划,“analyze”没有帮助。
“实体”表:

CREATE TABLE `entity` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(4096) COLLATE utf8_bin DEFAULT NULL,
`tag` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`revision` int(11) NOT NULL,
`class_id` bigint(20) NOT NULL,
`legacy_id` bigint(20) DEFAULT NULL,
`description` varchar(255) COLLATE utf8_bin DEFAULT NULL,
`last_modified_by` varchar(64) COLLATE utf8_bin DEFAULT NULL,
`removed` tinyint(1) NOT NULL DEFAULT '0',
`modification_date_int` bigint(20) DEFAULT NULL,
`creation_date_int` bigint(20) DEFAULT NULL,
`created_by` varchar(64) COLLATE utf8_bin DEFAULT NULL,
`ancestor_class_id` bigint(20) NOT NULL,
`acu_id` bigint(20) DEFAULT NULL,
`secured` tinyint(1) DEFAULT '1',
`system_modification_date` bigint(20) DEFAULT NULL,
`archived` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `constraint_unique_class_legacy_id` (`class_id`,`legacy_id`),
UNIQUE KEY `entity_tag` (`class_id`,`tag`),
UNIQUE KEY `class_hierarchy_tag` (`tag`,`ancestor_class_id`),
KEY `entity_legacy_id_idx` (`legacy_id`),
KEY `entity_modification_date_int_idx` (`modification_date_int`),
KEY `entity_class_modification_date_int_idx` (`class_id`,`removed`,`modification_date_int`),
KEY `ancestor_class_id` (`ancestor_class_id`),
KEY `acu_id` (`acu_id`),
KEY `entity_name_idx` (`class_id`,`name`(255)),
KEY `entity_archived_idx` (`archived`),
CONSTRAINT `entity_ibfk_1` FOREIGN KEY (`class_id`) REFERENCES `class` (`id`),
CONSTRAINT `entity_ibfk_2` FOREIGN KEY (`ancestor_class_id`) REFERENCES `class` (`id`),
CONSTRAINT `entity_ibfk_3` FOREIGN KEY (`acu_id`) REFERENCES `acu` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=60382455 DEFAULT CHARSET=utf8 COLLATE=utf8_bin

mysql版本:

SELECT @@version;
+--------------------+
| @@version          |
+--------------------+
| 5.6.30-76.3-56-log |
+--------------------+
brgchamk

brgchamk1#

好吧,我找到了一个部分答案:我正在使用的mysql workbench隐式地向查询添加了limit1000,它极大地降低了性能,尽管响应中的行要少得多。“explain extended”将primary显示为键,这不再是一个问题。如果我将limit增加到10000,那么查询将在250ms内完成。看起来mysql优化器中有一些启发式方法,在低“limit”的情况下强制它使用主索引。

相关问题