# Indexing table

**URL:** <https://www.ruby-forum.com/t/indexing-table/134440>\
**Category:** Rails\
**Created:** [April 10, 2008, 9:02pm UTC](https://www.ruby-forum.com/t/indexing-table/134440 "2008-04-10T21:02:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rashantha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@rashantha](https://www.ruby-forum.com/u/rashantha)\
**Post date:** [April 10, 2008, 9:02pm UTC](https://www.ruby-forum.com/t/indexing-table/134440/1 "2008-04-10T21:02:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rashantha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@rashantha](https://www.ruby-forum.com/u/rashantha)\
**Post date:** [April 10, 2008, 9:09pm UTC](https://www.ruby-forum.com/t/indexing-table/134440/2 "2008-04-10T21:09:44Z")

</div>

-----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](http://www.odetocode.com/Articles/70.aspx)

More specific information should be part of your RDMBS’s documentation.

* * *

Phillip G.  
Twitter: [twitter.com/cynicalryan](http://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](http://enigmail.mozdev.org)

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

---

<div class="post-metadata">

**Author:** ![rashantha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@rashantha](https://www.ruby-forum.com/u/rashantha)\
**Post date:** [April 10, 2008, 9:14pm UTC](https://www.ruby-forum.com/t/indexing-table/134440/3 "2008-04-10T21:14:25Z")

</div>

[http://membres.lycos.fr/ccpfordz](http://membres.lycos.fr/ccpfordz)

---

<div class="post-metadata">

**Author:** ![rashantha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@rashantha](https://www.ruby-forum.com/u/rashantha)\
**Post date:** [April 10, 2008, 9:29pm UTC](https://www.ruby-forum.com/t/indexing-table/134440/4 "2008-04-10T21:29:07Z")

</div>

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
