0

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?

asked Aug 15, 2017 at 11:41
3
  • The data is cached. Commented 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. Commented 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/… Commented Aug 15, 2017 at 13:15

1 Answer 1

0

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.

answered Aug 16, 2017 at 23:30

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.