像中型表上的查询性能(~3m记录)

时间:2016-06-28 07:52:07

标签: mysql

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

问题是,完成查询大约需要9秒。我有什么想法可以让它变得非常快?

查询是:

SELECT * FROM `cities` WHERE name LIKE '%test%'

1 个答案:

答案 0 :(得分:2)

尝试使用 FULLTEXT INDEX 。 InnoDB现在也有FULLTEXT INDEX。

SELECT * FROM cities WHERE MATCH(`name`) AGAINST('test');

并使用如下:

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]>

<强>样品

我创建了一个5000000行的简单样本表,这是我的结果:

passport.authenticate()