mysql select count()比全文/匹配搜索中的select慢1000倍

o4tp2gmn  于 2021-06-20  发布在  Mysql
关注(0)|答案(0)|浏览(223)

上下文:这是一个b2c项目的mysql数据库。
我使用select count()语句预先知道常规select将返回的行数。这只是用于gui中的分页计算。我遇到的问题是,在我的笔记本电脑本地设置中,select count()查询持续时间的危险顺序是1s,而select query的危险顺序是1ms,大约慢1000倍!!
我已经隔离了这个问题,并创建了一个单独的项目,目的是为了拥有一个简单的测试平台。表文档是这样创建的:

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个单词,brief为12个单词,单词的内容约为50kbyte。
典型的用户查询类似于:

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-3毫秒之间,对我来说已经足够了。考虑到全文/匹配搜索对用户的价值,我准备了更长的时间。count()查询最多会上升到1s,这是一个很长的时间,即使在低并发的情况下,也会导致较高的风险。
你知道如何减少select count()的持续时间吗?
mysql版本为:10.1.10-mariadb,文档表为innodb。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题