Quantcast

Ruby on Rails, Io, Lisp, JavaScript, Dynamic Languages, Prototype-based programming and more...

Technoblog reader special: $10 off web hosting by FatCow!

Monday, July 10, 2006

Use or Force an Index with Rails

I just found an ActiveRecord hint that is very useful and very undocumented. If you are working with a table who's indexes are sufficiently complex, MySQL will inevitably guess the wrong index at times. To counteract that in SQL, you write:
SELECT * FROM alphas USE INDEX (some_index) WHERE betas = gamas;

Rails does not have a corresponding :use_index for find methods. However, it does have an un-documented :from option.
Alpha.find(:all, :from => "alphas USE INDEX (some_index)", :conditions => "betas = gamas")

Keep this in the back of your mind if you are going to be working with complex database tables any time soon.

Technoblog reader special: $10 off web hosting by FatCow!

4 Comments:

Anonymous Dan said...

I had the same problems with mysql not performing as it should, I used:

[code]
@posts = ForumPosts.Find(:all, :order => 'touched DESC', :joins => 'FORCE INDEX (post_touched)')
[/code]

I had written about it on my blog, but have been in the same situation as you - my host f*cked me sideways...

2:55 AM, July 11, 2006

 
Anonymous Small Business Web Hosting said...

Dan,

Switch Web Hosting Companies!

6:30 AM, October 01, 2006

 
Blogger Bobbie the GrandParents Home Finder said...

We have many offerings. Please review
Of course Bloggers get 10% discount
Refer a company and get 10% commission

* We have super fast servers with many upstream providers.
* 250 strands of fiber
* power for months on battery
* first month free if you mention blogger for 10% off

** many hosted solutions **
hosted solution provider Great Plains
Cisco Support
Novell GroupWise to exchange
hosted solution QuickBooks Pro
asterisk voip hosted pbx
Hosted Exchange Server
hosted solution for Microsoft Dynamics
hosted cisco platform for voip
** Need a Job? ***
IT Staffing
* Outsourcing *
IT Outsourcing
** Have a Case to win ***
Forensic Services

9:10 PM, August 05, 2007

 
Anonymous Anonymous said...

This company provide amazing tier one level connectivity. You have to check it out.
CoLocation Los Angeles

6:56 PM, August 16, 2007

 

Post a Comment

<< Home