Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid SHOW FIELDS in ActiveRecord

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!

like image 859
Allan Grant Avatar asked Dec 21 '09 15:12

Allan Grant


1 Answers

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.

like image 163
klew Avatar answered Sep 28 '22 18:09

klew