Mysql 与中等表(约3m条记录)上的查询性能类似

Mysql 与中等表(约3m条记录)上的查询性能类似,mysql,Mysql,我有个小问题。我有一个大约300万个城市的表格,我需要在上面运行一个类似于的查询 问题是,完成查询大约需要9秒。你知道我该如何快速完成吗 查询是: SELECT * FROM `cities` WHERE name LIKE '%test%' 尝试使用全文索引。InnoDB现在也有全文索引 CREATE FULLTEXT INDEX idx_name_ft ON cities (`name`); 然后像这样使用: SELECT * FROM cities WHERE MATCH(`name

我有个小问题。我有一个大约300万个城市的表格,我需要在上面运行一个类似于的查询

问题是,完成查询大约需要9秒。你知道我该如何快速完成吗

查询是:

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

尝试使用全文索引。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]>

尝试使用全文索引。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]>

你知道,我们不是巫师。发布查询!可能的重复将您正在搜索的查询词放入另一列中,为该列建立索引,并通过直接比较您的词来搜索新列,而不使用
like
,您知道,我们不是向导。发布查询!可能的重复项将您正在搜索的查询词放入另一列中,为该列建立索引,并通过直接比较您的词与新列进行搜索,而不使用
like