1

字符串搜索查询(MySQL 5.5)存在性能问题。主表包含几个文本列。表 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 中已经有大约几十万条记录,而 multi 中的记录数量也差不多,查询需要几秒钟才能完成。

这是解释:

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
4

0 回答 0