简体   繁体   English

MySQL慢日志:简单的选择查询需要26秒

[英]Mysql slow log: simple select query takes 26 seconds

I have a simple table, which is using InnoDB: 我有一个简单的表,正在使用InnoDB:

tag_id int(20), primary
tag varchar(50)

There are only 106 tags in the table and sometimes this simple select query is taking 10s, 16s, 30s or more: 表中只有106个标签,有时这个简单的选择查询需要10s,16s,30s或更长时间:

# Query_time: 26  Lock_time: 0  Rows_sent: 106  Rows_examined: 106
use database;
SELECT `tag`
FROM (`tags`);

My question: is there any way to optimize this query (so it wouldn't take 26s to complete) or is this a clear sign for Mysql server overload? 我的问题:有什么方法可以优化此查询(这样就不需要26s的时间来完成),或者这是Mysql服务器超载的明显标志吗? Will I solve this problem if I upgrade from shared hosting to VPS? 如果我从共享主机升级到VPS,是否可以解决此问题?

It might be faster if you force it to use the primary key instead of doing a full table scan. 如果您强迫它使用主键而不是进行全表扫描,则可能会更快。 Try doing SELECT tag FROM tags USE INDEX(PRIMARY) if you are using innodb. 如果您使用的是innodb,请尝试SELECT tag FROM tags USE INDEX(PRIMARY) Alternatively, you could also just add WHERE tag_id > 0 to your query. 另外,您也可以在查询中添加WHERE tag_id > 0 From my understanding, innodb will do a range scan, which is more costly, as opposed to an index scan, if no index is used in the query. 据我了解,如果在查询中未使用任何索引,则innodb会进行范围扫描,这比索引扫描要昂贵得多。 If you force it to use an index, it will scan the index instead to find all of the rows of the table, which is probably going to be faster. 如果您强迫它使用索引,它将扫描索引而不是查找表的所有行,这可能会更快。 There may be more at play here though, I'm not as well versed in the mysql/innodb internals as I used to be. 不过,这里可能还有更多活动,我不像以前那样精通mysql / innodb内部。

If you aren't, then I would guess the bottleneck is somewhere else (likely HDD I/O). 如果您不是,那么我会认为瓶颈还在其他地方(可能是HDD I / O)。 Upgrading to a different server isn't the only solution in that case (although it may solve the problem). 在那种情况下,升级到另一台服务器并不是唯一的解决方案(尽管它可以解决问题)。 If this table isn't constantly changing (ie changing every couple of seconds), it might be worthwhile to use some kind of memory caching mechanism, such as memchached (there are others as well). 如果该表不是不断变化的(即每隔几秒钟变化一次),则可能值得使用某种内存缓存机制,例如memchached(还有其他)。 If you are running into I/O issues, using a memory cache for data from this table and/or others might be worth looking into. 如果您遇到I / O问题,则可能值得研究使用内存缓存来存储此表和/或其他表中的数据。 You may find a host with faster disk I/O, but no matter how you slice it, reads and writes to the disk are expensive. 您可能会发现具有更快磁盘I / O的主机,但是无论如何对其进行切片,对磁盘进行读写都非常昂贵。 It may be worthwhile to come up with some kind of caching procedure. 提出某种缓存过程可能是值得的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM