Rendering thousands of records on one page is slow. Pagination loads one page at a time and keeps large lists fast.
Important
Work on this lesson in
advisormode.
Learning goals
- You can explain why rendering a large collection on one page is slow — queries, object instantiation, rendering — and how pagination avoids it.
- You can add pagination to a list with a gem (e.g.
will_paginate) and style its controls to match the app. - You can read the queries pagination produces (a
LIMIT/OFFSETquery and a count) and explain what each is for. - You can create large amounts of test data quickly, e.g. inside a single transaction.
Resources
Read what's new to you, skim what's familiar, skip what you already master. Stop when you can meet the learning goals.
Your agent can also generate an overview, a tutorial or an explanation for anything here, tailored to what you already know. Just ask.
- 📄
will_paginate
Show archive.org snapshot
— README of the gem used in the exercise:
paginate,will_paginateview helper, customizing the rendered links - 📄 Rails Guide: Active Record Query Interface — limit and offset Show archive.org snapshot — what pagination does in SQL
- 📄 pagy Show archive.org snapshot — a faster, framework-agnostic alternative you'll meet in some projects
Exercises
- Create 7500 movies in MovieDB (hint: Doing it in a single transaction is much faster). Load the movies index and measure how long it renders.
- Use the
will_paginategem to add pagination to MovieDB's list of movies. How long does the movies index render now? - Inspect the HTML generated by
will_paginate. Customize the style so it matches the look of your MovieDB. -
tail -f log/development.logand see which querieswill_paginategenerates. What do they do?
Posted by Henning Koch to makandra Curriculum (2015-07-08 17:29)