Stop using COUNT(id) to count rows
COUNT(*) is not slow. It's the fastest way to count rows in MySQL, because the star lets MySQL count however it wants, and it picks the smallest secondary index. COUNT(id) ends up doing the same thing anyway.
Everything I said
Y'all, counting is so easy. I've been able to do it for several years. One, two, three, four, five. I could keep going. I could use two hands if I needed to. It's easy, but there's this one myth about counting in MySQL that just refuses to die, and that's the myth that COUNT * is the slowest way to count.
You shouldn't use COUNT *. You've heard it before. You've heard somebody maybe review your code or your SQL before and say, "Whoa, whoa, whoa, whoa. Hang on there, friendo. Don't use COUNT * because COUNT * looks at allll the columns in the table, and that's really, really slow. So don't process all of that data. You should just use COUNT ID." Well, friends, that is actually a myth.
It's not only a myth, nothing could be further from the truth. COUNT * is specifically optimized to be the fastest way to count rows in MySQL. Okay? It's the fastest way to count rows. Let me prove it to you. Here's where I think this myth comes from. If you do SELECT *, you are actually pulling back all the columns from the table, and some seasoned, grizzled veteran is gonna come along and say, "Listen, don't
use SELECT *, select only what you need." That's great. I love that advice. Select only what you need. It's good advice. The grizzled vets say it for a reason, because it's good advice. Now, this is where people get confused. SELECT *; SELECT * means all columns, right? So if we were to see SELECT COUNT *, what do you think this star means?
I think it means that, too. I think it also means all columns. I've seen the star before. SELECT * brings back all columns. I'm seeing the star again, COUNT *. I think that means all columns. Well, unfortunately, that's not what it means at all. What this star means is basically it means count anything you can as fast as you can.
You are ceding control. You are giving control over to MySQL to say, "Hey, man, I just wanna, I just want you to count the rows. I really don't care how you do it. Count whatever you want. Just make it fast, please." That's what the star means here. It's not , it's not quite as short, it's not quite as pithy.
It just means count anything. Watch this. So if we say SELECT COUNT * FROM... Let's do FROM todos. We have this table called todos. I'll show you the structure in a second, but I think it's interesting to run this before I show you the structure. So if we explain this, we'll see, okay, we d- we said nothing about due date, right?
We didn't say anything about due date. I didn't even tell you that that column existed. But when we run SELECT COUNT FROM todos, MySQL says, "Hmm, very interesting indeed. I'm gonna use the due date key to satisfy this query." And what's happening here is MySQL is looking at this table and saying, "Okay, I understand their intent.
Their intent is to count the rows. The fastest way to count the rows is to pick the smallest secondary non-null index." Whoa, lot of words. Let's say that again. The smallest secondary non-null index. And in this case, it is the due date index.
Let me show you the structure of this table. Show create table todos, and we'll pull this out here. And if we take a look at the structure of this table, we have a key, we have the primary key, which is ID, and then we have a key on due date, and we have a key on created at. So we have three indexes: ID, due date, and created at.
Created at and due date are what's called secondary indexes, which just means not primary. That's all it means. So the primary index is the ID, and in MySQL, the primary index is how the rows are actually arranged on disk. It's also called the clustered index. And so traversing that entire index, reading through that entire index, getting from beginning to end on that entire index
can, m- can, can take a lot of time because you have to get through all of the data in the entire table because the primary key is the clustered index. It is how the index is arranged on disk. Secondary keys aren't that way. Secondary keys are very, very compact on disk because you're only storing that information.
You're only storing that due date or that created at. And so to read, to count every item in that index is very, very fast. And so what happens is MySQL says, "All right, I know that you wanna count rows. I've got a secondary index that is very compact on disk. It's very compact, and so I'm just gonna count all of those entries in that secondary index." So let's watch this again.
Let's go back to the beginning. We'll say explain select count star from todos, and remember that we see the due date, right? Now, if we were to change this star to ID, do you think it's gonna use ID? In fact, it's not going to use ID. This is the, this is the case of MySQL protecting us from ourselves. The optimizer steps in, and as it's building out this plan, it looks at it and says, "Hmm, I think I know what you're trying to do.
You're trying to count the rows. You're trying to be clever, but you're trying to count the rows, and I have a better way to do that. I'm not going to use ID like you requested because I know that logically I will get the same answer if I count due date, and I can count that much quicker 'cause all that data is together on the disk." So if you use COUNT *, it goes to due date.
COUNT ID, it goes to due date. Count of a constant, it goes to due date. So we can finally dispel this myth that COUNT * is slow. So over Thanksgiving, when you're arguing with your family about whether COUNT * or COUNT ID is faster, you can show them this video, make sure they subscribe, and you can prove to them that COUNT * is not slow.
And in fact, if you really want to put your uncle in his place, you can tell him the docs recommend that you use COUNT *, and it's gonna be a great Thanksgiving. You'll have won the argument, which is what the holidays are all about. So hopefully this puts to bed that myth about COUNT * being slow. If you liked this, we've got a lot more videos.
Go to planetscale.com/mysql or stay tuned to this YouTube channel. See ya.