Phoenix Rising
2011-Aug-07 04:55 UTC
How can I disable Rails 3.1''s ActiveRecord Query Caching?
Hey guys, As most of you know, Rails 3.1 introduces query caching for ActiveRecord. Great change all-in-all, but for those of us with IT department mandates for using MySQL (yeah, yeah, I know...), it''s kind of a mixed bag. I need to disable query caching in a project I''m building based on 3.1. I''ve tried a couple different approaches, both without any success. I was hoping some one could enlighten me on how to do that, exactly. The first approach I tried was to create an initializer (config/ initializer/active_record.rb) and include this code: ActiveRecord::Base.connection.disable_query_cache! (Documented at http://edgeapi.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/QueryCache.html without comments or examples, unsurprisingly). This doesn''t appear to work as I''m still seeing prepared statement caching going on in my log file: Started GET "/dashboard" for 127.0.0.1 at 2011-08-06 22:43:07 -0600 Processing by DashboardController#index as HTML User Load (13.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]] Rendered dashboard/index.html.erb within layouts/application (0.3ms) CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 So my next attempt was to place it in config/environments/ development.rb (I''m of course trying to get this to work in development mode before doing it in production, obviously): MyApp::Application.configure do # ... other stuff ... config.active_record.disable_query_cache! end Same deal. I tried also changing that to config.active_record.cache_queries = false - no such luck (and that was just a long shot guess). Can anyone explain to me exactly how I''m supposed to turn this off? Thanks. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-Aug-07 08:52 UTC
Re: How can I disable Rails 3.1''s ActiveRecord Query Caching?
On Aug 7, 5:55 am, Phoenix Rising <polarisris...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hey guys, > > As most of you know, Rails 3.1 introduces query caching for > ActiveRecord. Great change all-in-all, but for those of us with IT > department mandates for using MySQL (yeah, yeah, I know...), it''s kind > of a mixed bag. > > I need to disable query caching in a project I''m building based on > 3.1. I''ve tried a couple different approaches, both without any > success. I was hoping some one could enlighten me on how to do that, > exactly. > > The first approach I tried was to create an initializer (config/ > initializer/active_record.rb) and include this code: > ActiveRecord::Base.connection.disable_query_cache! > > (Documented athttp://edgeapi.rubyonrails.org/classes/ActiveRecord/ConnectionAdapter... > without comments or examples, unsurprisingly). > > This doesn''t appear to work as I''m still seeing prepared statement > caching going on in my log file: > Started GET "/dashboard" for 127.0.0.1 at 2011-08-06 22:43:07 -0600 > Processing by DashboardController#index as HTML > User Load (13.4ms) SELECT "users".* FROM "users" WHERE "users"."id" > = $1 LIMIT 1 [["id", 1]] > Rendered dashboard/index.html.erb within layouts/application (0.3ms) > CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 > LIMIT 1 > > So my next attempt was to place it in config/environments/ > development.rb (I''m of course trying to get this to work in > development mode before doing it in production, obviously): > > MyApp::Application.configure do > # ... other stuff ... > config.active_record.disable_query_cache! > end > > Same deal. I tried also changing that to > config.active_record.cache_queries = false - no such luck (and that > was just a long shot guess). > > Can anyone explain to me exactly how I''m supposed to turn this off? > Thanks.The ActiveRecord::QueryCache middleware turns on query caching during for each request. Remove it from the middleware stack and you should be ok ( http://guides.rubyonrails.org/configuring.html#configuring-middleware ) Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Phoenix Rising
2011-Aug-08 04:57 UTC
Re: How can I disable Rails 3.1''s ActiveRecord Query Caching?
As usual, Fred - you''re the man. Thanks a ton. One other question, however: how exactly can I remove that from the middleware stack? Looking at http://api.rubyonrails.org/classes/ActionDispatch/MiddlewareStack.html, I''m not seeing any method that I can use to "subtract" this from the stack. My understanding is that I''d do it something like this: # app/config/environments/development.rb MyApp::Application.configure do # ... other stuff ... config.middleware.??? ActiveRecord::QueryCache end What method should I use in place of ??? ? (damn that''s a lot of question marks). The only thing I can guess at this point would be using swap and calling nil on that, to swap that middleware with "nil", but that seems really...off: config.middleware.swap ActiveRecord::QueryCache, nil In theory that would work, but is there a cleaner way to do it? Any input here? Thanks again! On Aug 7, 2:52 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Aug 7, 5:55 am, Phoenix Rising <polarisris...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > > > > > Hey guys, > > > As most of you know, Rails 3.1 introduces query caching for > > ActiveRecord. Great change all-in-all, but for those of us with IT > > department mandates for using MySQL (yeah, yeah, I know...), it''s kind > > of a mixed bag. > > > I need to disable query caching in a project I''m building based on > > 3.1. I''ve tried a couple different approaches, both without any > > success. I was hoping some one could enlighten me on how to do that, > > exactly. > > > The first approach I tried was to create an initializer (config/ > > initializer/active_record.rb) and include this code: > > ActiveRecord::Base.connection.disable_query_cache! > > > (Documented athttp://edgeapi.rubyonrails.org/classes/ActiveRecord/ConnectionAdapter... > > without comments or examples, unsurprisingly). > > > This doesn''t appear to work as I''m still seeing prepared statement > > caching going on in my log file: > > Started GET "/dashboard" for 127.0.0.1 at 2011-08-06 22:43:07 -0600 > > Processing by DashboardController#index as HTML > > User Load (13.4ms) SELECT "users".* FROM "users" WHERE "users"."id" > > = $1 LIMIT 1 [["id", 1]] > > Rendered dashboard/index.html.erb within layouts/application (0.3ms) > > CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 > > LIMIT 1 > > > So my next attempt was to place it in config/environments/ > > development.rb (I''m of course trying to get this to work in > > development mode before doing it in production, obviously): > > > MyApp::Application.configure do > > # ... other stuff ... > > config.active_record.disable_query_cache! > > end > > > Same deal. I tried also changing that to > > config.active_record.cache_queries = false - no such luck (and that > > was just a long shot guess). > > > Can anyone explain to me exactly how I''m supposed to turn this off? > > Thanks. > > The ActiveRecord::QueryCache middleware turns on query caching during > for each request. Remove it from the middleware stack and you should > be ok (http://guides.rubyonrails.org/configuring.html#configuring-middleware > ) > > Fred-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hassan Schroeder
2011-Aug-08 14:05 UTC
Re: Re: How can I disable Rails 3.1''s ActiveRecord Query Caching?
On Sun, Aug 7, 2011 at 9:57 PM, Phoenix Rising <polarisrising-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> As usual, Fred - you''re the man. Thanks a ton. One other question, > however: how exactly can I remove that from the middleware stack? > Looking at http://api.rubyonrails.org/classes/ActionDispatch/MiddlewareStack.html, > I''m not seeing any method that I can use to "subtract" this from the > stack.http://edgeguides.rubyonrails.org/configuring.html#configuring-middleware HTH, -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Phoenix Rising
2011-Aug-08 23:30 UTC
Re: How can I disable Rails 3.1''s ActiveRecord Query Caching?
Unfortunately Hassan that doesn''t really help -- that article doesn''t explain how to *remove* things from the stack. Does anyone know a clean(er) way to do this than what I''ve already theorized above? On Aug 8, 8:05 am, Hassan Schroeder <hassan.schroe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Sun, Aug 7, 2011 at 9:57 PM, Phoenix Rising <polarisris...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > As usual, Fred - you''re the man. Thanks a ton. One other question, > > however: how exactly can I remove that from the middleware stack? > > Looking athttp://api.rubyonrails.org/classes/ActionDispatch/MiddlewareStack.html, > > I''m not seeing any method that I can use to "subtract" this from the > > stack. > > http://edgeguides.rubyonrails.org/configuring.html#configuring-middle... > > HTH, > -- > Hassan Schroeder ------------------------ hassan.schroe...-Re5JQEeQqe80Tx58lXaADg@public.gmane.org://about.me/hassanschroeder > twitter: @hassan-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hassan Schroeder
2011-Aug-08 23:51 UTC
Re: Re: How can I disable Rails 3.1''s ActiveRecord Query Caching?
On Mon, Aug 8, 2011 at 4:30 PM, Phoenix Rising <polarisrising-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Unfortunately Hassan that doesn''t really help -- that article doesn''t > explain how to *remove* things from the stack. Does anyone know a > clean(er) way to do this than what I''ve already theorized above?>> http://edgeguides.rubyonrails.org/configuring.html#configuring-middle...[quoting that source] They can also be removed from the stack completely: (example) config.middleware.delete ActionDispatch::BestStandardsSupport -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Phoenix Rising
2011-Aug-09 00:25 UTC
Re: How can I disable Rails 3.1''s ActiveRecord Query Caching?
My apologies Hassan, I looked at it and didn''t realize that it was the EDGE version of the guide and thought I was looking at the same documentation that we''d already been through. Thanks! On Aug 8, 5:51 pm, Hassan Schroeder <hassan.schroe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Mon, Aug 8, 2011 at 4:30 PM, Phoenix Rising <polarisris...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Unfortunately Hassan that doesn''t really help -- that article doesn''t > > explain how to *remove* things from the stack. Does anyone know a > > clean(er) way to do this than what I''ve already theorized above? > >>http://edgeguides.rubyonrails.org/configuring.html#configuring-middle... > > [quoting that source] > > They can also be removed from the stack completely: > (example) > config.middleware.delete ActionDispatch::BestStandardsSupport > > -- > Hassan Schroeder ------------------------ hassan.schroe...-Re5JQEeQqe80Tx58lXaADg@public.gmane.org://about.me/hassanschroeder > twitter: @hassan-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Phoenix Rising
2011-Aug-09 01:46 UTC
Re: How can I disable Rails 3.1''s ActiveRecord Query Caching?
Thanks again for your help, Hassan. Unfortunately it''s still caching queries. I''ve tried both approaches in this thread in isolation, and together, and regardless, the query cache is still being used. First, I created an initializer: config/initializers/active_record.rb with ActiveRecord::Base.connection.disable_query_cache! inside it, and then used config.middleware.delete ActiveRecord::QueryCache in config/ environments/development.rb inside the configure block. Unfortunately I''m still left with cached queries inside the log file: Started GET "/dashboard" for 127.0.0.1 at 2011-08-08 19:43:11 -0600 Processing by DashboardController#index as HTML User Load (12.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]] Do you guys think maybe it''s time to submit a "bug" (for lack of a better word) report for 3.1rc5, or is there another way I should go about testing this? On Aug 8, 6:25 pm, Phoenix Rising <polarisris...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> My apologies Hassan, I looked at it and didn''t realize that it was the > EDGE version of the guide and thought I was looking at the same > documentation that we''d already been through. Thanks! > > On Aug 8, 5:51 pm, Hassan Schroeder <hassan.schroe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > > > > > > > On Mon, Aug 8, 2011 at 4:30 PM, Phoenix Rising <polarisris...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Unfortunately Hassan that doesn''t really help -- that article doesn''t > > > explain how to *remove* things from the stack. Does anyone know a > > > clean(er) way to do this than what I''ve already theorized above? > > >>http://edgeguides.rubyonrails.org/configuring.html#configuring-middle... > > > [quoting that source] > > > They can also be removed from the stack completely: > > (example) > > config.middleware.delete ActionDispatch::BestStandardsSupport > > > -- > > Hassan Schroeder ------------------------ hassan.schroe...-Re5JQEeQqe9ZOsn5GM1qlQ@public.gmane.orgp://about.me/hassanschroeder > > twitter: @hassan-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hassan Schroeder
2011-Aug-09 14:58 UTC
Re: Re: How can I disable Rails 3.1''s ActiveRecord Query Caching?
On Mon, Aug 8, 2011 at 6:46 PM, Phoenix Rising <polarisrising-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks again for your help, Hassan. Unfortunately it''s still caching > queries. I''ve tried both approaches in this thread in isolation, and > together, and regardless, the query cache is still being used. > > First, I created an initializer: config/initializers/active_record.rb > with ActiveRecord::Base.connection.disable_query_cache! inside it, and > then used config.middleware.delete ActiveRecord::QueryCache in config/ > environments/development.rb inside the configure block.I haven''t played with 3.1 at all yet, so I''m swinging in the dark but -- does `rake middleware` list QueryCache or no? -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Matt Jones
2011-Aug-10 15:17 UTC
Re: How can I disable Rails 3.1''s ActiveRecord Query Caching?
On Aug 8, 9:46 pm, Phoenix Rising <polarisris...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks again for your help, Hassan. Unfortunately it''s still caching > queries. I''ve tried both approaches in this thread in isolation, and > together, and regardless, the query cache is still being used. > > First, I created an initializer: config/initializers/active_record.rb > with ActiveRecord::Base.connection.disable_query_cache! inside it, and > then used config.middleware.delete ActiveRecord::QueryCache in config/ > environments/development.rb inside the configure block. Unfortunately > I''m still left with cached queries inside the log file: > > Started GET "/dashboard" for 127.0.0.1 at 2011-08-08 19:43:11 -0600 > Processing by DashboardController#index as HTML > User Load (12.8ms) SELECT "users".* FROM "users" WHERE "users"."id" > = $1 LIMIT 1 [["id", 1]] > > Do you guys think maybe it''s time to submit a "bug" (for lack of a > better word) report for 3.1rc5, or is there another way I should go > about testing this?I think you''ve got the wrong terminology here - you''re not seeing any more *query* caching, but the prepared statement stuff is still active. The Rails internals seem to prefer the term "statement cache" for that functionality, and it doesn''t appear to be affected by the switches mentioned here (which affect caching of query *results*). In fact, there doesn''t seem to be a way to switch it off at all. I have to admit, I''m not terribly familiar with prepared statements (other than to note that they tend to make repeated queries much faster); if there''s a situation where they are causing incorrect operation that''s DEFINITELY a bug and should be reported. --Matt Jones -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.