---
title: "MySQL case insensitive searching (don't use LOWER)"
url: "https://aaronfrancis.com/videos/mysql-case-insensitive-searching-dont-use-lower"
path: "/videos/mysql-case-insensitive-searching-dont-use-lower"
type: video
author: Aaron Francis
published: 2023-06-02
topic: Databases
description: "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."
keywords: [mysql case insensitive, case insensitive search, mysql collation, lower function, mysql index, query optimization, mysql performance, database search]
youtube_url: "https://www.youtube.com/watch?v=ML24GRzXF9w"
site_index: "/llms.txt"
keyword_index: "/llms/keywords.txt"
---

# 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.

## Transcript

**Aaron Francis:** 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?

**Aaron Francis:** 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.

**Aaron Francis:** 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...

**Aaron Francis:** 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?

**Aaron Francis:** 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.

**Aaron Francis:** 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.

**Aaron Francis:** 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.

**Aaron Francis:** 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.

**Aaron Francis:** 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.

**Aaron Francis:** 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.

**Aaron Francis:** 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."

**Aaron Francis:** 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,

**Aaron Francis:** 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.

**Aaron Francis:** 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.

**Aaron Francis:** 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.

**Aaron Francis:** "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.

**Aaron Francis:** 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.

**Aaron Francis:** 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.

**Aaron Francis:** 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.

**Aaron Francis:** 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.

**Aaron Francis:** See ya.

---

## Related on aaronfrancis.com

- [Easy database indexing strategies](/videos/easy-database-indexing-strategies) *(Video · 2023-10-26 · shared: case insensitive search, query optimization)*
- [Don't hide your database indexes!](/videos/dont-hide-your-database-indexes) *(Video · 2023-07-03 · shared: query optimization, mysql performance)*
- [MySQL for application developers](/about/speaking/mysql-for-application-developers) *(Talk · 2023-04-20 · shared: mysql performance)*
- [Database performance for application developers](/about/speaking/database-performance-for-application-developers) *(Talk · 2022-09-14 · shared: query optimization)*
- [Is COUNT(*) slow in MySQL?](/writing/is-count-slow-in-mysql) *(Article · 2022-08-07 · shared: query optimization)*

## Relevant Resources from Aaron Francis

- **[MySQL for Developers](https://databaseschool.com)** (overview on site: [/courses](/courses)): Full video course covering MySQL indexing, deferred joins, pagination, query plans, and schema design for application developers.
- **[Mastering Postgres](https://databaseschool.com)** (overview on site: [/courses](/courses)): Deep-dive video course on PostgreSQL indexing, query performance, JSONB, full-text search, locking, and production operations.

## Search aaronfrancis.com (2-Hop Static Keyword Index)

Pick the alphabetical shard matching any topic to find exact pages across all articles, newsletters, videos, talks, and podcast transcripts (every shard is < 40 KB):
[A-B](/llms/keywords/a-b.txt) · [C](/llms/keywords/c.txt) · [D-E](/llms/keywords/d-e.txt) · [F-G](/llms/keywords/f-g.txt) · [H-J](/llms/keywords/h-j.txt) · [K-L](/llms/keywords/k-l.txt) · [M-N](/llms/keywords/m-n.txt) · [O-P](/llms/keywords/o-p.txt) · [Q-R](/llms/keywords/q-r.txt) · [S](/llms/keywords/s.txt) · [T-Z](/llms/keywords/t-z.txt) · [Directory](/llms/keywords.txt) · [Site Map](/llms.txt)

Shards for this page's topics: `mysql case insensitive` ([M-N](/llms/keywords/m-n.txt)), `case insensitive search` ([C](/llms/keywords/c.txt)), `lower function` ([K-L](/llms/keywords/k-l.txt)), `query optimization` ([Q-R](/llms/keywords/q-r.txt)).

## About Aaron Francis (Current Context)

Aaron Francis is VP of Marketing and Community at [Laravel](https://laravel.com), founder of [Try Hard Studios](https://tryhardstudios.com), creator of [Solo](https://soloterm.com), co-host of [Mostly Technical](/podcast), and author of [Mastering Postgres](/courses), [High Performance SQLite](/courses), [MySQL for Developers](/courses), and [Screencasting.com](/courses). Full bio: [/about](/about) · Current focus: [/now](/now).
