October 202336:0329.4k views

Easy database indexing strategies

About

Functional indexes and generated columns are a way to index things that aren't traditionally indexable, like JSON columns, data normalizations, or calculations.

Transcript

Everything I said

00:00

We all know that adding an index on a single column, very powerful, great way to speed up your queries. Adding an index across multiple columns, even better. But what if I told you there's a secret third thing that can take it to the next level, something the pros use to speed up even the gnarliest queries, and sometimes queries that you're not even in control of? We're gonna talk about functional indexes and their little brother, the generated column.

00:25

I'm gonna show you ten examples of functional indexes and generated columns and where you might use them. As you're watching me work through these examples, I want you to be thinking about your data and your domain and think about where these techniques might apply. The examples are interesting. The techniques are probably more valuable. Before I show you these examples, I'm gonna explain very quickly what a functional index is.

00:46

A traditional index you would put on a column or a set of columns, right? A functional index you put over the result of an expression or the result of a function. So instead of putting an index on, say, the price column, you could put an index on the price times the quantity plus the tax. So that would be a functional index.

01:08

We put the index on the result of a function instead of a single column. This means you can tailor your indexes to really specific and complicated query patterns. You can put an index on a calculation like we did, on a piece of data normalization or transformation, or you can extract a key out of JSON and just put an index on that.

01:29

So a lot of things that you didn't think were indexable before are indexable with functional indexes. In MySQL, functional indexes were introduced in eight point zero point thirteen, but if you're on an earlier version, there is a workaround, and that's to go through a generated column. A generated column is just like a functional index where you give it a formula, but instead of going straight to the index, it writes it to a column, or it puts it in a column, and then you can put an index on that.

01:58

So you have one extra little hop that doesn't really matter that much. So we'll talk about functional indexes, generated columns, how they're the same, how they're different, when you might wanna use one or the other, but let's talk about this first example, indexing the end of a string. Indexing the end of a string is really hard because that's just not how indexes work. So if I've got this table full of people here, and we read it out, and I wanna find people with a domain of planetscale.com, I will probably do it like this, WHERE email LIKE, and then I will put '@planetscale.com'

02:30

in there, and it'll come back with just me. So the question is, does this use an index? And unfortunately, it doesn't because we have the wild card at the beginning. We can prove this out further if we say ALTER TABLE people ADD INDEX to email. So if we do that, it's gonna add an index, and then we'll run this again, and it still works.

02:52

But if we hit Explain on that, you'll see that no key was used at all. Whereas if I change this to aaron@planetscale.com, it uses the email key. So you can tell that our index is not being used to find the emails that have a domain of planetscale.com. So while this is seemingly impossible, it is not impossible with a functional index.

03:13

So what we're gonna do instead is we're gonna break apart that string. We're gonna explode that string on the @ sign, and we're gonna put an index just on the domain part. And then we can do our lookups and our groupings and our orderings using that index to assist us. To add a functional index, it's very simple. You do the same ALTER statement that you would do for a normal index, and then you open your parentheses.

03:36

But instead of putting a column name here, which is what you would do with a normal index, right? Instead of that, you open another set of parentheses, and this tells MySQL, "Hey, I'm about to do a functional index, not a column index." And what we're gonna put here is we're gonna put a substring. So we're gonna, we're gonna look at that email column, and we're gonna try to pull out just the domain. Before we do that, let's see if we can figure out how to do that.

03:59

So if we say substring index from people, what we wanna do is we want to take a substring of the email column. We're gonna split it on the @ sign, and we're gonna go from the end and work our way backwards. And so if we do that, we see, okay, great. That's what's going to get us our email. So if we copy that out and come up here, we can put an index on the domain by running that.

04:23

That's gonna take maybe a second or so. Now, if we say SELECT star FROM people WHERE substring index email equals, now we can just say planetscale.com. So if we run that correctly, if we run that, we see that it's just me.

04:43

That's great, but did it actually work? And there you go. It used our functional index to satisfy this query. So we took something that was probably not indexable, and we added a functional index to break that thing apart and just index the thing that we're looking for. Okay, you're now ten percent of the way to being a functional index wizard.

05:04

We've got nine more examples to go, so let's just keep moving. The next one that we're gonna look at is case insensitive searching, and this is a tricky one 'cause I don't know if you actually need this one. Let me explain. So if we're looking in this table, SELECT star FROM people WHERE, and we say first_name equals Aaron. Good name. We look at that. That seems reasonable, right? But what if we were to say Aaron like this?

05:27

That still comes back just the same. But what you see is you see a lot of people doing lower first_name equals lowercase search string, and this is a pattern that you'll see kind of often, especially when being issued by some ORMs that maybe aren't that great. There are good ORMs, but bad ORMs might do something like this, and this can get you into trouble.

05:48

The reason that this can get you into trouble is because if you have an index on first name, when you wrap it in a function like this, that index is no longer usable. It's what's called obfuscating your index. So if we were to ALTER TABLE people ADD INDEX Add index on first name and we run that.

06:11

It's gonna take a second. And then if we were to run this again and explain on it, you'll see that it's not using that index. And if we were to remove this and explain it, you would see it is using that index. So it looks like we have two indexes on first name, and it's decided to use one of those indexes. But wrapping it in a function obfuscates the index.

06:34

So if you are not in control, if you are not in control of whatever is issuing this query, and you're the DBA, and you're thinking, "This query is killing us. This query is killing us. I cannot figure out, or I'm not in control of whoever's issuing this query," you can add a functional index on this and just say, "Fine, I know that this is stupid, but I'm gonna go ahead and just put an index on lower first name as well." And so once that runs, if we explain that again,

07:04

you'll see now it is using that functional index, even though in most cases you don't need this to do case insensitive searching. Why don't you need that to do case insensitive searching? I have a full video on that, but the quick answer is because the collation that is being used on this column is case insensitive itself, so you don't actually need to wrap it in a function.

07:28

Now you have indexed the end of a string and a lowercase version of a string, but let's take a look at example number three, indexing a string that is too large to index. So if you have a string that's stored in a text column and you try to put an index on it, MySQL is gonna tell you, you can't do that. You have to specify a prefix length 'cause it can only index part of that string.

07:49

But if you're looking for strict equality on a huge column, there is a way around that using obviously a functional index. So let's take a look at this logs table here. If we select star from this logs table, we'll limit it to 10. You'll see it's a pretty basic table with a URL column, and that URL column is a text column. So if we describe logs, you'll see it is a text column, so we cannot just alter table logs, add index to URL because it's too big and MySQL says you have to add a prefix.

08:20

So in our case, what we're gonna do is we're gonna add a functional index on it, and we're gonna say MD5 of the URL. And this-- what this will do is this will compress it down into an MD5 hash. And so if we take a look at this table again, we're gonna pull out example.com and now what we can do is we can say select star from logs where MD5 of URL equals MD5 of

08:50

the URL that we're looking for. Then that brings back the one record that we were looking for. If we throw an explain on that, you'll see that it is using the functional index. And if you are concerned about collisions, you can add a second condition down here and just do the actual full, uh, comparison to the full text column.

09:13

Because what's gonna happen is MySQL's gonna use that index to narrow down to just the few set or just a few rows that match that key, and then what it will do is it'll use the full URL to eliminate any false positives. So now we've indexed a field that's too big to be indexed normally. We've put a hash on it, and we can say, show me rows where the URL equals a certain value.

09:34

But unfortunately, we can't say, show me rows where the URL starts with a certain value. That won't be index assisted because we've hashed it, and we've lost a lot of that information. So it only works for strict equality, but it is very helpful for strict equality. I have a full video on that if you want to watch that more. But let's move on from strings and start talking about dates. More specifically, we're gonna be talking about date parts.

09:57

So if we look at this people table again, we see we do have a birthday column there. So let's look for people that were born in nineteen eighty-eight. We could do where year of birthday equals nineteen eighty-eight, and that works, right? All of these people were born in nineteen eighty-eight, so that seems like we accomplished our purposes. Eh, kind of. Like we talked about in one of the previous examples, when you wrap a column in a function, you obfuscate it, you hide it.

10:25

It is no longer going to use an index if you have an index on birthday. Now, what you may be thinking that we're gonna do is add a functional index on year of birthday. That would be fine. That would be totally fine and may fit your use case. But what you can also do is you can always try to unwrap those columns.

10:46

So whenever you can, you wanna move everything to the other side of that, uh, comparison and leave those columns alone. And that can be done here by saying where birthday is between, and we can say, what was it, nineteen eighty-eight, oh one, oh one, and nineteen eighty-eight, twelve, thirty-one. And we don't need to put any times on this because it is a date column.

11:10

So by doing this, we haven't added a functional index. We have given MySQL the opportunity to use an index on birthday by simply changing this to a range condition instead of obfuscating it with a function. So anytime you can do that, I would definitely do that. Don't reach for a functional index every time just because they're awesome.

11:31

They are awesome, but if you can rewrite the query to take advantage of a regular index, that might be a more widely usable index. So in this case, it was just a matter of rewriting the query to use a between instead of a year on the column. But sometimes that's not possible. And searching for people born in a certain month is that exact problem.

11:51

There's no way to rewrite this. So if we were to say, "I wanna find all the people born in February of any year," there's no way to unwrap that to convert that into a regular condition, so this is the only way to do it. So what we can do here is, of course, we can alter the table. Alter table people add index. We'll open our parentheses, and then we'll put inside there another set of parentheses, and we can say month of birthday.

12:19

And if we run that, now we have an index on month of birthday, and if we explain that, you'll see we're using our functional index. We're just adding tons of functional indexes to this table. We're up to functional index number three. So now we're able to search on the month of birthday, and that's kinda cool, right? You can extract a date part, put an index on it, and that may be helpful for some sort of filtering UI or searching UI or just some sort of query that you're running.

12:44

Now you can extract that date part and index it. Let's take this a step further. This is gonna be a bonus example, so you're gonna get two for one here. Let's take this a step further and add a little bit more semantics to that part of the date that we're extracting. Let's move on from that people table and look at this todos table. It's very basic. We've got a title and a due date and some timestamps that we don't care about.

13:05

If I want to define all of the todos that are due on a weekend day, what I could do is I could say, weekday of the due date is in one or seven, and that will tell me if it's due on a weekend. Now, what I could do, of course, is take this and shove it into a functional index and be super happy.

13:27

The question is, when my future self comes back to this, will I know what the heck I've done here? I'm always trying to be kind to my future self because sometimes my past self has done stuff like this, and I curse his name, and so I'm always trying to be kind to my future self. That's why I feel like this one would be better exposed as a generated column.

13:47

So instead of just shoving this into an index and hoping that we remember what this means or forgetting to use it, I think what we would be better off doing is saying alter table todos add column tinyint unsigned and add column, let's call this, is weekend tinyint unsigned.

14:08

And then what we need to say here is we need to say generated always as, and we can put our method or our function in here. So if we run that, now we can read this back. Select star from todos. We'll throw a limit on it. And if we read that back, we see we now have a proper first-class, uh, column that shows whether it's the weekend or not.

14:30

And I feel like this is really nice because we've given this formula a name instead of trying to remember, what exactly does that thing do? Now, the way that you add an index to that is pretty simple. Alter table todos add index to is weekend. So if we run that, now we have an index on the is weekend generated column.

14:51

Now, one of the really nice things about using a generated column is we can address it by name, right? We can say, is weekend, and it will use that index. However, we don't have to address it by name. We can still address it by underlying function, and MySQL is smart enough to look at it and say, "Wait, that looks familiar. I've got an index on it. You named it is weekend, but the underlying function is that exact same thing, so I'm just gonna use that index even though you didn't call it is weekend." Let me see if we can prove this.

15:19

We'll start by proving select star from todos where is weekend equals one. We'll start by proving that this guy actually works by hitting explain on that, and we see that it's using that is weekend key, which is great. That's what we would expect. However, what if somewhere in your application or some developer on your team forgets that there's this new fancy generated column and they say, "I'm just gonna do the thing that I've always done, which is use this weekday function." You're fine.

15:49

MySQL looks at it and says, "Eh, yeah, you gave it a pretty name, but under the hood, it's the exact same thing. I'm just gonna use the index here." So the nice thing about these generated columns is they give you that name, but they don't take away your ability to use the underlying formula. All right. That one was a little bit of bonus. You got two for one on that one. And before you start thinking, "Whoa, Aaron, he's so generous, he's so kind," one of the future examples doesn't actually work.

16:15

It's in there kind of as a red herring. I'm not gonna tell you which one it is yet, but to stay true to the promise of ten examples, I gave you two for one on that one. Let's move on to example number five. We're still gonna be working with dates and times. But instead of doing lookups, we're gonna use this one to do groupings. This is a little bit of a wacky example 'cause we're keeping a bunch of logs in MySQL instead of a logging tool, but I have seen a lot worse, and I've seen a lot weirder.

16:43

So let's just move forward with this. If we throw a limit ten on here, you'll be reminded that we have this created at timestamp over here. So we've got this created at timestamp, and what I wanna do is I wanna count these logs by the hour. I kinda wanna bucket them into chunks, right? I can do that by saying group by, and then I'll throw a date format on here, and we're gonna format the created at timestamp.

17:08

And the way that we're gonna do this is we're just gonna say year, month, day, hour, and drop off the minutes and seconds, and we can do that by saying year, month, day, hour, and then just, just manually set the minutes and seconds to be zero. And so if we do a count star of that, and we'll just copy this guy and throw it up here.

17:29

Now we see here's, here is a count of our buckets. So we see the number of visits per bucket, and that's kinda useful, right? You can imagine in your data where this might be useful. This is another good case for pulling it out to be a named generated column. So let's do that. We'll do alter table. What's the name of this table?

17:50

Oh my gosh. I've actually forgot the name. Logs. Table logs. This happens all the time. Alter table logs, and what we're gonna do is we're going to add a generated column. So we'll say add column. We'll just call it bucket, and we can say timestamp. And in here, we're gonna put our formula. But we need to say generated always as, and that will be, um, that will be our formula.

18:15

This is going to create that generated column. Now, if we read that back, select star from logs limit ten, we should see we have a bucket on there where it's been zeroed out. So alter, alter table logs, didn't forget it that time, add index to bucket. Select count star from logs group by bucket.

18:42

We should go ahead and add the bucket on there as well. And if we explain that, you'll see, great, we're using our new index on the generated column that has this nice name to it. And this time, instead of using the generated column or the functional index, whichever one, instead of using that for, um, filtering or equality lookups, we're using that for grouping.

19:06

Let's keep going with some of these date calculations. I'm liking these date calculations, and let's move on to example number six, where we calculate somebody's age and put an index on that. So to start, we'll do select star from people limit ten, run that, pretty it up. That's a good start. And then we're gonna use a function called timestamp diff, and we're gonna say, "Give it to me in years." And we'll use the birthday column and just right now.

19:36

And so if we scroll over here, we see that we do have the age. So let's just do first name, birthday, and age. That's a little easier to see. So now we have the age in years over here. So as we have been doing, let's alter table people, add index age.

19:57

We'll do double parentheses and throw this in the middle. And if we, we run this, we'll see this is the one that doesn't work. So I told you one of these doesn't work. This is that one. Y- we get an error here that says, "The expression of functional index contains a disallowed function." And so the problem with adding a functional index on this is that this now value changes depending on when it's run.

20:28

And so when you're creating a generated column or a functional index, it has to be what is called deterministic, meaning you get the same value every single time it's run. So to prove that, let me show you. We can't just create a functional index on now. Another one that we can't do is rand, because you can't put a functional index on something that changes, and the random function changes over and over and over, and so does the now function.

20:56

So this is the example I told you that doesn't work. So this one doesn't count. It must be deterministic. Sorry for leading you astray on that one. I did give you that bonus example, so hopefully we're still good. You still get your ten examples. Let's move on to the next one, which is data cleaning or data normalization or type juggling even. This is very common when you get a third-party data set from a vendor or supplier or somebody else.

21:22

You get a set of data that either needs a little bit of cleaning or just a little bit of juggling to get it in the format that you need. So in this case, we're gonna take a data set that comes as latitude and longitude, and we're gonna create another column where it's just stored as a single geometric point in the database. So if we look at this addresses table, select star from addresses, limit ten.

21:45

You'll see that we get this data in latitude, longitude format, which is totally fine. But you can imagine we want this in the native MySQL point type, which is a geometric type, because sometimes that can be more performant. So what we're gonna do is we first need to construct that point type. So if we go back here, select star. And then the way that we can do this is there is a function called point from text.

22:11

So we can create a point out of a text, um, out of a piece of string. And so what we're gonna do is we're gonna say concat. And then the way that you do this is you have a point, and you put in the longitude and the latitude. And so we're gonna use the concat function here, and we're gonna say concat that first part with the longitude and then a space and then the latitude.

22:35

And did that work? Yeah. That looks like the actual definition that you would write to get a native geometric point in MySQL. So you know exactly what we're gonna do. Alter table addresses, add column point, and we'll just call it point generated always as.

22:56

We'll open this up, and we'll put this guy in the middle. Now, one thing I'm gonna do differently here is I'm gonna make this a stored generated column. The default is a virtual generated column, but I'm gonna make this one stored so that it's actually written to the disk, so it doesn't have to be calculated every single time.

23:19

So now, if we run this, it's gonna take maybe a second or two or three or four, four seconds. It's gonna take four seconds across a million rows. So if we do select star from addresses limit ten, you'll see that we now have this point column that is based on the raw data that our vendor or supplier has given us. And so if we needed to, we could add a spatial index to this column now.

23:45

But regardless, we have taken this, this source data, and we've cleaned it up, or we've normalized it using that generated column, and then we can slap an index on top of that. I like that one because that's taking raw data that we've been giving, not really messing with the data, but adding something else of value to it. This next example is the same idea, except it's just a pure raw calculation.

24:07

So we're gonna take some columns that we've been given, do a calculation, and if this is a common access pattern in your application, you might consider doing the calculation ahead of time and putting an index on it like we're about to do. I've changed the addresses table just a little bit. So if we do select star from addresses limit ten, we'll see that we do have a square footage and a price that are totally made up.

24:31

But let's say that the common access pattern here is that users wanna search by price per square foot. So what we're gonna do is we're gonna alter the table and add a price per square foot column, and we're gonna make it generated always as. And then let's just say that we want to round this-- Actually, let's just make it a ceiling.

24:52

So we'll do ceiling of price divided by square foot, which then allows us to make this an integer column. So if we run that, that's gonna add to however many rows we have there. So select star from addresses. Let's do a limit ten. We'll see we do have the price per square foot. So if we were to alter table addresses, add index to the new price per square foot and run that guy, now we can select

25:22

star from addresses where price per square foot. You wanna go cheap or expensive? Let's go, let's go expensive. Is greater than six hundred and fifty dollars a square foot. And if we explain that, you see that the key that we have used is the key that sits on top of that generated column. Now, we didn't do this one as a stored generated column because, eh, we don't really need to, right?

25:46

We don't really need to because first of all, this calculation, very, very, very fast. Databases are good at that. But more importantly, we don't need to store it because it's actually stored in the index structure itself. And so this is not going to be calculated every time this query is run because it's going to go into that index and look for those values that have been stored.

26:09

So even though, even though it's not stored in the column, it is stored in the index. And so this is a perfect use case for a virtual generated column. We're getting close to the end, which probably makes you as sad as it makes me, but we do have two really good examples left. The next one is combining multiple status into a single composite status.

26:31

So the situation here, we've all been in it. Over time, the business requirements change, right? So you have a table full of posts, and you just look at published at, and then you add soft deletes. You're like, "Okay, well, it needs to be published and not deleted." Well, published and not deleted and not archived and not marked as spam and, and, and. And eventually, your query has these eight conditions just to see if the post should be visible or not.

26:55

So we're gonna wrap all of that up into a single column, and we're gonna look at composite indexes over some of these columns. All right, so here's the situation. Very common. Select star from posts limit ten, and we'll take a look at these columns. We've got is spam, published, deleted, archived, and then the other timestamps.

27:17

So what happens over time is you say select star from posts where published at is not null and archived at is null and deleted at is null and is spam equals zero. And this becomes your default scope or the query or the, the part of the query that you have to run no matter what query you're running, right?

27:42

So this is now, this is now your set of posts that should be visible. So let's wrap all that up into a single column. So let's take that, and we'll do alter table posts, add column, and we'll do, um, is visible as a tinyint unsigned generated always.

28:03

You could do this in your sleep now, right? Generated always as. And we're just gonna drop all of that in here. So now this is wrapping up all of those things into one single column. So if we run that and we do select star from posts and put a limit on it, of course, we see that we now have the is visible flag over here, which wraps up all of that business logic.

28:27

We could, we could put an index on that. We've been doing that. That's not terribly interesting. What we can do is we can use that as part of a composite index. So we can alter table posts, add index. We'll just call it composite. And what we're gonna do is we're gonna say is visible, but also we want to put, uh, we want it to be a two-part index that covers is visible and the views.

28:52

And so the situation here is we wanna, we wanna select some posts for the homepage. So if we run this guy, then what we can do is we can say select star from posts where is visible equals one and views is greater. What should we put on the homepage? Ninety thousand? That feels right. So if we run that, we get a hundred and fourteen rows back, and we explain that.

29:14

You'll see the possible key is that composite key, and the key that was chosen is that composite key. And so now a nice thing that we have done is we've compressed all of those columns into one single unsigned tinyint, and then we use that as part of an index, as part of a two-part index. We can go even further.

29:35

So this is kinda crazy. If we do alter table posts, drop index composite, so we'll drop that index out. Let's say we want to, we want to specifically optimize for this homepage post selection query. This can actually be a functional key part. So instead of saying views, we can say views greater than ninety thousand if we're very, very sure that this is the query that we're going to run.

30:03

So now we don't have an index on the is_visible and the views, we have an index on the is_visible and the views greater than 90,000. So if we run this again, we'll see that we're using that composite key again and the length got even smaller. This key is now much, much, much smaller because it's just two little integer guys instead of the views, which is a, a regular integer.

30:27

So this is one way that you can combine a bunch of stuff. We combined a bunch of stuff into this is_visible column, but then not only that, we added a composite index on top of it and then we, we took that second column, that views column, and made that a functional key part. And so now really we have two functional key parts because this is under the hood, this is a functional index, and on its face, this is a functional index.

30:53

At this point, if you're not in love with generated columns and functional indexes, I feel like I've, I've failed. They are incredibly powerful and incredibly cool and we have one more example that I think you can probably apply to your data today, and that is indexing a JSON blob. Now, more specifically what we're gonna do is we're gonna put an index on a specific key of a JSON blob.

31:16

So MySQL has first party JSON support with a JSON column, but you can't put a blanket index over a JSON blob. However, you can extract a, a key or a part of that JSON blob and put an index on that and that is what we're gonna do. So I've got some JSON data here and I put it in a table called JSON data.

31:41

So let's read that out and you'll see the first thing that we notice is this is a terrible, terrible table design, right? We have this email key, and that's the one that we're really after, but it's nested all down in this JSON blob. Now, sometimes you end up here accidentally, sometimes you end up here on purpose, and sometimes you end up here just because you've been working with this application for 15 years and things have changed.

32:06

Regardless, if you know that this should be a top level column, it's probably best not to shove it in JSON in the first place. But sometimes you have these JSON blobs that are totally fine to store in the database and later on you realize, ooh, I actually need to, I actually need to pull one of those keys out and search by it. That's the example we're gonna have here today.

32:29

I would recommend that anything you know should be a top level column, make it a top level column, but we're gonna save ourselves from that problem right here. The first thing is, how do we get that email out? How do we get that email out of that column so that we can put an index on it? We're gonna use something called the JSON unquoting operator. So if we use this double weird arrow sign and we do dollar dot, now we're saying from the object, here's the key path, and the key path is just email.

32:57

So if we select that, you'll see we do get just the email out. Now, why do I call that the unquoting operator? If we remove one of those little arrow guys and run it again, you'll see that it comes back as a JSON string in quotes. And so what we want is we actually want the JSON unquoting operator, which is this double arrow, which removes the quotes.

33:18

Now we're gonna do the same thing that we've always done and we're going to alter table... What is this called? JSON data, add column. We're gonna call it email generated always as. We're gonna put this in here and we're gonna make this one stored. So if we run that, I did something wrong.

33:41

I didn't declare it as a data type. 255 characters seems good, seems like a lot, but that is totally fine. So now if we select star from JSON data limit 10, we'll see we have the email as a top level column and the good news is we just, we can't change it. So if you try to change that to something, you'll see that MySQL tells us, "That is not allowed.

34:02

You don't own that column. I own that column." Eh, and that's what we want, right? We want it based off of that JSON data. So we're gonna do the same thing we've always done. Alter table email, add index to email. I should have called this JSON data. Alter table JSON data, add index to email. So that worked just fine.

34:24

So if we read this back, we can say, let's find my ... Still there. If we read this back, let's find this person. Select star from JSON data where email equals that person. That worked. And if we explain that guy, we have now put an index on a JSON blob, which everyone tells you is not possible, and they're kind of right.

34:47

You can't put it on a blob, but you can put it on a key part. And so now we have this generated column and again, if we, if we were to forget that that generated column has a name and we were to just do that JSON unquoting operator again, you'll see that it does use that key, that index again, because it's smart enough to know I've got an index based on this function.

35:10

Yeah, but you didn't call it by its name. But I don't really care about that. So that's it. You made it. You made it through all 10 examples, including the fake example where we learned some things. So I don't feel super bad about that. I would love to hear where you use these in your business. I read every single comment and I respond to most of them. Um, so I would love to hear what, what use cases did I miss?

35:30

Where would you use these? Where do you use these? And maybe I'll make a follow-up video of the best use cases that I missed here. I would love that. That would be so fun for me. So please let me know. You can find more at planetscale.com/youtube. I teach an eight-hour course on MySQL that's totally free, so go over there and check that out or stay tuned to this channel where we're always putting out more MySQL content.

35:56

Until the next time, see ya.

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