6.2 a tip when creating a column in a database

it is important to consider if we will need to find records by that column, the we decide if we need to set index on this column!!!!!!!!!!!!!!!

 

consider, for example, the email attribute, when we allow user to sign in, we need to find the user record corresponding to the submitted email address.

 

if we don't have index on email column, the only way to find a user is to look through each user row in the database and compare its email attribut to the given email. this is known as full-table scan, and for a big site it is a bad thing!

 

putting a index on the email column will solve this issue.

 

a index in a table is like a book index, when geting the record, database no need to get each record, it will just find in a simple table containing the email and the record index, and get the index fast.

 

also, if the index is unique, we can add

:unique => true

then if two record getting saved have the same email, database will report error.

 

你可能感兴趣的:(database,index)