Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails -- database index necessary for :id attribute?

So as I was following the Ruby on Rails Tutorial by Michael Hartl I noticed that in the users table we added a unique index for the :email attribute to improve the efficiency of the find method so it doesn't search row by row. So far we have been searching using both find_by_email and find_by_id depending on the case. Yet we never set up an index for the :id attribute. Is :id automatically indexed because it is by default unique and sequential in nature? Or is this not the case and should I add an index for :id searching?

like image 606
Kvass Avatar asked Jun 23 '11 04:06

Kvass


People also ask

What is index in database rails?

An index is used to speed up the performance of queries on a database. Rails allows us to create index on a database column by means of a migration. By default, the sort order for the index is ascending. But consider the case where we are fetching reports from the database.

What database does Rails use by default?

Rails defaults to using a SQLite database when creating a new project, but you can always change it later.

What is SQL Indexing?

A SQL index is used to retrieve data from a database very fast. Indexing a table or view is, without a doubt, one of the best ways to improve the performance of queries and applications. A SQL index is a quick lookup table for finding records users need to search frequently.


1 Answers

Most databases (sqlite included, which is the default db in RoR) automatically index the primary key, which with Rails Migrations is :id by default.

like image 124
smcphill Avatar answered Oct 18 '22 19:10

smcphill