Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.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 - Fatal编程技术网

Mysql 为什么';不使用索引吗?

Mysql 为什么';不使用索引吗?,mysql,Mysql,我有一个简单的问题: SELECT id FROM logo WHERE (`description` LIKE "%google%" AND `active` = "y") ORDER BY id ASC 表logo(MyISAM)由大约30个字段组成,其中包含2402024行。 字段description是一个varchar(255),不为空。 字段active是一个不为空的枚举('y','n') 这些索引的基数为: `active`: BTREE Card. 2 `descripti

我有一个简单的问题:

SELECT id
FROM logo
WHERE (`description` LIKE "%google%" AND `active` = "y")
ORDER BY id ASC 
logo
(MyISAM)由大约30个字段组成,其中包含2402024行。 字段
description
是一个varchar(255),不为空。 字段
active
是一个不为空的枚举('y','n') 这些索引的基数为:

`active`: BTREE Card. 2 
`description`: BTREE Card. 200168 
解释查询返回的结果:

select_type: SIMPLE 
table: logo 
type: ALL 
possible_keys: active 
key: NULL 
key_len: NULL 
ref: NULL 
rows: 2402024 
Extras: Using where 
我想知道为什么查询没有使用
description
索引,以及如何优化表以便在不进行完整表扫描的情况下顺利运行此查询


已经优化了表并检查了错误。

如果您的
LIKE
条件在要比较的表达式的开头和结尾处有一个
%
,则索引在此上下文中是无用的


只有在比较表达式可以“有序”找到的情况下,索引才有用,如A在B之前,B在C之前。换句话说,
如“google%”可以使用索引,而
如“%google%”
则不会使用索引。

sql引擎只能使用“以开头”的索引,因此“google%”将使用索引。在字段中查找子字符串会强制引擎进行全表扫描。无论如何,我可以修改表以加快查询速度吗?也许是分区?你最好用全文搜索之类的。。。