# Problem indexing add Simple Table Inherance

**URL:** <https://www.ruby-forum.com/t/problem-indexing-add-simple-table-inherance/184977>\
**Category:** Rails\
**Created:** [March 11, 2010, 10:50am UTC](https://www.ruby-forum.com/t/problem-indexing-add-simple-table-inherance/184977 "2010-03-11T10:50:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 11, 2010, 10:50am UTC](https://www.ruby-forum.com/t/problem-indexing-add-simple-table-inherance/184977/1 "2010-03-11T10:50:11Z")

</div>

I have a simple table inherance, with an upper class GeneralElement. A  
class that inherits is Activity:  
class Activity \< GeneralElement  
…  
end

The GeneralElement table is very big (about 2.000.000 rows!). Other  
classes that inherit from GeneralElement return queries very fast, but  
Activity.last is very slow. I have added indexes to id and type, but it  
has no effect. What can I do?

I’ll appreciate any help

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 12, 2010, 7:34pm UTC](https://www.ruby-forum.com/t/problem-indexing-add-simple-table-inherance/184977/2 "2010-03-12T19:34:41Z")

</div>

On Mar 11, 4:50 am, John S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a simple table inherance, with an upper class GeneralElement. A  
> class that inherits is Activity:  
> class Activity \< GeneralElement  
> …  
> end
> 
> The GeneralElement table is very big (about 2.000.000 rows!). Other  
> classes that inherit from GeneralElement return queries very fast, but  
> Activity.last is very slow. I have added indexes to id and type, but it  
> has no effect. What can I do?

You may want to grab the query that Activity.last is using, and try  
running it through ‘EXPLAIN’ to see what it’s looking for. Have you  
defined an order (via default\_scope, for instance) on Activity? You  
might need to have an index on that field together with ‘type’ to get  
right behavior.

–Matt J.

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 14, 2010, 11:39pm UTC](https://www.ruby-forum.com/t/problem-indexing-add-simple-table-inherance/184977/3 "2010-03-14T23:39:29Z")

</div>

Matt J. wrote:

> On Mar 11, 4:50ï¿½am, John S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I have a simple table inherance, with an upper class GeneralElement. A  
> > class that inherits is Activity:  
> > class Activity \< GeneralElement  
> > …  
> > end
> > 
> > The GeneralElement table is very big (about 2.000.000 rows!). Other  
> > classes that inherit from GeneralElement return queries very fast, but  
> > Activity.last is very slow. I have added indexes to id and type, but it  
> > has no effect. What can I do?
> 
> You may want to grab the query that Activity.last is using, and try  
> running it through ‘EXPLAIN’ to see what it’s looking for. Have you  
> defined an order (via default\_scope, for instance) on Activity? You  
> might need to have an index on that field together with ‘type’ to get  
> right behavior.
> 
> –Matt J.

I have added an index to the type column; but maybe it’s possible to add  
a special index to both the id and type column at the same time. How  
could I do this?

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 16, 2010, 2:18am UTC](https://www.ruby-forum.com/t/problem-indexing-add-simple-table-inherance/184977/4 "2010-03-16T02:18:21Z")

</div>

On Mar 14, 6:39 pm, John S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > Activity.last is very slow. I have added indexes to id and type, but it  
> > > I have added an index to the type column; but maybe it’s possible to add  
> > > a special index to both the id and type column at the same time. How  
> > > could I do this?

add\_index will accept an array of columns:

add\_index :general\_elements, [:type, :id]

–Matt J.
