# Indexing of association

**URL:** <https://www.ruby-forum.com/t/indexing-of-association/145347>\
**Category:** Rails\
**Created:** [August 8, 2008, 9:03am UTC](https://www.ruby-forum.com/t/indexing-of-association/145347 "2008-08-08T09:03:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shuaib85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shuaib85](https://www.ruby-forum.com/u/shuaib85)\
**Post date:** [August 8, 2008, 9:03am UTC](https://www.ruby-forum.com/t/indexing-of-association/145347/1 "2008-08-08T09:03:55Z")

</div>

Dear

Which table do we index when we have many\_to\_many relations on rails?  
since the foreign key is not in any of the main tables;

when I query, i use eager loading. However, the performance is kind of  
slow, because I am confused which table to index

Thanks

---

<div class="post-metadata">

**Author:** ![shuaib85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shuaib85](https://www.ruby-forum.com/u/shuaib85)\
**Post date:** [August 13, 2008, 12:36pm UTC](https://www.ruby-forum.com/t/indexing-of-association/145347/2 "2008-08-13T12:36:25Z")

</div>

ok guys

another question

if i have this

select \* from users order by name desc

in order to increase the performance, is it possible to index the  
column ‘name’  
or it will not help

indexing is a bit confusing me, any idea  
thanks

---

<div class="post-metadata">

**Author:** ![shuaib85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shuaib85](https://www.ruby-forum.com/u/shuaib85)\
**Post date:** [August 13, 2008, 12:42pm UTC](https://www.ruby-forum.com/t/indexing-of-association/145347/3 "2008-08-13T12:42:17Z")

</div>

On 13 Aug 2008, at 11:35, Shuaib85 wrote:

> in order to increase the performance, is it possible to index the  
> column ‘name’  
> or it will not help

This sort of thing is ultimately database dependant, so check the docs  
for your database.  
Most should on something like this. (and you can always use EXPLAIN to  
see whether the database is using your index)

Fred

---

<div class="post-metadata">

**Author:** ![shuaib85](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shuaib85](https://www.ruby-forum.com/u/shuaib85)\
**Post date:** [August 14, 2008, 9:04am UTC](https://www.ruby-forum.com/t/indexing-of-association/145347/4 "2008-08-14T09:04:58Z")

</div>

I have added the index to the column name,

i did the following queries, it seems that order by name uses the  
index, if not is there any other way

thanks

mysql\> explain select \* from products;  
±—±------------±--------±-----±--------------±-----±--------  
±-----±-----±------+  
| id | select\_type | table | type | possible\_keys | key | key\_len |  
ref | rows | Extra |  
±—±------------±--------±-----±--------------±-----±--------  
±-----±-----±------+  
| 1 | SIMPLE | products | ALL | NULL | NULL | NULL  
| NULL | 129 | |  
±—±------------±--------±-----±--------------±-----±--------  
±-----±-----±------+  
1 row in set (0.00 sec)

mysql\> explain select \* from products order by name;  
±—±------------±--------±------±--------------  
±----------------------±--------±-----±-----±------+  
| id | select\_type | table | type | possible\_keys |  
key | key\_len | ref | rows | Extra |  
±—±------------±--------±------±--------------  
±----------------------±--------±-----±-----±------+  
| 1 | SIMPLE | products | index | NULL |  
index\_products\_on\_name | 258 | NULL | 129 | |  
±—±------------±--------±------±--------------  
±----------------------±--------±-----±-----±------+  
1 row in set (0.00 sec)

On Aug 13, 6:41 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
