我试图通过在代理列上设置索引来优化mysql查询,但我的索引似乎不起作用。下面是我的表格:
CREATE TABLE `Test` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`country_id` INT(11) NULL DEFAULT NULL,
`agent` TEXT NOT NULL COLLATE 'latin1_general_ci',
PRIMARY KEY (`id`)
)
COLLATE='latin1_general_ci'
ENGINE=InnoDB
alter table test add index index_for_agent (agent(767));
explain select * from test cu WHERE cu.agent REGEXP 'bot|Spider|SiteExpl|crawler'

如何优化我的查询
发布于 2014-03-15 21:44:55
如果agent接受一个值并且只有一个值,则使用in
select *
from test cu
WHERE cu.agent in ('bot', 'Spider', 'SiteExpl', 'crawler')那么在test(agent)上建立索引将是非常有效的。
如果agent接受多个值,那么更改数据结构,这样就有了另一个表,比如test_agent,每个值占一行。然后上面的方法就会起作用了。
如果智能体有前导匹配,那么"bot“应该匹配”无底深坑“,在like和union all中使用多条语句
select *
from test cu
where cu.agent like 'bot%'
union all
select *
from test cu
where cu.agent like 'spider%'
. . .以及场地上的索引。
如果要查找全文,请使用全文索引。请注意,您将需要更改最小单词长度参数,因为默认值为4,它不会索引"bot“。
如果您需要在字段中搜索随机字符串,那么我已经没有提高性能的想法了。
https://stackoverflow.com/questions/22422590
复制相似问题