Reputation: 3154
What would be an easy way to make active record use SQL_NO_CACHE with all queries in development environment?
I want to optimize the statements for worst case load time. Hope it makes sense to even do it this way, but I get very slow queries the first page hit and next time all queries are really fast, as the server supposedly caches them.
I am using the mysql2 gem 0.3.11 with rails 3.2.3.
Upvotes: 5
Views: 1389
Reputation: 3154
As I didn't want to edit hundreds of queries, I placed a before filter in the application controller like this:
def disable_sql_query_cache_in_development
if Rails.env == "development"
Rails.logger.warn Utils.yellow "ApplicationController.disable_sql_query_cache_in_development", "Query cache will be reset for worst case performance tuning"
#ActiveRecord::Base.connection.execute("SET SESSION query_cache_type = OFF")
ActiveRecord::Base.connection.execute("RESET QUERY CACHE")
end
end
Upvotes: 2
Reputation: 5182
Using a regular rails find you can stuff this in:
2.3.5:
User.find(:all, :select => "SQL_NO_CACHE *", :conditions=>"id = 1234")
3+
User.where(:id => 10).select("SQL_NO_CACHE *")
Upvotes: 1