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
Advanced JSON
JSON aggregates

Full Course

$
179
$229
USD, one-time fee
High Performance SQLite is more than just an SQLite course, it is a general SQL masterclass. The course has unlocked new breakthroughs for query performance in my day-to-day, it's a great level-up!
Eckhardt Dreyer
Eckhardt Dreyer

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 this video, I learned about JSON aggregation functions like JSON group array and JSON group object, which help transform table data into JSON arrays or objects by grouping values together. These functions allow me to organize products by category, turning product names into JSON arrays, or compress employee data into JSON objects with names and salaries as key-value pairs. Using these JSON functions can offload data manipulation to the database, making it efficient to handle data before it's handed back to the application.

Video Transcript

(keyboard keys clacking) We're gonna look at some JSON aggregation functions now. There are two, but there are really four, but they're like kind of the same. So we have JSON group array and JSON group object. And so that is when you have a table and you're grouping by something, you can turn some of those values into a JSON object or a JSON array instead of maxing or summing or you know, concatting. We can turn those into JSON objects. So I said there are two, but there are really four. The other two are just the JSON B versions of the same thing. So you have JSON group array, JSON B group array, JSON group object, JSON B group object. We're gonna start with JSON group array. If I do select * from products, I have five products in here, not terribly interesting. Three fruits, two vegetables. Now, if I were to do select category, and I want to, I wanna group it on category, but I wanna retain the names of all the products, but I don't wanna do a group concat with a comma separated or pipe delimited or anything. I want some actual JSON. So JSON group array. And we're gonna say name, that's it. From products group by category. There you go. Category fruit, category vegetable, and a proper, honest to goodness JSON array with their names in there. This is cool. We can get a little bit fancier and we can say, let's say JSON objects. Ooh, it's all starting to come together. So let's say the key is name and the value is name, and the key is price as a string. And the value is price pulled out of the column. And so if we were to look at that now, that's potentially even more interesting. We've got a full on proper array. And inside each array or each array item rather, is a JSON object that has the name and the price. This is a very cool implementation of JSON group array. So what we're saying is, as SQLlite, as you're grouping this, as you're squishing all of these rows together, every time, why don't you create a new JSON object and here's how you do it. Key name, value name, key price, value price. And put that into this group array that we are running. The next function that we're gonna look at is JSON Group objects. So let's start by saying select * from employees. I got another dinky little table here, not terribly interesting. There are five people in there. So if we did select JSON group object and we put name and salary in there from employees. Now we didn't really group by anything. So the entire table is just being compressed down into one value and you see that the key is the name. So that gives us Alice there and the value is salary. And so that has created this key value structure. So we've basically compressed this table into a JSON object. Let us do something a little bit different. We can say select department, if you can spell department and we'll do this as employee. Yeah, that's fine, employees, from employees group by department. And if we do that, that's a little bit more interesting. So we have, engineering has Alice at 75 and Charlie at 80. Marketing has Diana and sales has Bob and Eve. And so this is similar but different, it creates a JSON object. It's all right there in the name. So if you want to, if you wanna group it down into an array, you've got JSON group array. And if you want to group it down into an object where you control the key and the value, you can do that with JSON Group object. Remember that you have JSON B variance of both. If you're continuing on with your processing or putting this back in the database, we were actually looking at it and in this fictional example, probably handing this back to the application to do some work. And so we went with the plain JSON versions. So this is a very cool way to do a little bit more heavy lifting on the database side. Of course, you could bring all the data back and then iterate over it in your favorite programming language, but databases are really good at data. And so I like to push as much of the data manipulation down to the database as possible and keep all my business logic in the app layer. That's just kind of a preference that I have. Let the database do what it's good at, let the app do what it is good at. And I find that the database handling some of this aggregation, turning it into JSON, it's a pretty good use case for it. So JSON group object, JSON group array.