---
title: "Make your database queries 1000 times faster!"
url: "https://aaronfrancis.com/videos/make-your-database-queries-1000-times-faster"
path: "/videos/make-your-database-queries-1000-times-faster"
type: video
author: Aaron Francis
published: 2023-07-31
topic: Databases
description: "A demo of PlanetScale Boost. One button takes a query from 800 milliseconds to 18, and then I try it on a 28-million-row table where the same query takes 55 seconds. It isn't exactly a cache and it isn't exactly a materialized view: the result is updated when the data is written, so what you read is fresh."
keywords: [planetscale boost, query optimization, database performance, materialized view, caching strategy, planetscale, mysql, query latency, large datasets, fresh data]
youtube_url: "https://www.youtube.com/watch?v=sJVzvBLDqqw"
site_index: "/llms.txt"
keyword_index: "/llms/keywords.txt"
---

# Make your database queries 1000 times faster!

A demo of PlanetScale Boost. One button takes a query from 800 milliseconds to 18, and then I try it on a 28-million-row table where the same query takes 55 seconds. It isn't exactly a cache and it isn't exactly a materialized view: the result is updated when the data is written, so what you read is fresh.

## Transcript

**Aaron Francis:** I'm gonna show you how you can make your queries up to one thousand times faster just by clicking a button. And I don't, I don't use a thousand as a placeholder to mean a lot. Not, like, a lot faster. I mean, literally a thousand times faster. So I'm gonna show you how this works with PlanetScale Boost. And you may be thinking, "Okay, PlanetScale Boost is probably just a cache." Mm, it's not ex- it's, it's not exactly a cache.

**Aaron Francis:** You may be thinking, "I bet it's a materialized view." It's, it's not exactly a materialized view. It is instead a secret third thing, which I'll talk about in a little bit. I will tell you how it works, but first I wanna show you that it works. So I have a PlanetScale database here ready to go. And when it comes to PlanetScale, a, a regular database actually has quite a bit going on here.

**Aaron Francis:** So we've got our load balancers that are available across many zones, then our primary and our two replicas. So that is our simple database that PlanetScale has deployed for us. Now let's hop over to Table Plus, and let's poke around and see what is in the database already. For this demo, we've got two tables that we're working with, a repositories table and a stars table. It might remind you maybe of a large code hosting platform.

**Aaron Francis:** That's merely coincidental. So let's take a look at the structure of these tables. Let's do show create table repositories and take a look at that. The tables here are gonna be very simple, um, because the point is to, to watch this query execution speed. So the tables are very simple, and we've filled it up with a ton of data, but there's not a lot of interesting stuff here. We've got ID, name, URL in the repositories table.

**Aaron Francis:** That makes enough sense. We'll do show create table stars, and this is similarly very simple. You'll see that it's mainly just a table full of references to other tables. So we have the user that starred the repository and the repository that was starred. That's basically it. Here's the query that we're gonna be running. We're gonna select all of the repositories and aggregate the star counts and join that in.

**Aaron Francis:** So if we run this, you'll see that it does take a little bit of time, but we do get the results back. So we get the repositories with their URL and the star count. And if we run that again, you'll see that it looks like it's averaging about seven hundred and fifty, eight hundred milliseconds. So this is the query that we're gonna be using to test, but I don't wanna just run it over and over and over again, so I built a little demo to run it for me.

**Aaron Francis:** Here's our query again. I'm gonna hit save and run. And now what this demo is gonna do is this is going to run the query over and over for me, and it's gonna plot that response time on this chart over here, so we can watch it go up and hopefully go down when we hit this boost button. You'll see down here that we have the query, the results, and the query duration, which is still hovering around seven to eight hundred milliseconds.

**Aaron Francis:** I'm going to simulate activity, and what this does is this starts to churn through some stars. So it simulates users starring and un-starring things. You can see the star count over here is we've got about a million stars, and then it drops back down. So with this query running, what we're gonna do now is we're going to enable PlanetScale Boost, and it's going to give us up to a thousand times improvement by just hitting this button right here.

**Aaron Francis:** And we should see the query time fall off a cliff. So it drops down to eighteen, seventeen milliseconds, and if we disable boost, it's gonna go right back up. And you can see that this data is still live down here. This data has been live the entire time. If we enable boost again, it's gonna fall off a cliff, but the data stays live. We're not reading some stale cache.

**Aaron Francis:** These values are being updated when the data is written, and so when we read it out, it is fresh. Now, you may be thinking, "Hang on, let me do the math. Eighteen milliseconds, eight hundred milliseconds, that's not exactly a thousand times. That's more like forty to forty-four point four repeating times." You're correct. Shame on me for telling you I was gonna show you a thousand times and showing you a fifty X improvement.

**Aaron Francis:** How embarrassing. Let me show you a one thousand X improvement. The problem with showing a thousand X improvement is that first unimproved query is gonna be super slow, and it's gonna make for very boring television. But we'll figure a way around it. I do wanna show you that it does work. So I've run select counts from stars off camera because it took twenty-six seconds to run, and the number we got back was twenty-eight million.

**Aaron Francis:** So this is a different, this is a different database, same structure, but we were working with a million stars. Now we're working with twenty-eight million. And I just filled it up until it slowed down enough. So we've got twenty-eight million stars here. I'm going to run, I'm going to run this terrible query over this twenty-eight million row table, joining it all together, and it's gonna take a very long time.

**Aaron Francis:** So I'll try to sit as still as possible, but I'm gonna fast-forward so that we don't have to watch the whole thing. You'll see the time down here at the bottom. Okay, here we go. Fifty-five seconds. It finally finished. Hopefully, in the final cut of this video, I made it a lot shorter, but it took fifty-five seconds.

**Aaron Francis:** At a million stars, I think it was seven hundred milliseconds. At twenty-five million stars, it's fifty-five seconds. This is a great example of a query that maybe works in the beginning, but as you grow, it becomes just completely untenable and a showstopper unless you can click a button and make it a thousand times faster. We're gonna do that by enabling this boost cached queries variable.

**Aaron Francis:** This is what that button in the web UI was doing. It was just flipping a variable, telling PlanetScale, "Hey, run this one through boost." That's all it was doing. So the, the implementation on our side, on the application developer side, is very easy. We just say, "Please use boost for this query." So if we turn on boost, and then we run, and then we run this again, we're gonna see a thousand X improvement.

**Aaron Francis:** There you go. So we can run this again and again and again, and it's running through boost now. So instead of taking fifty-five seconds, it's taking something like 15 milliseconds. So that is PlanetScale Boost improving a query a thousand times. Now that I've shown you that it works, let's talk about how it works.

**Aaron Francis:** Here we are back on the dashboard with that overview of all of the infrastructure that goes into a single database. This is actually not what we're gonna look at though. We're gonna look at the Boost tab, and if we switch over to the main branch, you'll see something that looks familiar. This is our query. This is our query that we have been working with, and it has been selected.

**Aaron Francis:** I have personally selected it and said, "PlanetScale Boost, I need you to make this faster." I've done everything I can. I've got indexes. I know all about how to make queries fast, and listen, this one is just not going to be fast. And so at that point, I hand it over to Boost and say, "You gotta, you gotta help me out here." And so you can assign, you can assign certain queries to be boosted.

**Aaron Francis:** You can assign certain parameter values of certain queries to be boosted. We've got documentation on all of that, and so I'm not gonna show you how all of that works. But you can, you can come in here and find your slowest queries using PlanetScale Insights and say, "I need that one to be fast." Sure, maybe it's fixable. Maybe I could re-architect my entire application, or I could just hit a button.

**Aaron Francis:** Maybe I could just hit a button and make it go faster. You can assign a certain amount of memory per query. So you can say, "This one I wanna assign just a little bit because I'm maybe gonna add a parameter to that one, and this one needs to be a lot bigger." So you can assign it in chunks however you want. And then once you hit that button, once you hit that Add Query button, then Boost takes over, and those queries become almost instant.

**Aaron Francis:** Now, the question is how? How does that actually work? I have told you that PlanetScale Boost is not just a cache, it's not just a materialized view, but instead a secret third thing. The secret third thing that it is, it's a partial materialization engine. It's a mouthful. A partial materialization engine. If it were just a cache, we would really have to worry about cache invalidation of your query results.

**Aaron Francis:** If it were just a materialized view, it would be incredibly expensive to continually recalculate that as new data is coming in. If it's just a virtual view, well, we don't even really get anything in terms of performance. So it is a partial materialization engine, and importantly, the values in the cache, so it is storing that data in a cache, the values in that cache are being updated every time you write to the database, which is what makes those reads

**Aaron Francis:** so fast. So we need to talk about how that works just a little bit. We have a full blog post on the, the technical deep dive behind how this works, but I'm gonna describe it to you just a little bit. The first thing that we need to understand is that PlanetScale is powered by an open source database called Vitess. So Vitess sits on top of MySQL, and it does a lot of the work at the Vitess layer to figure out where's all the data, how do I go get it, how do I aggregate it, join it, that kind of stuff.

**Aaron Francis:** One of the things, one of the things that that Vitess layer does is it writes a query plan. So your SQL comes in, our stars query comes into Vitess, and Vitess looks at it and breaks it apart and says, "All right. I gotta figure out what we're doing here and where do I go to get the data." So it is ... One of its core features is writing a query plan.

**Aaron Francis:** Now, what we can do is we can, we can take that query plan and invert it. So we're writing the query plan to get the data out, right? We can invert it using a lot of very hard computer science. We can invert it and say, "How would the result of that query change should someone write data to the underlying database?" And so what we can do is we can subscribe to those changes, we can subscribe to those writes, and we can

**Aaron Francis:** change that cached value whenever someone writes to the database. So we take that query plan used to retrieve data, we invert it, and then anytime someone writes data, we can update the value in the cache, and then when you query a boosted query, it's instantaneous, and it's up to date because it's coming out of the cache, and the cache was updated at the point the data was written, not at the time that you're reading it.

**Aaron Francis:** So that is how PlanetScale Boost enables those queries to be so fast and have the same latency as a read replica. We're talking like one or two seconds instead of ca- uh, going to something in the cache that's not there and having to execute the query. It doesn't do that. The value is updated when the data is written.

**Aaron Francis:** I would encourage you, go read the blog post. The blog post is amazing. It's a very technical deep dive. We have a product announcement post, um, but we also have the technical deep dive on how all of this stuff is done, and it's incredibly interesting. It's got charts and graphs and data flow diagrams, all kinds of stuff that you will enjoy.

**Aaron Francis:** If you need to boost your queries by up to 1,000 times, go check out PlanetScale, planetscale.com. If you need a modern MySQL database platform, go check out PlanetScale, planetscale.com. See ya.

---

## Related on aaronfrancis.com

- [You don't always need JOINs](/videos/you-dont-always-need-joins) *(Video · 2023-11-22 · shared: database performance, query optimization, mysql)*
- [Year in review: 2022](/writing/year-in-review-2022) *(Article · 2022-12-30 · shared: database performance, planetscale, mysql)*
- [All the Frontends, Onboarding Nova, and MySQL Stuff](/podcast/all-the-frontends-onboarding-nova-and-mysql-stuff-4b092040) *(Podcast · 2022-01-27 · shared: mysql, database performance, query optimization)*
- [Easy database indexing strategies](/videos/easy-database-indexing-strategies) *(Video · 2023-10-26 · shared: mysql, planetscale, query optimization)*
- [Stop using COUNT(id) to count rows](/videos/stop-using-countid-to-count-rows) *(Video · 2023-08-07 · shared: mysql, database performance, query optimization)*

## Relevant Resources from Aaron Francis

- **[MySQL for Developers](https://databaseschool.com)** (overview on site: [/courses](/courses)): Full video course covering MySQL indexing, deferred joins, pagination, query plans, and schema design for application developers.
- **[Mastering Postgres](https://databaseschool.com)** (overview on site: [/courses](/courses)): Deep-dive video course on PostgreSQL indexing, query performance, JSONB, full-text search, locking, and production operations.

## Search aaronfrancis.com (2-Hop Static Keyword Index)

Pick the alphabetical shard matching any topic to find exact pages across all articles, newsletters, videos, talks, and podcast transcripts (every shard is < 40 KB):
[A-B](/llms/keywords/a-b.txt) · [C](/llms/keywords/c.txt) · [D-E](/llms/keywords/d-e.txt) · [F-G](/llms/keywords/f-g.txt) · [H-J](/llms/keywords/h-j.txt) · [K-L](/llms/keywords/k-l.txt) · [M-N](/llms/keywords/m-n.txt) · [O-P](/llms/keywords/o-p.txt) · [Q-R](/llms/keywords/q-r.txt) · [S](/llms/keywords/s.txt) · [T-Z](/llms/keywords/t-z.txt) · [Directory](/llms/keywords.txt) · [Site Map](/llms.txt)

Shards for this page's topics: `planetscale boost` ([O-P](/llms/keywords/o-p.txt)), `query optimization` ([Q-R](/llms/keywords/q-r.txt)), `database performance` ([D-E](/llms/keywords/d-e.txt)), `materialized view` ([M-N](/llms/keywords/m-n.txt)).

## About Aaron Francis (Current Context)

Aaron Francis is VP of Marketing and Community at [Laravel](https://laravel.com), founder of [Try Hard Studios](https://tryhardstudios.com), creator of [Solo](https://soloterm.com), co-host of [Mostly Technical](/podcast), and author of [Mastering Postgres](/courses), [High Performance SQLite](/courses), [MySQL for Developers](/courses), and [Screencasting.com](/courses). Full bio: [/about](/about) · Current focus: [/now](/now).
