我有个小问题。我有一个大约300万座城市的表,我需要对它运行一个like查询。
问题是,大约需要9s才能完成查询。知道我怎么能很快做到这一点吗?
查询是:
SELECT * FROM `cities` WHERE name LIKE '%test%'发布于 2016-06-28 08:47:37
尝试使用全文索引。InnoDB现在也有全文索引。
CREATE FULLTEXT INDEX idx_name_ft ON cities (`name`);然后像这样使用:
SELECT * FROM cities WHERE MATCH(`name`) AGAINST('test');样本
我创建了一个包含5000000行的简单示例表,这是我的结果:
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]>https://stackoverflow.com/questions/38070249
复制相似问题