# Database indexes

**URL:** <https://www.ruby-forum.com/t/database-indexes/69346>\
**Category:** Rails\
**Created:** [September 20, 2006, 1:08pm UTC](https://www.ruby-forum.com/t/database-indexes/69346 "2006-09-20T13:08:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![surendra\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@surendra\_s](https://www.ruby-forum.com/u/surendra_s)\
**Post date:** [September 20, 2006, 1:08pm UTC](https://www.ruby-forum.com/t/database-indexes/69346/1 "2006-09-20T13:08:27Z")

</div>

Hi,  
I have 4-5 different types of models which can be tagged. So, in the  
taggings  
table should I declare a multicolumn index on [taggable\_type,  
taggable\_id] or  
a single column index on [taggable\_id]. What are the pros and cons of  
either,  
and which one is preferred?

If multicolumn, then in what order, i.e., [taggable\_type, taggable\_id]  
or  
should it be [taggable\_id, taggable\_type]?

I have another table for tracking views for different objects, and this  
time  
there are 3 columns [viewable\_type, viewable\_id, user\_id], so should  
this one  
be a 3 column index?

My question is not really Rails related, but I am hoping that I will  
get some  
some advice from other fellow Web developers.

## Thanks a lot.

Surendra S.  
[http://ssinghi.kreeti.com](http://ssinghi.kreeti.com), [http://www.kreeti.com](http://www.kreeti.com)  
Read my blog at: [http://cuttingtheredtape.blogspot.com/](http://cuttingtheredtape.blogspot.com/)  
,----  
| Great wits are sure to madness near allied,  
| And thin partitions do their bounds divide.  
|  
| (John Dryden, Absalom and Achitophel, 1681)  
`----

---

<div class="post-metadata">

**Author:** ![surendra\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@surendra\_s](https://www.ruby-forum.com/u/surendra_s)\
**Post date:** [September 20, 2006, 10:50pm UTC](https://www.ruby-forum.com/t/database-indexes/69346/2 "2006-09-20T22:50:01Z")

</div>

On Sep 20, 2006, at 7:02 AM, Surendra S. wrote:

> If multicolumn, then in what order, i.e., [taggable\_type,  
> taggable\_id] or  
> should it be [taggable\_id, taggable\_type]?

The database will be able to use an index on [taggable\_type,  
taggable\_id] to get all rows for a given type which is almost  
certainly more likely that wanting all the types sharing an id.

> I have another table for tracking views for different objects, and  
> this time  
> there are 3 columns [viewable\_type, viewable\_id, user\_id], so  
> should this one  
> be a 3 column index?

Generally, every “prefix” of the columns in an index acts almost like  
an index of those columns, so there’s very little benefit to having  
both [alpha\_id,beta\_id] and [alpha\_id,beta\_id,gamma\_id] indices.  
However, I’ll add both [alpha\_id,beta\_id] and [beta\_id,alpha\_id]  
indices to a join table between “alphas” and “betas” if the lookups  
come from both directions.

> ,----  
> | Great wits are sure to madness near allied,  
> | And thin partitions do their bounds divide.  
> |  
> | (John Dryden, Absalom and Achitophel, 1681)

you’re welcome,

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![surendra\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@surendra\_s](https://www.ruby-forum.com/u/surendra_s)\
**Post date:** [September 21, 2006, 1:53am UTC](https://www.ruby-forum.com/t/database-indexes/69346/3 "2006-09-21T01:53:34Z")

</div>

I thought one chooses indexes with respect to the queries that will be  
run. With mysql you use EXPLAIN with the SQL statement you’re trying to  
optimize for.

EXPLAIN select a,b,c,d from T where …

Then you look at the output and see what is missing, or what is  
overkill.

Stephan
