MySQL: Disable query cache for database profiling

Updated . Posted . Visible to the public.

If you want to see how long your database queries actually take, you need to disable MySQL's query cache. This can be done globally by logging into a database console, run

SET GLOBAL query_cache_type=OFF;

and restart your rails server.

You can also disable the cache on a per query basis by saying

SELECT SQL_NO_CACHE * FROM ...

You also probably want to disable Rails internal (per-request) cache. For this, wrap your code with a call to ActiveRecord::Base.uncached. For example, as an around_filter:

def disable_cache
  ActiveRecord::Base.uncached do
    yield
  end
  true
end

Don't forget to re-enable MySQL query caching later with

SET GLOBAL query_cache_type=ON;
Tobias Kraze
Last edit
Michael Leimstädtner
License
Source code in this card is licensed under the MIT License.
Posted by Tobias Kraze to makandra dev (2010-10-04 14:39)