Search code examples
ruby-on-railsdatabasepostgresqlsingle-table-inheritancesti

Where do indexes go when using STI?


I am using Rails and postgres.

I have a couple of models using STI and i was wondering where i should put indexes on the tables and why?

For example lets say i have the following setup:

class Comment < AR; end
class MovieComment < Comment; end
class MagazineComment < Comment; end


# Fake Comment Table
id:integer
title:string
body:text
type:string

Thanks!


Solution

  • On the type field, if you want only one of MovieComment or MagazineComment. If you don't do that, you won't need the index here. I'm not sure if AR does use type every time, but just to make sure.

    Because the id field is a primary key, an index should already be there.

    If you want to query by both type and id make sure you have a combined index.

    On the other fields: Depends what you query on, but I suppose you want to retrieve these only.