我有一個表(目前)超過200.000行。 每一行都是我網站上的一個頁面,我使用友情網址,例如site.com/itemName。加快MySQL字符串LIKE查詢
在我的SlowQueries日誌上,我有那個查詢,重複了1500次,加載時間爲1.5s。
這是查詢:
SELECT * FROM table WHERE `name` LIKE 'S' ORDER BY id DESC LIMIT 1
表型是MyISAM的。該字段是帶有FULLTEXT索引的varchar 250。 另外,我有這個MySQLTuner結果:
-------- General Statistics --------------------------------------------------
[--] Skipped version check for MySQLTuner script
[OK] Currently running supported MySQL version 5.1.61-log
[OK] Operating on 64-bit architecture
-------- Storage Engine Statistics -------------------------------------------
[--] Status: -Archive -BDB -Federated +InnoDB -ISAM -NDBCluster
[--] Data in MyISAM tables: 191M (Tables: 4)
[--] Data in InnoDB tables: 48K (Tables: 1)
[!!] Total fragmented tables: 1
-------- Performance Metrics -------------------------------------------------
[--] Up for: 1d 4h 56m 2s (3M q [34.272 qps], 37K conn, TX: 36B, RX: 3B)
[--] Reads/Writes: 91%/9%
[--] Total buffers: 290.0M global + 2.7M per thread (151 max threads)
[OK] Maximum possible memory usage: 705.2M (68% of installed RAM)
[OK] Slow queries: 0% (2K/3M)
[OK] Highest usage of available connections: 8% (13/151)
[OK] Key buffer size/total MyISAM indexes: 8.0M/63.3M
[OK] Key buffer hit rate: 100.0% (581M cached/182K reads)
[!!] Query cache efficiency: 4.7% (146K cached/3M selects)
[OK] Query cache prunes per day: 0
[!!] Sorts requiring temporary tables: 32% (8K temp sorts/26K sorts)
[OK] Temporary tables created on disk: 5% (3 on disk/53 total)
[OK] Thread cache hit rate: 99% (13 created/37K connections)
[OK] Table cache hit rate: 53% (33 open/62 opened)
[OK] Open file limit used: 5% (59/1K)
[OK] Table locks acquired immediately: 99% (3M immediate/3M locks)
[OK] InnoDB data size/buffer pool: 48.0K/8.0M
你的意思是'LIKE'S%''或類似嗎?您目前的查詢實際上只是'='S'' – MatBailie
不,我的意思是'喜歡'S''。就我所知,名稱LIKE'S'與WHERE name ='S'相同。 – Biwu
在這種情況下,你有'name,id'上的索引嗎?你的查詢非常簡單,這樣一個索引應該是你需要的全部... – MatBailie