Objective:
Today a friend asked a question, the original text is as follows: Mysql5.1,myisam table, select COUNT (*) as total from m_bff WHERE from_uid= ' 73149293 ' and isdeleted=0. From_uid Index, the first time to execute this sentence slow, more than 1 seconds, with show profile is slow on the sending data. But then I add sql_no_cache, execution only takes 0.01 seconds, there are a lot of sentences are similar to the first slow, and then add Sql_no_cache is not slow, key_blocks_unused also a lot, ask what is the reason for this?
The question is: Why is the query for the MyISAM table so much slower for the first time than the second?
Recalling the difference between InnoDB and MyISAM, one of the most important is that MyISAM caches only the indexed content, and InnoDB not only caches the index but also caches the data. That is supposed to say MyISAM every time the data query should be disk access will not have a big speed gap.
In fact, there is a layer of OS-level file caching between the MyISAM file and the operating system. Guessing is the OS file cache ramming Ghost.
Verify:
The authentication method is very simple, run cat 3 >/proc/sys/vm/drop_caches under the shell with root privileges to empty the OS file cache and run SQL to see if it is the same speed as the first run.
Results:
Well, afterEcho 3/proc/sys/vm/drop_caches , it's slow again, and it looks like you're talking about a problem.
Extended:
Once you know the MyISAM feature, you can use the file pre-read method to do data preheating.
Suppose a table is a hotspot table, and the operating system has more memory spare. We can use cat table. MYD >>/dev/null way to load files into the OS file cache. This way, when some users ' data is accessed for the first time, there is no slow query.
MySQL and file system cache