OhMyRuby
(OhMyRuby)
April 10, 2008, 7:01pm
1
i have a table of users
table of organizations
i am sorting users by last name and outputting the organization they
belong to.
there are only 1,000 records in users but it takes about 25 seconds to
sort by last name output.
is this normal, will indexing make this faster?
can someone give me an example of indexing this.
using - @user = User.find(:all).sort_by(&:lname)
i would like to sort on downcase of last name as well.
rashantha wrote:
i have a table of users
table of organizations
i am sorting users by last name and outputting the organization they
belong to.
there are only 1,000 records in users but it takes about 25 seconds to
sort by last name output.
is this normal, will indexing make this faster?
Yes, indexing will make it faster.
A general overview on indexes:
http://www.odetocode.com/Articles/70.aspx
More specific information should be part of your RDMBS's documentation.
- --
Phillip Gawlowski
Twitter: twitter.com/cynicalryan
Make it right before you make it faster.
~ - The Elements of Programming Style (Kernighan & Plaugher)
Indexing with a call like this:
@user = User.find(:all).sort_by(&:lname)
Will do no good.
User.find(:all) is the db call, the sort_by is a method of enumerable.
So your app grabs all Users then does an enumerable sort on it.
Much more efficient to have the database order it.
@users = User.find(:all, :order => "lname asc")
if you had an index on user.lname THEN indexing would make a
difference.
- Richard