[英]Like query performance on medium table (~3m records)
我有一個小問題。 我有一張約有300萬個城市的表格,並且需要對其執行like
查詢。
事實是,完成查詢大約需要9秒鍾。 有什么想法可以使它變得很快嗎?
查詢是:
SELECT * FROM `cities` WHERE name LIKE '%test%'
嘗試使用FULLTEXT INDEX 。 InnoDB現在也有FULLTEXT INDEX。
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]>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.