上下文:这是B2C项目的MySQL数据库。
我正在使用SELECT COUNT()语句来提前知道常规SELECT将返回的行数。这仅用于GUI中的分页计算。我遇到的问题是,仅在我的笔记本电脑本地设置中,SELECT COUNT()查询的持续时间相对于SELECT查询的危险时间约为1s,而SELECT查询的持续时间约为1ms。慢了大约1000倍!
我已经隔离了问题,并创建了一个单独的项目,目的是拥有一个简单的测试台。表 document 是通过以下方式创建的:
drop table document;
create table document (
id int primary key auto_increment,
brand varchar(128) not null,
ref varchar(32) not null,
title varchar(128) not null,
brief varchar(256) not null,
content mediumtext not null,
fulltext key (brand, ref, title, brief, content)
);
要建立一个实际问题的模型,我在表格中填充了2000个随机文本行,其中包含 brand 和 ref 1个单词, title 6个单词,简要 12个单词,内容大约50kbytes。
典型的用户查询如下:
select id, brand, ref, title, brief, content from document
where match (brand, ref, title, brief, content) against ('espadas' in boolean mode)
limit 24
因此COUNT()查询应为:
select count(id)
from document
where match (brand, ref, title, brief, content) against ('espadas' in boolean mode)
使用我的笔记本电脑中的本地设置,常规查询的经验持续时间在1-3ms之间,对我来说足够好了。考虑到FULLTEXT / MATCH搜索对于用户的价值,我准备了更长的时间。 Count()查询上升到1s,这很长的时间会导致较高的风险,即使在低并发情况下也是如此。
有什么想法可以减少SELECT COUNT()这么长的时间吗?
MySQL版本为:10.1.10-MariaDB,文档表为InnoDB。