27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Database query enhancements<br />

Indexing the data in your databases allows you to perform fast lookups and avoid<br />

full table scans. Imagine that your database is a phonebook and that the names are in<br />

no particular order. In this situation, it would be difficult to find all people with a<br />

name such as John Smith-McGee, because you’d have to scan the entire phone book<br />

to find out who has this name.<br />

An index sorts this data into a logical order and allows for a much faster lookup.<br />

Ever seen how a phonebook that has the letter and the first name at upper left, and<br />

maybe the same or a different letter at upper right, with another name? That’s an<br />

index. That allows you to easily find names because you know that the letter A comes<br />

before B, and C after B, and so on.<br />

Indexes allow you to run much faster queries because you tell your database how<br />

to index the data. Although it may seem like premature optimization at this point,<br />

you’re going to put an index on your tickets table to speed up finding collections of<br />

tickets for a project. It’s common sense to have these from the beginning: adding<br />

them onto large datasets will take a long time, because you’ll need to work out how to<br />

index each record.<br />

To add this index, create a new migration with this command:<br />

rails g migration add_project_id_index_to_tickets<br />

This will generate a file at db/migrate that ends with the name you’ve given it. You’re<br />

going to need to open this file now and add in the index, because Rails cannot (yet)<br />

read your mind. You’ll add this index inside the self.up part of the migration using<br />

add_index and remove it in the self.down method using remove_index, like this:<br />

def change<br />

add_index :tickets, :project_id<br />

end<br />

Run this migration using rake db:migrate db:test:prepare to run it on the development<br />

and test environment databases. You’ll see this line in the output:<br />

-- add_index(:tickets, :project_id)<br />

-> 0.0015s<br />

Just to reinforce the message: it’s better to add the indexes when the database is first<br />

being designed rather than at a later point because this 0.0015 seconds could easily<br />

become whole seconds on a larger dataset. This index will now group your tickets into<br />

groups of project_id columns, allowing for much faster lookups to find what tickets<br />

belong to a specific project.<br />

You want the absolute best performance you can get out of your database because<br />

it’s a key point in your requests. Indexes and eager loading are the two most basic ways<br />

you can get better performance out of your database.<br />

If your database is performing optimally and your pages still aren’t loading fast<br />

enough, you’ll need to look for alternative methods of speeding them up. Two of<br />

these methods are page and action caching, which allow you to store the output of a<br />

page to serve it up rather than re-processing the code and hitting the database again.<br />

447

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!