Don't hide your database indexes!
Using a redundant condition is a technique to allow MySQL to use an index where it might otherwise be impossible!
Everything I said
I've got good news for you. You're still smarter than a database. Is there a super intelligent AI on the rise? I don't know. What do, what do I know about AI? All I know about is databases and drinking Diet Cokes, and I know a lot about those two things. And what I know is that you, as the human being, you're still smarter than the database. Listen, can you store more information in your head than a database?
No. But can you process the information more quickly? Also no. However, you do know more about the domain than the database does, right? You know the shape of the data. You know the thing you're looking for, and sometimes the database needs to be tricked or, or, or coerced or, or encouraged into doing the right thing, right?
So using our powerful brains, we set up these, these indexes, and we craft these perfect queries, and yet the database looks at it and says, "Meh, I'm not gonna use an index. I'm gonna scan the whole table." As the human, we can trick the database into doing the right thing in certain scenarios. So let me show you what I'm talking about. Let's imagine we've got a series of points that looks something like this.
It looks like a bunch of dots. We tell the database, "I'm gonna draw a circle," and let's call that circle A. All the points that I'm looking for must be inside of circle A. Now, I'm gonna draw a second circle, circle B, and the points that I'm looking for must also be inside of circle B. And so you're looking at this. You're looking at, you're looking at circle A and circle B and thinking, "Hang on.
Circle A is inside of circle B. What is the point of circle B?" Circle B seems redundant. It seems extra. It seems to not be doing anything. I could have just said, "Show me all the points in circle A," because circle B entirely encompasses circle A. This is a redundant condition, and sometimes this really, really helps MySQL use an index that would otherwise not be used.
Okay, we are back in our client where we feel warm and fuzzy and at home, and before we talk about redundant conditions, we're gonna talk about obfuscated indexes. I promise I'm not making all these words up. These are real words. They just start to sound weird when you say them over and over and over. We're gonna talk about obfuscated indexes. So if we do show create table todos here, so we have this real simple todos table, and we have ID, title, due date, due time, and then two timestamps
to keep track of created at and updated at. We have two indexes, one on due date and one on created at. I don't typically recommend storing due date and due time separately, but it serves our purposes very well today. So I've done it. I've stored it separately here. Okay, we have an index on created at, so we're gonna talk about obfuscated indexes first.
If we do select star from todos where year created at equals twenty twenty-three, what do you think is gonna happen? If we throw an explain on this, do you think it is going to use the index? If you guessed no, you are correct. It is not using the index. So we can see down here in the explain that the type is all, meaning it just scanned the entire table.
In fact, there were no keys that it could have considered, which is weird 'cause we have an index on created at, right? Meh, you've obfuscated it. You have hidden this index from MySQL. By wrapping it in this year function, MySQL looks at that and says, "Okay, you're asking me, you're asking me to filter on the result of a function.
I don't have an index on the result of a function. I have an index on a column." So if we were to unwrap this and say, instead of year equals twenty twenty-three, what if we were to say between twenty twenty-three, oh one, oh one and twenty twenty-three, twelve, thirty-one?
Now, what do you expect is going to happen here? If you guessed that it is gonna use the index, congratulations. You win a prize. Please click the thumbs up button. So what happened here was MySQL looked at this and said, "Yeah, no problem. I'm a database. I got an index on created at. I'm gonna go ahead and use that index." So if we look at the possible keys, it says the possible keys are created at, and the key that it chose was created at.
So the lesson here is an obfuscated index is an index where you have hidden the indexed column from MySQL. So what you wanna do is you wanna make sure your columns are always, um, untouched. You want to unwrap those functions when possible. However, it's not always possible to unwrap those functions, so let's talk about redundant conditions.
Let me show you a very basic and very ridiculous redundant condition. Select star from todos where ID is less than five. That's what I was looking for, less than five. Now, a redundant condition would be and ID is less than ten. This is the circle A, circle B situation, right? Circle A is here. Circle A is small, and circle B is big, and so why would we add circle B?
Well, like I said earlier, we probably wouldn't in this case. However, if we are looking for todos that are due in the next twenty-four hours, now we're getting into a little bit of a tricky situation, right? Because we have due date and due time stored separately. And so to look for the todos that are due in the next twenty-four hours, we have to combine those two things.
When you add those two things together, you get a condition that looks something like this. You have add time of the due date and the due time. So we're taking those two disparate columns, which you probably shouldn't do, and we're adding those together to make a single value, a single date time value, and we're saying, "Show me all of the ones where those two things added together is between now and one day from now." So this accomplishes our purposes, right?
This accomplishes the purposes of finding all the to-dos that are due in the next twenty-four hours. If we were to explain this, what do you think is going to happen exactly? It's gonna scan the entire table. It's gonna scan the entire table because we have an index on due date, yes, but that's not super helpful here, right? Because we've obfuscated it by saying, "Uh, I'm not looking for due date.
I'm actually looking for the result of this function of due date and due time," right? Add those two things together, and then it scans on that. This is a situation where you can't easily unwrap, you can't easily de-obfuscate, um, this column to make the index usable.
So instead, what we're gonna do is we're gonna draw a circle B, right? This is our circle A. This is what we're actually looking for. This is, this is the set of points that we're trying to find. This is circle A. What we're gonna do is we're gonna draw a circle B around it, and this will be our redundant condition.
So this is our redundant. This is our bigger circle that necessarily includes circle A, but I've been telling you, I've been promising you that this will make sense, and this is where we get to show that circle B can use an index when circle A can't. And what might this redundant condition look like?
Well, we wanna leave due date alone, right? We wanna leave due date alone, and this, these bounds need to be logically bigger than circle A. And so we're gonna have something like this. We're gonna say due date is between today, the current date, and tomorrow. So this will give us a few to-dos that are in the past, right?
A few that are today, but have already passed, and a few that are tomorrow, but are outside of twenty-four hours, right? So this is our circle B that's broader. This will bring back a few false positives, but then the database will use this to eliminate those false positives. But what we're doing is we're using that index to chop off a huge amount of information that we know is never gonna fit inside of circle A.
We, we narrow it down to circle B, and then the database just eliminates the difference between circle A and circle B, right? So if we were to run explain on this, look, now we're using an index on due date, right? So we're using an index to get, whoop, all the way down to just the ones that are due today or tomorrow. Some are already overdue, and some are due outside of twenty-four hours.
And then the remaining circle A is filtered out on this condition, which is more expensive and not able to be indexed. Let's do another one. Let's say, um, let's say we wanna do select star from to-dos where modified or is it updated? Where updated at is less than 2023-01-01. So we want to find to-dos that haven't been updated since the beginning of this year, right?
And if we were to explain that guy, we would see, man, we're scanning the table again, 'cause remember, we only have an index on created at, not on updated at. This one is a little bit different. This is where you can use your human brain and say, "Okay, according to the way that my application is set up, the way that these records are created, I know that a record cannot be updated before it's created," right?
A record cannot be updated before it's created. So what we can do is we can say, we can say that this is our circle A. This is the one that we're actually looking for, but we can narrow it down with our redundant condition. We can narrow it down and say, "Well, I'm gonna also include it has to be created before 2023-01-01." Why?
Because for it to be modified before that date, it must be created before that date. And that's where you get to determine, based on what you know about the domain, you get to determine what a reasonable redundant condition is. You can find these in your application, but you as the developer have to decide, is this logically correct?
Is this circle B necessarily bigger and always containing circle A? If so, you found a redundant condition, and it might be helpful. Redundant conditions are awesome, right? It's our last moment in history to be smarter than the machines, and we're using that time to optimize queries, as we should. I don't use redundant conditions everywhere, obviously.
If there is something that you're querying a lot, it's a regular access pattern, it's better to add a real index to that thing. You can even create an index on the result of a function. You can use a functional index in MySQL. But redundant conditions are great when changing the schema is out of scope. Maybe you're not in control of the schema, or it's a query that you run not very often, and you just wanna make it a little bit faster.
That's a great use case for a redundant condition. If you liked this, we have tons of MySQL videos at planetscale.com/mysql, or you can just subscribe to this channel. See ya.