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?
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.
Rails defaults to using a SQLite database when creating a new project, but you can always change it later.
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.
Most databases (sqlite included, which is the default db in RoR) automatically index the primary key, which with Rails Migrations is :id
by default.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With