Faster database indexes (straight from the docs)
Creating faster composite indexes via hashed columns! To learn more about PlanetScale, visit us at planetscale.com.
Everything I said
If you don't spend a lot of time reading the MySQL documentation, you probably, you, you, you probably have a lot more friends than I do. Nerd. But you're missing out on a lot more tips than I am. So if we take a look at my personal copy of chapter eight of the MySQL documentation, that is actually not available to purchase, I got this printed and bound myself.
So if we take a look at, uh, this little note right here, as an alternative to a composite index, you can introduce a column that is hashed based on information from other columns. If the column is short, unique, and indexed, it might be faster than a wide index on many columns.
Interesting. MySQL is telling us that if you're putting a composite index over a bunch of columns, you might be better off if you hash all of those things together and put an index on that. Let's take a look. For this example, we're gonna be looking at the addresses table, and we've got about five hundred thousand rows in here across six good columns of data.
And the docs are telling us that instead of creating a very wide composite index, we might consider hashing all of those values and us- putting an index on that. And that will create a very small, very compact index that we can use for strict equality lookups. Before we do that, let's see what the docs are trying to have us avoid.
If we were to say alter table addresses add index, and then the columns that we wanna add the index on are actually all of them. So we wanna do an address lookup across every column here. So city, state, and ZIP code. So this is the index that we could create.
And this is what the docs are telling you you might avoid. This is what's called a composite index, but it's very wide because it's across six columns. Now, interestingly, if we try to run this, it's gonna say too big. The, the key is too big, so you can't, in this case, you can't even create the index because the index is too large.
So what can we do? We can do exactly what the docs say, and we can create a hashed column. A hashed column isn't anything special. We're just gonna take all of these columns and hash them together into a very small value. In our case, we're gonna use the MD5 function. Remember, MD5 is not safe for passwords.
It is a broken algorithm for passwords, but it's great for taking a bunch of data and making it really small. So we're gonna use MD5. You could use any algorithm that you want. You could use MD5, SHA-one, two, two fifty-six. You could even use CRC thirty-two. You could use anything that you want. For this case, MD5 works perfectly fine.
So we're gonna take all of those columns, put them together, compress them into a small value, and then index that. So the question is, how do you generate a hash here? We can do select MD5 of hello world, and MySQL is gonna generate that hash for us. Remember, if you hash passwords with MD5, straight to jail. Now, what do we want to hash?
Let's do select star from addresses, and we'll throw a limit on it. So if we do that, we've got all of our columns here. Let's start doing a little bit of hashing. So we can do hash of primary number. So now we're starting to see, well, we can just hash a different column. But this is not what we're looking for, right? We, we need to hash everything altogether to give us that really unique identifier that we can later look up.
So if we do hash of a concat, so we're gonna put some columns together here, primary, street, street suffix, city, state, a lot of columns, ZIP code. So now we have a much better hash representation of this entire row. We've taken everything that we wanna do this strict equality lookup on, and we've hashed it together.
We do have one problem, though, and that's that the concat function is not super great for this. So if we jump up here and we say, um, select concat Aaron planet scale, we get Aaron planet scale. Oh, that's great. If we say select concat Aaron null planet scale, we get null.
What's the deal with that? That's so frustrating to me that if you throw one null right in the middle, game over, you lose. So instead, we're gonna use, uh, concat WS, which is with separator, and that allows you to throw a separator in the middle, and it handles nulls.
So if you had, if you had a whole bunch of nulls here in the middle, but you also had, um, a valid text in here and then more nulls, it's okay. You're totally gonna be fine. It's incredibly frustrating that concat doesn't handle that, but we work with what we've got. So we're gonna put concat with separator, and you can make your separator anything you want.
In our case, we're just gonna make it the pipe operator. We're getting pretty close, right? Now, we've calculated the hash. We've calculated the hash for every row based on six columns worth of data. We've got one pretty compact column that we can use for our strict equality lookups. But it would be a huge pain if every time we updated or inserted, we had to calculate that hash, right?
Probably something that's gonna get out of date at some point, we're gonna forget about it. But instead of hanging out with friends, if we continue to read the MySQL documentation, you might come across something called a generated column. A generated column is a way that MySQL will keep those values up to date on your behalf. So we're gonna tell MySQL the formula to calculate this value, and then we never have to think about it again.
It's very, very cool. Let's copy this part out. That's the good stuff. We'll get rid of all that. And if we say alter table addresses, add column, uh, we can just call this hash. Looks like that's a keyword. We'll call this address hash. So what kind of column are we gonna put here? We don't need a varchar, varchar, varchar. We don't need 255 characters.
That much I am sure of. We only really need 32 characters because we know it's an MD5 hash, but in fact, we don't even need that. I'll show you that in a second. But what we're gonna do is we're gonna say generated always as, and then we're gonna put our function in here. So what this is telling MySQL is, "Hey, go ahead and create this column.
Here is the data type," which we'll talk about. Always, always, always fill it with this value right here. So if we run this, we'll see, great. All right. Okay. That looks like it worked. Select star from addresses limit, we'll just say limit 10. Now, the address hash is there, but we didn't populate it, right?
In fact, if I try to change it, MySQL is gonna say, "Sorry, change is not allowed. You don't own that column anymore." MySQL owns that column. It's not yours, which is honestly great news. We, we love that because I don't wanna be responsible for that. What do I mean when I say that we actually don't even need 32 characters here?
Well, we don't need to store this as character data at all. We can use another MySQL function called UNHEX to convert those characters into their binary representation, and we can make this a binary 16, which is even smaller.
There's no reason to keep the character data around. We just need the raw sequence of bytes. So let's do that. Address hash, and then we'll just say that this one's called address hash bin because it's a binary. And so then to take, to take those characters and turn them into binary data, we're just gonna say UNHEX.
This is the MySQL function that does that on our behalf. So if we run that, and then we run select star from addresses limit 10, we're gonna see something, my ASDF is still there. We're gonna see something that looks pretty similar or pretty weird here. It's still showing up as characters, and that's because this client, this TablePlus client, is seeing that it's binary data, and it knows that I, the human, do not read binary data, and so it's
showing it as hex data for me. This really is binary data, and it's smaller than the character column. So after all of that, let's go ahead and add an index. So alter table addresses, add index to address hash bin. While this index is running, we gotta figure out what, how do we actually use this thing?
We've been unhexing and hashing and adding indexes, but how do we actually use it? Well, when we're doing a strict equality lookup, what we're gonna do is we're gonna take the values that we're looking for and plug them into that same hash function that we used to generate those stored hashes. So if we come back to the addresses table, and let's look for this particular row right here, 8215 Winecup Hill.
So we're gonna say where address hash bin equals, and then we're gonna plug in the exact same function that we used to generate this column, this hash. But instead of using the column, we're gonna take those values that we're searching for. So this could be from the application or if you're manually writing the query, and you could, you would plug those in right here.
So we'll say San Antonio in the state of Texas, and the zip code is 78256. Now, with any luck, we will use that hash column to look this address up and get one result back via the index.
So if we run that, there is our one result, and if we explain it, we'll see that we did use that address hash bin index, which is very, very small and lightning fast. We've taken what could have been a very wide composite index across six columns that, in fact, was too wide to index, and we've compressed that down into a binary 16 column that is very, very small.
Now, this is only useful when you're trying to do strict equality across six different columns. We can no longer use this index to search for properties that are on Winecup Hill or in San Antonio because we've taken all that source data, and we've compressed it into something that doesn't, doesn't represent the parts any longer. So in the, in this scenario where you have strict equality across a bunch of columns, this is a great solution.
In the scenario where you need to search the individual parts, this is no longer a good solution. If you're worried about MD5 hash collisions, I think you have better things to worry about. I think the odds are the same as getting struck by lightning nine times, so you'd be better off worried about being struck by lightning four times than having an MD5 hash collision.
However, you can further refine this query by adding each individual part. So you can say and street number equals 8215 and street name equals Winecup. You could do that for each individual part, and that would allow MySQL to use that index on the hash to get to the, the one or two that have that hash and then use these further conditions to eliminate any false positives, which
I don't think there will be any at all. You don't always have to calculate a hash across multiple columns. This can be useful across a single column. If you have a column that is too big to be indexed, so that would be like a text or a blob column, and you're trying to do strict equality on it, you can create a hash out of a single column. If you have a URL column that's a text column, because it, it can truly be many thousands of characters long, you can't put an index on that.
But you can compress it into a hash, and you can do your lookups against the URL hash instead. Finally, I have to say this could all be done with a functional index. This can totally be done with a functional index. You don't have to go through the generated column, but functional indexes under the hood are implemented via generated columns.
So it doesn't super matter. I do sometimes like to see the generated column on the table just so that I remember that it's there. So if you wanna just skip the generated column and use the functional index, you can totally do that. I hope you enjoy that. I hope you find this useful. I read every single comment that y'all leave, so please leave a comment. Let me know what you would like to see next.
Until the next time, see ya.