December 202313:3921.9k views

Faster than a regular database index

About

Three ways to pull one day's signups out of 5.3 million rows: no index (2.5 seconds), an index on created_at (about 70 milliseconds), and a covering index, which comes out an order of magnitude faster again. All benchmarks are wrong, but some are helpful.

Transcript

Everything I said

00:00

All benchmarks are wrong, but some are helpful. Hopefully, these that I'm gonna show you today are helpful. We're gonna benchmark a couple of different indexing strategies over five or six million rows, and we're gonna see that one comes out to be an order of magnitude faster than the others. Anytime you see a benchmark online, you need to ask yourself, "What, what's the configuration that they're benchmarking?

00:22

What are the other conditions? What's the data set that they're using, and does that match what you're doing?" Benchmarks exist in this kind of void in the ether, and your application exists in the real world. So hopefully, what I'm gonna show you is directionally correct, but you will need to think about your data and your application and how this applies to you.

00:43

Let's start by just taking a look at this table. Select star from users limit 10. Pretty basic SAS setup, name, email, verified password, remember, and then a couple of timestamps. And if we do select count star from users, we don't need the limit, but it doesn't matter, 5.3 million rows in here. I did just record a video on how I seed this data, and I'll link it up in the description when it's out, so you can see how I seeded this data.

01:11

Here's what we're gonna do. We're gonna try to get a list of emails for people who signed up on the first day of the year. We're gonna benchmark three separate ways. One will be slow, one will be fast, and one will be even faster than fast. Looking at this table, you'll see that we don't have any indexes that are going to help us in this particular situation. There's nothing on created at here. So if we do select star from users where created at is greater than or equal to that first day and created at is less than the second

01:41

day. So this will get us all the people that signed up on or were created on the first day, and that's 12,777. That was too many sevens, but I'm gonna take this query into a command line application that I've written that will allow us to run it over and over and over and get an average. It's a pretty basic benchmark, but it's better than just running it once, right? That will also allow us to add the other queries in and run them all and average them all so we can get comparisons between each one.

02:09

Now, this is a Laravel command line application. That doesn't super matter. I just know and love Laravel, and it has these nice helpers for us, so we're gonna use it. So from the users table, we bind in both of those created ats that we were working with in TablePlus, and then we're gonna drop down to this benchmark class. So in this benchmark class, we can give it a closure, and it will run it many times and give us the average response.

02:31

We are going to eventually run many queries in here, but we'll start by just saying select no index because we're not using an index yet. And we'll do the PHP short closure, and we'll say query get. That's it. Um, we can pass in the number of times we wanna run it. Because this one is so slow, I'm just gonna run it two times.

02:53

And if we hop over to the terminal here, we can run PHP artisan app benchmark, and that's gonna take a few seconds to run 'cause it is running twice, and it is a very slow query. There we go. So it takes 2,500 milliseconds, which is 2.5 seconds. Great. That is our beginning. That is our, that is our baseline.

03:14

That is the thing that we definitely don't want to ship. Our first attempt at fixing this is going to be adding the most obvious index, which is an index on the created at column. That's the thing we're filtering by, so let's add an index on there. So we can do alter table users add index created at, created at. And if we run that, that is gonna take a second.

03:34

Over 5 million rows. We should see that that pops back. Now, let's just back up a little bit and run it here in TablePlus. You'll see we're already down to 100 milliseconds. I told you we were gonna do something slow, which was no index. This is, this is fast. We haven't yet gotten to even faster than this, but th- this is fast, right? This was the obvious thing to do. Let's take this and put it back into our benchmark command just so we can start to build up the different queries.

04:00

Because we've added the index now, this is actually just, we'll call it select star, and we can turn this up to run it 10 times since it's not taking two seconds every time. And if we check the benchmark here, it's gonna run it 10 times, and it averages out to 73 milliseconds. So that's already a lot better than 2,500 milliseconds, but we're not done yet.

04:25

We can go a little bit further. Remember what we're actually looking for, though. We're looking for a list of emails for people that signed up on the first day of the year. This is giving me flashbacks to school, where you answer the question, but it's not the question that was asked, right? We need to answer the question that was asked, which is, "Get a list of emails." To select only the email column, we're gonna duplicate this and pass through the email column here.

04:50

So this is gonna say select email instead of select star, and we'll change our pretty display label here, and we'll pad this guy out so everything lines up nice and neat. And we'll turn up the iterations to 25 just so we can hopefully get some more reliable, unreliable data. And if we run this, we'll see it is going to run each of those queries 25 times, and we get a difference.

05:13

That's great. We do get a little bit of a difference. I am unmoved by this difference. I'll be honest with you. 74 milliseconds, 64 milliseconds, this doesn't move me. However, this is a good illustration of the principle, select only what you need. You'll see that everywhere. Select only what you need. If this table were much, much wider with a lot more data or if my database and my server were further apart instead of on my local machine, you might see this difference go up.

05:39

So always select only what you need when you can. But this is not very interesting, right? This is a good practice, and you should do it, yes. Please select only what you need. But let's do something a little bit more interesting, for goodness sakes. We can add a better index to satisfy this particular query. Let me show you what it is.

06:00

This is the raw SQL version of that query. We've been running select email from users created at, created at, right? So h- what is a better index that could satisfy this particular query? Well, if we do alter table users add index We're gonna add an index on created_at and email in that order, and that order is very, very important.

06:24

And we will call this created_at_email, and if we run that, that's gonna take a little bit of time, and it's taking a lot of time. Now, let's add one in the reverse order as well. This is not the, this is not the index that you want, so if you're not paying attention, don't copy and paste this part. So we've added an index on created_at and email in both orders, and I'll tell you why that is important in a second, but I wanna show you that we've gotten a 10 times improvement in this query by

06:54

creating an index that includes email even though we're not filtering on email. Very interesting, at least I think so. This is where we left off, and now that we have this index, we can actually just run this again. I am gonna add a comparison query here of name, because there is something special about this email, and I will show you what it is. So we have the name as a comparison.

07:16

We have select star as a comparison, but select email is the assignment. That is the thing that we're trying to do. So if we run the benchmark again, we should see that select email is quite a bit faster, six versus 62 for selecting name and 73 for selecting star. Let's turn the iterations down a little bit so it's a little bit faster, and if we run that again, we see that it is a durable and repeatable benchmark.

07:43

It is 10 times faster to select email than to select name even though everything else is the same, right? Even though we're filtering on created_at, everybody's allowed to use the same index. So what have we done here? What's the technique? How can we apply this to our own data? Well, we have created, you and I together, we have created a covering index, and that is the thing that is making this so fast, a covering index.

08:08

But it looks like we just created regular indexes, right? We didn't add any special keywords or anything. A covering index is a regular index in a special situation. A covering index is in a special situation where it satisfies the needs of the entire query. It covers the needs of the entire query.

08:31

Let me show you the explain. Here's the fast version of our query. If we throw an explain on it, we'll see what the heck is going on here. Over here, at the very end, in the extra column, where MySQL just says, "Here's some stuff. Good luck with it," we see the words using index. That is the key. This would be better if it said using only index.

08:52

That would make a lot more sense. When it says using index, it means using only the index. This query never had to go to the table. It never touched the table, because in this case, the created_at_email key is a covering index. This index has everything that the query needs, and what the query needs is it needs to filter on created_at, and it needs to return email.

09:17

And both of those pieces of data, they happen to be in the index. And so the database went to the index, and it got everything it need and just gave it directly back to us without ever accessing the table. That's why the name version was so much slower, because the using index is gone. It's not an index-only query, and same goes for select star.

09:40

When you are doing the select star and the select name, it is using that created_at index, but for filtering only. And then once it filters down to the row IDs that match, y- it has to go back to the table to grab the name and the other data to then put together in a result set and send it back to you. But when it's email only, it does that filtering in the created_at index, and the email is right there next to it, because we created an index over both of those columns.

10:08

And I told you that the order of those columns is really important, right? In our query, we're only filtering on created_at, and so we wanna put that at the front of that composite index, 'cause MySQL cannot skip over a part of the index. So because we're only filtering on created_at, we put that at the front, and then we just throw email on the end as extra data.

10:31

In Postgres, there is a way to create an index on a few columns and then bring extra data along for the ride. There's no way to do that in MySQL, so the way that you have to do that is you wanna put your filtering columns up front in the composite index, and then if you're bringing extra data along for the ride to create a covering index, put that at the end.

10:53

There's one more thing I wanna show you about a covering index, one more trick. I love tricks. Remember, we're looking for using index down here, and I wanna push the boundaries of what we can select up top. If we add created_at, that's fine, right? That makes sense. The index is on created_at and email, so we can use created_at to filter and use created_at up in the select statement.

11:14

But what if we add ID? ID is not part of that index, so you think. ID is not part of that index, so the using index should go away. However, the using index does not go away. In fact, if we select only ID, the using index still remains even though the index is on created_at and email.

11:36

So if we add the email back, you'll see we're using the created_at_email here, and we're still using that index only. And that is because every secondary index in MySQL has the primary key embedded in it. It's appended to the end of the secondary key. So every index over email is actually an index over email and the primary key, which is usually ID.

12:01

So when we created an index over created_at and email, we actually created an index over created_at, email, and ID. So all of those fields are available to you to select without breaking out of that covering index. So that's your final trick for covering indexes. The fields that you indexed are in there, but also the primary key is in there, so you're safe to select that without breaking the covering index.

12:29

It is hard to design for covering indexes. They are regular indexes in special situations, and if you change this query even a little bit, it's no longer a covering index. And so I don't know that I would design my entire strategy around a covering index, but it is another tool that you can use that can give you unbelievably fast performance when you need it.

12:56

That's a great trick. We love covering indexes. Stick around to this channel. We've got a lot more videos about MySQL coming. Please subscribe and let me know what you wanna see next. See ya. I'm gonna... That was too many sevens. But I'm gonna take this... That was too many sevens. That was too many sevens, but I'm gonna take this query.

13:17

That was too many sevens, but I am going to take this query into the command line. That was too many sevens, but I am going to take this... That was too many sevens. That wa- that was too many sevens, but I'm gonna take this query into a command line. That was too many, that was too many sevens, but I'm gonna take this query into the comman- That was too many sevens, but I'm gonna take this query...

Databases

More videos on databases

All videos

Get the next one by email

I’ll send you some more. Not too many.

Join 7,500+ other readers.

Read past issues