27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Pagination<br />

show 50 tickets per page. This will lessen the load on your database, but not completely<br />

eliminate it. That would only be possible if you were to run no queries at all. You could<br />

do exactly that if you cached the output of the page, or even just the part of the page<br />

that showed the list of tickets.<br />

The first process involves saving a copy of the page in the public directory, which<br />

would then be used to serve this page. Any action on tickets, such as creating one,<br />

adding a comment, or changing a state would then wipe this cache and start afresh.<br />

The second process is slightly different. Rather than storing the fragment as a file<br />

on the system, you will store it in memory and then access it through a key.<br />

Finally, by adding indexes to key columns in your tables, such as foreign keys, you<br />

can greatly speed up the queries it runs too. If you had 10,000 tickets in your system<br />

and you wanted to find all the tickets which had project_id set to 123, an index<br />

would help speed up this process.<br />

We’ll show you examples of all of these approaches in this chapter, beginning with<br />

pagination.<br />

16.1 Pagination<br />

We’ll discuss two different kinds of pagination here. The first kind paginates the interface<br />

that users can see, as shown in figure 16.1.<br />

If this project had a thousand tickets, it wouldn’t make sense to show all 1,000 at a<br />

time. It would also be terribly slow, because the database would have to retrieve 1,000<br />

records. Rails would then have to instantiate 1,000 Ticket objects, render 1,000 tickets<br />

to the page, and send back that massive chunk of HTML.<br />

The second kind of pagination has to do with your API. Back in chapter 13 you<br />

wrote the beginnings of the ticket API, and we promised you we’d revisit it in this<br />

chapter. Inside the Api::V1::TicketsController’s index action you have this<br />

innocuous-looking line:<br />

respond_with(@project.tickets)<br />

Again, if the database’s tickets table contains 1,000 records for this project, it will<br />

have to send all of them to Rails. Rails will then have to instantiate 1,000 objects, parsing<br />

them all to JSON or XML before sending them off<br />

to the user. All of this would happen with each<br />

request, and if you were getting a lot of requests it<br />

would bring your application to its knees.<br />

By paginating the result sets in both of these situations,<br />

you can change your application to return<br />

only 50 tickets at a time, which would theoretically<br />

make your application respond 20 times faster than<br />

if it were returning 1,000 tickets. Let’s begin by<br />

installing a gem called Kaminari that will help you<br />

with pagination. Figure 16.1 Tickets for a project<br />

435

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

Saved successfully!

Ooh no, something went wrong!