Indexing table

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.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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 G.
Twitter: twitter.com/cynicalryan

Make it right before you make it faster.
~ - The Elements of Programming Style (Kernighan & Plaugher)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkf+ZdUACgkQbtAgaoJTgL+dRgCeJORBDmt06q0/G9WLFCY7L+im
8VAAniCX1Ya+SfF8cDBvDUms8khY9rv+
=7aWA
-----END PGP SIGNATURE-----

http://membres.lycos.fr/ccpfordz

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