我有一個小問題。我有一個約3米城市的表格,我需要在其上運行like
查詢。像在中型表(〜3m記錄)上的查詢性能
事情是,完成查詢大約需要9秒。任何想法我怎麼能使它非常快速?
查詢是:
SELECT * FROM `cities` WHERE name LIKE '%test%'
我有一個小問題。我有一個約3米城市的表格,我需要在其上運行like
查詢。像在中型表(〜3m記錄)上的查詢性能
事情是,完成查詢大約需要9秒。任何想法我怎麼能使它非常快速?
查詢是:
SELECT * FROM `cities` WHERE name LIKE '%test%'
嘗試使用全文索引。 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]>
完美,謝謝 – overburn
完美,謝謝:) –
你知道,我們不是巫師。發佈查詢! – sagi
http://stackoverflow.com/questions/2042269/how-to-speed-up-select-like-queries-in-mysql-on-multiple-columns –
可能將您正在搜索的查詢詞在另一欄中加上'like',爲列指定索引,並通過直接比較你的單詞來搜索新的列而不使用'like' – Fredster