views:

148

answers:

2

Is there any way to prevent ActiveRecord from issued a SHOW FIELDS to the database when it's not needed?

I'm working on database performance critical application, and just noticed that on a typical query my SELECT takes 0.5 ms and the related SHOW FIELDS takes 2 ms -- 4 times longer! Even more importantly, it's not needed because I'm already specifying the only column I want to retrieve:

UsersAddress.find(:all, :conditions => {:user_id => 1}, :select => :address_id)

UsersAddress Load (0.5ms) SELECT address_id FROM users_addresses WHERE (users_addresses.user_id = 1)

UsersAddress Columns (2.1ms) SHOW FIELDS FROM users_addresses

Granted, this only happens once each time some table is touched for the first time, but shouldn't it be avoidable complete? First of all, that info is already in my schema. Second, I don't need it.

Any ideas how to optimize this so that Rails won't run a SHOW FIELDS unless it really needs it?

Thanks!

+3  A: 

In production mode it will load only once after starting a server (not every request).

In development mode it is loaded on every request (because it is development mode and it restarts almost everything every request)

So you don't have to worry about it in production mode.

klew
Thanks. I guess I won't worry about it then. Makes sense why it would check in dev mode.. in case I changed my model.
Allan Grant
A: 

it is still relevant to know how to disable it even in production mode. For fcgi and mod-rails the startup time is reduced.

deepak