I have the following Post model:
create_table "posts", :force => true do |t|
t.string "title"
t.string "content"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "comments_count", :default => 0, :null => false
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
t.boolean "published", :default => false
t.datetime "published_at"
t.boolean "draft", :default => false
t.string "document_file_name"
t.string "document_content_type"
t.integer "document_file_size"
t.datetime "document_updated_at"
end
add_index "posts", ["user_id", "created_at"], :name => "index_posts_on_user_id_and_created_at"
I only have 2 indexes so far.
I’m still not quite sure which columns to index to improve performance, so I would like to hear some suggestions.
(Sorry, I’m new to indexing so I was wondering if this: ["user_id", "created_at"] is creating a relation between these two indexes? (or there is no relation between indexes?) Or it looks like that because they were created in the same Rails migration?”)
You should worry about indexing columns when you know that you are going to be doing SQL lookups on that column often. Without seeing the rest of your application code, it’s hard to determine which columns you should index, but I’d say that you’ve chosen two good ones.
Below is a good post that helped me understand about indexes, it’s got some old Rails syntax in there, but what’s relevant are the points it makes about when to index a column.
http://tomafro.net/2009/08/using-indexes-in-rails-choosing-additional-indexes