MySQL case insensitive searching (don't use LOWER)
You probably don't need LOWER(first_name) to search without caring about case. MySQL's default collation is already case insensitive, and wrapping the column in a function hides your index from MySQL.
Everything I said
All right, you are building an application, and you've got a table full of people, and you wanna give your users the ability to search that table by first name only. But you don't really care about the casing. So if they're searching for the name Aaron, great name, you don't really care if they capitalize the first A or not. So what you do, what I've done, what we've all done, is we've wrapped that first name column in a lower function, right?
So we'll say lower first name equals lower Aaron, and that feels good. We think, "All right, we're no longer worried about case sensitivity." But there's a better way. One, you don't have to do that, and two, that could actually be harmful. So let's take a look at the reason you don't have to do that. Let's take a look at this people table. We've got select star from people, and you'll see that the best person is person number two, Aaron.
Now, let's take this, and we'll say select star from people where lower first name equals lowercase Aaron. Seems good enough, and we get all the Aarons back. So that seems like it's working, right? Well, it is working, but in this case, it's overkill. In this case, it's totally unnecessary. Watch this. So if we remove lower first name and we change this to...
let's change this to A-A Ron. Where is A-A Ron right now? So now we're just saying where first name equals big A, big A, little Ron, and we run that. You'll see that they all come back still. Um, okay, let's change this to A-A-R-O-N, like that. And they all still come back. So what is going on here? Why is this still working?
Well, this is where we need to talk about collations. A collation is a set of rules that decides how characters compare to each other. A collation is a set of rules. Let's take a look at the structure of this people table, and that'll help us understand collations a little bit better. If we run show create table people, and we'll bring this out here, so we can see it.
You'll see that the first name is a character column with a character set of UTF-8MB4. Now, the character set defines what can go in the column. It defines the set of legal characters. But that's not what we're talking about. We're talking about the collation. The collation is the set of rules that determines how the characters compare to each other.
In this case, the collation is UTF-8MB40900AI_CI. So it starts with, it starts with the name of the character set, and then it has a few modifiers out here. This is just referring to the O nine hundred Unicode weights. Unimportant right now. This is saying that, um, the collation is accent insensitive.
So an A with an accent is equal to a regular A. And what this is saying is that it's case insensitive, so a big A is equal to a little A. Every character column has a collation. You can see that the default collation for this table is AI_CI, and the collation for every character column in here is explicitly AI_CI.
So if we were to create a new character column in this table, it would take on the table default of AI_CI. Let's see if we can prove this out. I always like to test things in the smallest possible way. So let's do that now. Let's do select Aaron. Because it's pronounced Aaron. Okay, that looks reasonable enough. And if we collate it as UTF-8MB40900AI_CI, we still get Aaron back.
That's not very interesting. But let's say that we compare it to Aaron. That tells us, yes, that is the same. Lowercase, yes, that is the same. All goofy, yes, that is the same. Now, what if we were to say accent sensitive, case sensitive? Now it's-- now MySQL is telling us, "These are two different things.
These are two completely different things, because you told me that when I compare these characters, A is not equal to big A. Little A is not equal to big A." And we can do that even further by going just down to A. So and this is telling us that those A's are not the same. Those A's are the same. And if we change it back to case insensitive, you'll see that those big A and little, little A are now the same because we're telling MySQL, "Don't worry about casing."
And in the same way, if we put an A with an accent in there and run that, you'll see that it still comes out as equal. Even though one is uppercase, one is lowercase, one has no accent, one has an accent, it still comes out as equal because we've said it's accent insensitive. Now, if we were to change this to a lowercase, all right, that's still the same. But then we were to go back to say accent sensitive, now it's saying, "One has an accent and one doesn't, and those are not the same because you told me that I should care about accent sensitivity." Okay,
at this point, you may be thinking, "Aaron, I do not care. Collations are boring, and I just don't care about them. Lower first name always works, and I don't have to think about collations." Technically true. However, however, you may be accidentally totally hosing yourself. Let's take a look at why.
So if we do show indexes from people, I don't think we have any indexes on this table. We only have the primary key index. That's a great start. So if we say alter table people add index, and we're gonna add an index to first name. Great, now we have an index on first name. Let's prove that. Great, now we have an index on first name.
So we're good so far. Stay with me. Now, here is where you can accidentally totally mess yourself up. If we do select star from people where first name equals Aaron, great, that looks good. And if we explain that, you'll see we are using the first name key. So this is MySQL telling us, "Hey, you have an index on first name that I am going to consider." That's the possible keys.
"And in fact, I am actually going to use that key-" to satisfy this query. So instead of reading the whole table, it's gonna use the index, it's gonna find what we're looking for really quickly. This is what you're looking for. This is good. This is what we want. Now, watch this. If we were to say, "You know what? I don't care about collations. That guy on the internet, he's so boring," now, uh-oh, we're no longer using the index.
So if you're thinking, "I'm just gonna wrap it in a LOWER FIRST_NAME," what you're doing is you're hiding this index from MySQL. MySQL has an index on FIRST_NAME. You've now wrapped it in a function, and you're saying, "I wanna find things where the lowercase FIRST_NAME..." And MySQL says, "I don't have an index on that. You're on your own for that one." So again, if you take this LOWER away, we're using the index.
If you add the LOWER back, we're not using the index anymore. So this is why it matters. So even if you don't care about collations, you probably care about performance, and this is called index obfuscation, and you, you, you don't wanna do it. You never wanna obfuscate your columns if there's a way that you can easily work around it. Because if you had an index on FIRST_NAME, MySQL is not gonna use it.
This is a potential danger of using ORMs. I love ORMs. I think they're awesome. I think everybody should use them. This is a potential danger. You need to know what code, what SQL the ORM is producing, because stuff like this can get you in big trouble. You done messed up, A-Aron! Now you know when you might need LOWER FIRST_NAME, but more importantly, when you probably don't need to put LOWER around FIRST_NAME.
You learned a little bit about index obfuscation, and you learned about collations, which I'm sure your friends will find very interesting. If you liked this video, we have a full free course on MySQL at planetscale.com/mysql. It's me doing all the teaching for, I think, seven hours, 64 videos. So go over there and watch it, and until then, stay tuned to this YouTube channel.
See ya.