0

I am new to database optimisations and I've been creating Rails apps for some time now, by using scaffolding tools, generators and web tutorials. So far, pretty much everything works fine, no big loads of users/traffic/data and the UX is flawless.

I have just started to create a huge application that will hold customer support tickets and updates, so I am starting to worry about performance in the near future. I only remember indexes from my university education as database references for fast search.

What is unclear to me is when should I use them and how? I've seen the scaffolders add an add_index for the id and created_at datetime of a new model but I don't know If they are right to use and what should I add by myself in order to keep the object retrieval speed to an optimum/fast level and also learn common practises when creating models.

I've also seen indexes including 2 or more fields. Why?

My models are User, Customer, SupportTicket, so any recommendations will be appreciated. I am looking into possible examples in Rails.

Radolino
  • 1,834
  • 4
  • 26
  • 48
  • 1
    Possible duplicate of [How does database indexing work?](http://stackoverflow.com/questions/1108/how-does-database-indexing-work) – Tobias Dec 10 '15 at 12:27

1 Answers1

0

Indices will make your searches fast. You need to analyse your models and see, by which all parameters you are going to search more. Example

 User.where(email: "test@example.com")

In general, we always search for users using an email address, so it makes sense to put an index on it. So as a thumb rule, you need to check your fields and the ones, which would be used with .find or .where condition more.

However, the index also needs its own space, which can also take up a significant amount of space, so you need to be careful about that.

J.Olufsen
  • 13,415
  • 44
  • 120
  • 185
Sudipta Mondal
  • 2,550
  • 1
  • 19
  • 20