Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL在执行计划中显示索引,但不显示';我真的不用它_Mysql_Innodb - Fatal编程技术网

MySQL在执行计划中显示索引,但不显示';我真的不用它

MySQL在执行计划中显示索引,但不显示';我真的不用它,mysql,innodb,Mysql,Innodb,我有一个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%' ) ord

我有一个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
为什么我必须给出提示以使用已在执行计划中的索引

如果我将+0添加到order by,查询也将在250ms内完成

select id from entity 
where (entity.class_id = 67 and entity.name like '%321%' ) 
order by id + 0 desc
“解释扩展”在所有情况下都显示相同的执行计划,“分析”没有帮助

表“实体”:

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 |
+--------------------+

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

强制索引
。参考:没有人声称乐观主义者是绝对正确的。谢谢,但为什么我必须强制执行已经在执行计划中的索引?我希望MySQL在没有任何提示的情况下使用它。请添加
SHOW CREATE TABLE entity\G
的输出,并具体说明MySQL的版本:
SELECT@@versionentity\u class\u modification\u date\u int\u idx
,代码>很难帮助我们。