Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #11058 (new defect)

Opened 3 months ago

Last modified 3 months ago

Data Dictionary Queries are not Cached

Reported by: cfis Assigned to: core
Priority: normal Milestone: 2.x
Component: ActiveRecord Version: edge
Severity: normal Keywords:
Cc:

Description

When we analyze our postgresql log files, the most frequent query is a data dictionary query to get column information, which runs hundreds of thousands of times a day. I wrote up our findings here:

http://cfis.savagexi.com/articles/2008/02/08/rails-and-postgresql-eliminate-hundreds-of-thousands-of-queries-a-day

The attached patch fixes the problem by caching data dictionary queries for tables, indices, columns and serial keys.

Attachments

postgresql_adapter.diff (7.5 kB) - added by cfis on 02/08/08 18:31:12.
Data dictionary caching patch for Postgreqql

Change History

02/08/08 18:31:12 changed by cfis

  • attachment postgresql_adapter.diff added.

Data dictionary caching patch for Postgreqql

02/08/08 20:03:52 changed by cfis

Rick asked me if I had stack traces since column information should be cached. It looks like there is a way around the cache:

ActiveRecord::Associations::HasAndBelongsToManyAssociation.finding_with_ambiguous_select?

Which calls:

ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::column_definitions

02/08/08 20:15:39 changed by cfis

Ok - looked at this a bit more. The issue is this code:

def finding_with_ambiguous_select?(select_clause)

!select_clause && @owner.connection.columns(@reflection.options[:join_table], "Join Table Columns").size != 2

end

And more specifically:

@owner.connection.columns(@reflection.options[:join_table]

My two cents - the column caching code is in the wrong place. Instead of being on ActiveRecord::Base it should be on the base connection adapter. That would neatly solve this issue. Otherwise, I don't see an obvious solution, but I'm not that familiar with Rails internals.