我有一张桌子(目前)超过200,000行。 每一行都是我网站上的一个页面,我使用的是友情网址,例如site.com/itemName。
在我的SlowQueries日志中,我有这个查询,重复超过1500次,加载时间为1.5秒。
这是查询:
SELECT * FROM table WHERE `name` LIKE 'S' ORDER BY id DESC LIMIT 1
表类型是MyISAM。该字段是varchar 250,上面有FULLTEXT索引。 另外,我有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
答案 0 :(得分:2)
一些随意的想法:
WHERE name LIKE 'S'
与WHERE name='S'
相同。 MySQL可能足够聪明,可以生成相同的执行计划(我没有检查过),但这很令人困惑。FULLTEXT
索引用于全文搜索。你没有做一个,所以索引对这个查询没用。您应该创建一个常规索引。EXPLAIN
或EXPLAIN EXTENDED
预先添加到SQL代码中以获取它。答案 1 :(得分:1)
您可能需要使用fulltext searching functions来利用这些索引。据我所知,LIKE
无法自动使用它们。