开发者

Avoid SHOW FIELDS in ActiveRecord

开发者 https://www.devze.com 2022-12-15 00:35 出处:网络
Is there any way to prevent ActiveRecord from issued a SHOW FIELDS to the database when it\'s not needed?

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!


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.


Rails 2.x.x still has problems has_and_belongs_to_many associations

read more http://blog.lukeludwig.com/index.php/2009/01/08/rails-patch-for-caching-show-fields-for-has_and_belongs_to_many-associations/

and http://www.concept47.com/austin_web_developer_blog/ruby-on-rails/how-to-get-a-25-boost-in-your-rails-app/

0

精彩评论

暂无评论...
验证码 换一张
取 消