I have mysql (5.7) innodb table, and run a select query and using an Id foreign key. for the first query on this Id foreign key condition, the query took a lot of time to execute, and If I run another condition on select with the same id foreign key, it run so fast.
Why is that so? and how could I optimize my queries select to run fastely in the first execution?
-
The data is cached.Philᵀᴹ– Philᵀᴹ2017年08月15日 11:55:01 +00:00Commented Aug 15, 2017 at 11:55
-
You cannot. Very simply put it's the process called caching. The first time you execute a query data gets cached into RAM. And stays there. Becase of that second retrieval is much faster, since you have to look into RAM as opposed to reading from disk.Vladimir S.– Vladimir S.2017年08月15日 11:55:25 +00:00Commented Aug 15, 2017 at 11:55
-
The first part of your question is already asked and is possible duplicate of: dba.stackexchange.com/questions/37312/…Karan Desai– Karan Desai2017年08月15日 13:15:31 +00:00Commented Aug 15, 2017 at 13:15
1 Answer 1
No, you cannot optimize a query to run as fast on the first run as on the second. This is because during the first run the database caches information in the system. The second time it is run, if the information is still in the cache, then the database will reuse that info to make the second query faster.
Explore related questions
See similar questions with these tags.