High Performance
SQLite
Introduction
Introduction to this course
Introduction to SQLite
SQLite is safe
It's just a file
Running SQLite locally
Good uses for SQLite
SQLite's limitations
The documentation
SQLite Internals
SQLite's structure
SQLite's file format
Dot commands
Pragmas
Virtual tables
Schema
Flexible types
Types
Strict types
Dates
Booleans
Floating point
Rowid tables
Auto increments
Without rowid
Generated columns
Optimizing SQLite
Locking
Rollback mode
WAL mode
WAL vs Journal benchmarks
Busy timeout
Transaction modes
Vacuum
Analyze & Optimize
Suggested pragmas
Faster inserts
Indexes
Introduction to indexes
B+ trees
Primary, secondary, and clustered indexes
Without rowid tables
Benchmarking without rowid tables
Imposter tables
Primary key data types
Where to add indexes
Index selectivity
Composite indexes
Composite ordering
Covering indexes
Partial indexes
Indexes on expressions
Automatic indexes
Duplicate indexes
Indexing joins
Advanced SQL
Explain
Index obfuscation
Joins
Subqueries
Unions
CTEs
Recursive CTEs
Window functions
Dealing with NULLs
Row value syntax
Indexed sorting
Upserts
Returning
Aggregates
Triggers
Operating SQLite
Altering schema
Altering schema with tools
Multi-database
Multi-tenancy
Backups
Exports
How to corrupt SQLite
Advanced JSON
Intro to JSON
JSON vs. JSONB
Argument types: path, value, JSON
JSON5
Valid JSON
Creating JSON objects + arrays
JSON Extraction
Updating JSON
JSON aggregates
JSON table functions
Indexing JSON
Full Text Search
Creating FTS tables
Performing searches
Ranking results
Highlighting matches
Bonus Interviews
DHH discusses SQLite in Ruby on Rails
Distributed SQLite with Litestream and LiteFS
Offline-first, multiplayer SQLite
Production SQLite with Turso and libSQL
Migrating from Postgres to SQLite with Kent C. Dodds
Ruby on Rails with SQLite
Moving from Redis to SQLite with Mike Buckbee
Locked video

Please purchase the course to watch this video.

Video thumbnail
Indexes
B+ trees

Full Course

$
179
$229
USD, one-time fee
Fantastic! So much packed into one course. Full-text search was a cherry on top of what's possible with SQLite. The course opened my mind to completely new ideas, and gave me perspective on databases in general.
Przemyslaw Figura
Przemyslaw Figura

SQLite for Production

Build with and integrate SQLite into your production applications.

Test Data

I've made my test data available for you to use and follow along.

Download
or
Use on Turso

Summary

In the video, I explain how B-trees help databases quickly find information without scanning the entire table. By creating an index on a table, like on the "name" column for a list of users, we arrange the data in a B-tree that makes searching faster, especially for large datasets. This structure allows us to efficiently locate names, such as Jennifer or Taylor, by navigating through nodes until we reach the desired result.

Video Transcript

Okay. It's time to get a little bit academic but not terribly academic. I'm not a computer science guy. I just am a database enthusiast. So we're gonna talk about B trees.

B trees are the underlying data structure that make indexing effective, that make it powerful, that make it fast. So the b tree is the way that the data is arranged when you create an index. For this example, we have a users table. So if I do select star from users, it's a very simple table. It has 10 people in it.

And what we're gonna walk through is we're gonna walk through me creating an index on the name column and then I will show you, just kind of visually what that structure looks like. And then we're gonna walk through that tree looking for a particular name, pretending that we're the database. So taking these 10 names and putting an index on them, you get a structure that ends up looking like this. Maybe calling this a tree is generous. It kinda looks like a tree, but you can see at the top we have 2 names, Isaac and Steve.

That is the root node. Now, if we jump all the way to the bottom, you'll see across the bottom are all 10 names and importantly, those 10 names are in order. So it starts on the left at Aaron and then it goes all the way across to the right with Virginia, the last name in the tree. The bottom of this tree, these nodes down here at the very bottom, these are called the leaf nodes. So sticking with the tree metaphor, the thing at the very, very end, those are the leaves.

That's what's at the very, very bottom. Now, the leaf nodes are interesting because they contain the data. The the data lives in the leaf nodes and we'll walk through this in a second but also the pointer back to the table lives in those leaf nodes. Now, let's let's walk through this tree as if we're looking for the name Jennifer. So we're the database and we have been issued a query that says, hey.

In fact, let me just write this query out. Let's say, we have been issued a query that looks like this. Select star from users where name equals Jennifer. And using this index that we have created, we're gonna walk through how the database actually does that. Okay?

Let's go. Every tree traversal is going to start at the root node. So we have Jennifer. We look at the root node and we have Isaac on the left and Steve on the right. Alphabetically, Jennifer falls in the middle, so we're gonna take the middle path.

That leads us down to this interior node here with just the name Simon. So we compare Jennifer to Simon and Jennifer alphabetically is less than Simon and so we follow the left path of the tree down into the leaf node. And then once we're down in the leaf node, we just search for the name Jennifer and there is Jennifer. Let's do this again looking for the name Taylor. So we're gonna start at the root node again, and this time Taylor is greater than the right side of the node, which contains Steve.

So because Taylor is greater than Steve, we're gonna go down the right side of the tree. And then we hit a node that actually contains Taylor. And because it is equal to Taylor, we follow the right side of the tree again. And then down in the leaf nodes, we find what we're looking for which is Taylor. So what is the point of having an index?

It is cool. You must admit that it is cool. But what's the point? Well, the point is you want to avoid reading the entire table. So our alternative to having an index is something called a table scan, which scans the table.

It's all right there in the name. So if you don't have an index and you ask SQLite to find, the first name Taylor, it's gotta literally go and read every single row in the table looking for Taylors. When you create an index like this, it puts the names in order and it puts it in a structure that is easily traversable. So you can imagine that it's faster when you have 10 rows. That doesn't move me that much.

But once you hit a 1000, 10,000, a 1000000000 rows, you need this sort of so that you can skip over, you know, 0.9999000000000 and you just traverse the nodes to find exactly what you're looking for. So an index is that secondary data structure that maintains a copy of part of our data. And, in the next video, we'll see that it contains a pointer back to the main table.