Tuesday, May 3, 2011

debugging a rails controller making too many queries

Hi All,

I have a rails application that about 3 years old, and I'm having a problem with my pages making too many queries. Every page that loads has several lines that look like this:

ReqTdsLink Columns (1.0ms)   SHOW FIELDS FROM `req_tds_links`

what sort of method call would cause this output in the log? I can't find any before filters, or anything else in the application controller that could be causing this, but I have yet to check all the views (which are astronomical in number) I'd like to have something specific to look for before i start manually scanning each file for something that might cause this.

thanks,

-C

From stackoverflow
  • This is called by ActiveRecord reading the attributes of a model from your database. It won't happen multiple times in production, but in development mode, Rails queries the table each time to pick up on any changes that may have occurred.

  • This line actually comes from calling the model, i had an array in the application controller that looked like [Model1, Model2, Model3] and I changed it to look like ['Model1','Model2', 'Model3'] and that fixed all the extra calls.

  • Are you running in development or production mode?

    SHOW FIELDS FROM foo is done by your model, as you noted, so it knows which accessor methods to generate.

    In development mode, this is done every request so you don't need to reload your webserver so often, but in production mode this information should be cached, even if you're running a three year old version of Rails.

    Mike Woodhouse : This is certainly true in Rails 2.1.2, which is where I am right now.
  • If you have this kind of problem again and need to track down how a query is being called I would suggest the Query Trace plugin: http://github.com/ntalbott/query_trace/tree/master. It adds a stack trace to the log for every SQL query. Very handy.

0 comments:

Post a Comment