简体   繁体   中英

Mysql slow log: simple select query takes 26 seconds

I have a simple table, which is using 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:

# 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? Will I solve this problem if I upgrade from shared hosting to 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. Alternatively, you could also just add WHERE tag_id > 0 to your query. 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. 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.

If you aren't, then I would guess the bottleneck is somewhere else (likely 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). 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. 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. It may be worthwhile to come up with some kind of caching procedure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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