Data at Scale: Pages, Indexes & Speed
A little data is easy; a lot of data needs care. Paginating results, indexing for fast lookups, and knowing where the slow parts are โ this is what keeps a data app fast as it grows.
Working with a hundred records is effortless; working with a hundred thousand needs thought. As your data grows, a few techniques keep your app fast and usable: paginating results so you never load everything at once, indexing so lookups stay quick, and knowing where the real slow spots are. You don't need these on day one, but understanding them is what lets a data app scale gracefully instead of grinding to a halt. This final session covers data at scale.
Paginate: never load everything
You can't show a million records at once - and shouldn't even load them. Pagination shows one page at a time, fetching only what's needed.
from larzpaginate import paginate
page = paginate(results, page=3, per_page=20)
page.items # just these 20
page.pages # total pages
page.start_index, page.end_index # 'showing 41-60 of 1,240'Indexes and finding the slow parts
Two more ideas keep large data fast: indexes (so the database finds records without scanning everything) and measuring to find the actual bottleneck.
| Technique | What it does |
|---|---|
| Index (in a database) | Fast lookups without scanning every row |
| Pagination | Handle only a slice at a time |
| Measure, don't guess | Find the real slow spot (larzbench) |
| Query only what you need | Don't fetch data you won't use |
An <b>index</b> is like a book's index - it lets the database jump straight to matching rows instead of reading every one, turning a slow scan into a fast lookup (recall O(n) vs O(log n) from DS-101). And when something's slow, measure to find the real cause rather than guessing - the bottleneck is often not where you'd think. These are the tools of keeping data apps fast as they grow.
Scale gracefully
- Paginate - show and load data a page at a time, never everything at once (larzpaginate).
- Indexes let a database find records fast without scanning every row.
- Query only what you need - don't fetch data you won't use.
- Measure to find slow spots (larzbench) rather than guessing - then fix the real one.
๐งฉ Do it with LarzOS
Turn this lesson into action with the exact free tools and AI workflows for the job:
๐ฏ Key takeaways
- A little data is easy; a lot needs care to stay fast - pagination, indexing, and knowing the slow spots.
- Paginate: show and load data one page at a time, never everything at once (larzpaginate).
- An index lets a database find matching records fast without scanning every row (O(log n) vs O(n)).
- When something's slow, measure to find the real bottleneck (larzbench) and query only what you need.
Frequently asked
Why can't I just load all my data and show it - why bother with pagination?
For small amounts of data you can, and pagination isn't needed - but as data grows, loading and showing everything at once becomes slow, wasteful, and eventually impossible, which is why pagination is essential for any app that lists lots of things. Consider what 'load all and show it' means with real scale: if a search returns 50,000 results, or a feed has 100,000 posts, loading all of them means fetching a huge amount of data from your database or file (slow, and heavy on the database), holding it all in memory (wasteful, and potentially more than fits), sending it all to the browser (a massive, slow transfer), and rendering it all on the page (which would make the browser crawl or freeze) - all so the user can see the first handful, since nobody scrolls through 50,000 items anyway. It's enormously inefficient and provides a terrible experience: the page takes forever to load, if it loads at all. Pagination solves this by showing only a manageable slice at a time - say 20 items - with controls to move between pages. Crucially, with a real database, pagination means you only fetch that page's 20 records from the database (using its ability to return just a slice), rather than pulling all 50,000 - so the database does less work, less data travels, less memory is used, and the page loads fast, regardless of how much total data exists. The user sees a fast, usable page and navigates to more only if they want it. This is why virtually every app that displays lists of substantial size paginates: search engines show results in pages, social feeds load a chunk at a time (sometimes as infinite scroll, which is pagination fetching the next page as you scroll), admin tables page through records, APIs return results in pages. It keeps everything fast and light no matter the total volume. The technique also involves the metadata to make it work - which page you're on, how many pages there are, whether there's a next or previous - so you can render navigation, and getting that arithmetic right (which slice, 'showing 41-60 of 1,240', edge cases like the last partial page) is fiddly, which is what larzpaginate handles for you. So: for tiny datasets, showing everything is fine and pagination would be unnecessary complexity; but the moment your data could be large - and for anything user-facing that could grow, that's most of the time - you paginate, both to keep the app fast and light and because loading everything simply doesn't scale. Pagination is one of the fundamental techniques for handling data at scale, and it's essential knowledge for building real data-driven apps. Show a page at a time, fetch only that page, and your app stays fast whether there are 50 records or 50 million.
What exactly is an 'index' and do I need to worry about it as a beginner?
An index is a data structure a database maintains to find records quickly without scanning through all of them - and while you don't need to master indexing as a beginner, understanding the concept is valuable because it explains a huge part of why databases are fast and when they're slow. Here's the idea. Without an index, when you ask a database to find records matching some condition (say, all users with a particular email), it may have to check every single row in the table one by one - a full scan - which is fine for a small table but gets slow as the table grows to thousands or millions of rows (it's the O(n) linear scan from DS-101). An index changes this: it's a separate, cleverly-organised structure (often like a sorted lookup or tree) that lets the database jump almost directly to the matching rows without examining all the others - turning that slow scan into a fast lookup (more like O(log n), the logarithmic speed of binary search you learned about). The analogy in the name is apt: a book's index at the back lets you jump straight to the pages mentioning a topic instead of reading the whole book to find them; a database index does the same for finding rows. Databases automatically index some things (like the primary id of each row) and let you add indexes on columns you frequently search or filter by, so those queries stay fast as data grows. The trade-offs (which is why you don't index everything) are that indexes take extra storage and add a little overhead when writing data (the index must be updated too), so you index the columns you query on, not every column. As a beginner, do you need to worry about it? Not deeply, and not at first - for small projects and while learning, you generally don't need to think about indexes at all; the database handles the basics and your data is small enough that scans are fast anyway. But understanding what an index is pays off in a few ways: it explains why databases can search huge datasets quickly (the magic is indexing), it prepares you to recognise a common performance problem (an app that's slow because a frequent query isn't indexed - a classic issue where adding an index dramatically speeds things up), and it connects to the Big-O thinking you learned (indexes are how databases achieve fast lookups instead of linear scans). So the guidance is: as a beginner, know conceptually that indexes exist and that they make lookups fast by avoiding full scans, but don't stress about managing them - you'll learn to add indexes to frequently-queried columns as you work with real databases and encounter performance needs, which is the natural time to apply the knowledge. It's a 'understand the concept now, apply the details as needed later' topic. The key takeaway for now is simply that when a database query on a large table is slow, an index on the column you're querying is often the fix, because indexes are the mechanism that keeps data lookups fast at scale - the same speed-up principle (avoiding scanning everything) that runs through all of performance thinking. You've actually already met the underlying idea in DS-101's fast-lookup structures and binary search; an index is that idea applied inside a database. Learn it conceptually, and reach for it when your data and queries grow enough to need it.
You've finished the Working with Data sessions
Now lock it in with the coursework.
Do the assignments โRelated lessons
Modelling Your Data Well
How you *structure* your data shapes how easy everything else becomes. Good data modellingโฆ
Working with DataWhere Data Lives
Almost every app is really about data โ storing it and getting it back. The first thing toโฆ
Data Structures & AlgorithmsAlgorithms & Big-O Made Simple
An algorithm is just a recipe for solving a problem โ and Big-O is a simple way to talk abโฆ