2016-06-28 46 views
0

我有一個小問題。我有一個約3米城市的表格,我需要在其上運行like查詢。像在中型表(〜3m記錄)上的查詢性能

事情是,完成查詢大約需要9秒。任何想法我怎麼能使它非常快速?

查詢是:

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

你知道,我們不是巫師。發佈查詢! – sagi

+0

http://stackoverflow.com/questions/2042269/how-to-speed-up-select-like-queries-in-mysql-on-multiple-columns –

+0

可能將您正在搜索的查詢詞在另一欄中加上'like',爲列指定索引,並通過直接比較你的單詞來搜索新的列而不使用'like' – Fredster

回答

2

嘗試使用全文索引。 InnoDB現在也有FULLTEXT INDEX。

CREATE FULLTEXT INDEX idx_name_ft ON cities (`name`); 

,並使用這樣的:

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

樣品

我已創建一個簡單的示例表500萬行,這是我的結果:

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

完美,謝謝 – overburn

+0

完美,謝謝:) –