字符串搜索查询(MySQL 5.5)存在性能问题。表main包含一些文本列。表multi包含每个主条目的零个或多个多值字段。我们想要计算有多个主要项目,其中一个字段以特定字符串开头。
以下是查询:
select count(main_id) from main ma
where s1 like '888%' or s2 like '888%' or s3 like '888%'
or exists (select m.multivalue
from multi m
where ma.main_id=m.main_id and (m.multivalue like '888%'))
在main中大约有几十万条记录,并且多条记录的记录大致相同,查询需要几秒钟才能完成。
以下是解释:
PRIMARY ma ALL s1,s2,s3 100407 100.00 Using where
DEPENDENT SUBQUERY m ref PRIMARY,main_id,multivalue PRIMARY 8 sample.ma.main_id 1 100.00 Using where; Using index
表格定义:
CREATE TABLE `main` (
`main_id` bigint(20) NOT NULL AUTO_INCREMENT,
`s1` varchar(50) CHARACTER SET utf8 NOT NULL,
`s2` varchar(50) CHARACTER SET utf8 DEFAULT NULL,
`s3` varchar(50) CHARACTER SET utf8 DEFAULT NULL,
PRIMARY KEY (`main_id`),
KEY `s1` (`s1`),
KEY `s2` (`s2`),
KEY `s3` (`s3`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
CREATE TABLE `multi` (
`main_id` bigint(20) NOT NULL,
`multivalue` varchar(50) CHARACTER SET utf8 NOT NULL,
PRIMARY KEY (`main_id`,`multivalue`),
KEY `main_id` (`main_id`),
KEY `multivalue` (`multivalue`),
CONSTRAINT `FK_multi_main` FOREIGN KEY (`main_id`) REFERENCES `main` (`main_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
如何更快地进行此查询?
innodb设置:
innodb_additional_mem_pool_size=32M
innodb_flush_log_at_trx_commit=1
innodb_log_buffer_size=64M
innodb_buffer_pool_size=1000M
innodb_log_file_size=128M
innodb_thread_concurrency=16
innodb_file_per_table
innodb_file_format=Barracuda