首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类似于中型表(~3m记录)的查询性能

类似于中型表(~3m记录)的查询性能
EN

Stack Overflow用户
提问于 2016-06-28 07:52:06
回答 1查看 67关注 0票数 0

我有个小问题。我有一个大约300万座城市的表,我需要对它运行一个like查询。

问题是,大约需要9s才能完成查询。知道我怎么能很快做到这一点吗?

查询是:

代码语言:javascript
复制
SELECT * FROM `cities` WHERE name LIKE '%test%'
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-28 08:47:37

尝试使用全文索引。InnoDB现在也有全文索引。

代码语言:javascript
复制
CREATE FULLTEXT INDEX idx_name_ft ON cities  (`name`);

然后像这样使用:

代码语言:javascript
复制
SELECT * FROM cities WHERE MATCH(`name`) AGAINST('test');

样本

我创建了一个包含5000000行的简单示例表,这是我的结果:

代码语言:javascript
复制
MariaDB [yourschema]> SELECT * FROM cities WHERE `name` LIKE '%test%';
+---------+------------------------------------------------------+
| id      | name                                                 |
+---------+------------------------------------------------------+
|       7 | name of the city is test more text here              |
|    1096 | name of the city is other more text here - test      |
|    1109 | test name of the city is other more text here        |
| 4998932 | name of the city is other more text here - last test |
| 4999699 | name of the city is other more text here - test some |
| 4999997 | name of the city is - test again - more text here    |
+---------+------------------------------------------------------+
6 rows in set (4.29 sec)

MariaDB [yourschema]> SELECT * FROM cities WHERE MATCH(`name`) AGAINST('test');
+---------+------------------------------------------------------+
| id      | name                                                 |
+---------+------------------------------------------------------+
|       7 | name of the city is test more text here              |
|    1096 | name of the city is other more text here - test      |
|    1109 | test name of the city is other more text here        |
| 4998932 | name of the city is other more text here - last test |
| 4999699 | name of the city is other more text here - test some |
| 4999997 | name of the city is - test again - more text here    |
+---------+------------------------------------------------------+
6 rows in set (0.60 sec)

MariaDB [yourschema]>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38070249

复制
相关文章

相似问题

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