Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails - created_at when user for ordering, Should you add an Index to the table?

Hello for one of my models photos I have:

default_scope :order => 'photos.created_at DESC, photos.version DESC'

Given that I'm ordering by CREATED_AT and Version... Should I have a DB index on CREATED_AT?

Thanks

like image 991
AnApprentice Avatar asked Dec 14 '10 23:12

AnApprentice


1 Answers

One trick that may be helpful: in typical MySql/ActiveRecord models, you will have id as a numeric primary key (hence implicitly indexed), which should be in the same order of created_at - which means you could order by id instead and get the fast performance, without the write cost for an extra index.

like image 92
chesterbr Avatar answered Sep 17 '22 11:09

chesterbr