首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >mySQL全文索引减慢查询速度

mySQL全文索引减慢查询速度
EN

Stack Overflow用户
提问于 2018-03-20 13:08:39
回答 2查看 414关注 0票数 0

我有一个有两个列的表:"users“有一个全文索引,"x”是一个简单的int列。该表包含的条目略少于200万项。使用match...against选择包含特定用户的行会很快返回。

通过x的值进行搜索(没有索引)在大约3秒内返回。

但是,当我将这两个查询组合在一起时,查询时间为9秒!如果有的话,我希望合并的查询所花费的时间要少得多,因为全文索引会将可能的行减少一个数量级。甚至忘记全文索引和使用like "%___%"也更快!

这里发生了什么事?我怎么才能修好它?

mySQL输出包括以下内容:

代码语言:javascript
复制
mysql> desc testing;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| users | varchar(120) | YES  | MUL | NULL    |       |
| x     | int(11)      | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> select count(*) from testing;
+----------+
| count(*) |
+----------+
|  1924272 |
+----------+
1 row in set (3.56 sec)

mysql> select count(*) from testing where match(users) against("shy");
+----------+
| count(*) |
+----------+
|   149019 |
+----------+
1 row in set (0.42 sec)

mysql> select count(*) from testing where x>0;
+----------+
| count(*) |
+----------+
|  1924272 |
+----------+
1 row in set (3.62 sec)

mysql> select count(*) from testing where match(users) against("shy") and x>0;
+----------+
| count(*) |
+----------+
|   149019 |
+----------+
1 row in set (8.82 sec)

mysql> select count(*) from testing where users like "%shy%" and x>0;
+----------+
| count(*) |
+----------+
|   149019 |
+----------+
1 row in set (3.57 sec)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-03-20 13:38:24

始终检查innodb_buffer_pool_size的值,并根据系统的功能和软件需求对其进行调整。这意味着不要给MySQL比您拥有的内存更多的内存:)

如果索引不适合内存,MySQL将从磁盘上读取它,从而限制了硬盘的速度。如果你在使用SSD,这可能是好的,但在机械驱动器它是缓慢的蜗牛。

如果索引不能放入RAM中,它们就没有那么有用。

票数 1
EN

Stack Overflow用户

发布于 2018-03-20 13:12:22

考虑使用子查询。

代码语言:javascript
复制
select count(*) from (

    select * 

    from testing 

    where match(users) against("shy")

) shy_results 

where x>0;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49385095

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档