---
title: How I add millions of rows to MySQL (for performance testing)
url: "https://aaronfrancis.com/videos/how-i-add-millions-of-rows-to-mysql-for-performance-testing"
path: "/videos/how-i-add-millions-of-rows-to-mysql-for-performance-testing"
type: video
author: Aaron Francis
published: 2023-12-02
topic: Databases
keywords: [mysql, performance testing, database seeding, random data generation, planetscale, bulk insert, load testing, database optimization]
youtube_url: "https://www.youtube.com/watch?v=ywp9Z0xZPzo"
site_index: "/llms.txt"
keyword_index: "/llms/keywords.txt"
---

# How I add millions of rows to MySQL (for performance testing)

## Transcript

**Aaron Francis:** I make a lot of videos on this channel about databases, and queries, and indexes, and MySQL, and performance, and I'm gonna show you today a little bit behind the scenes of how I fill up my local databases with enough data to be interesting, and how I do it quickly and repeatably so that I can blow the database away and rebuild it back from scratch. I'm working on a video about covering indexes, and I needed a lot of data.

**Aaron Francis:** So we're just in this videos/covering directory, and I have a standard Laravel application running here. Laravel is a PHP framework. Yes, I know the jokes about PHP. No, I don't really wanna hear them. Yes, I really do like PHP unironically. So with that out of the way, let's keep moving. Usually, when I'm building a real application, which this is not, I would say php artisan make model, and let's just make a user model, right?

**Aaron Francis:** And I would pass in that I also want to create a migration, which is where I'm gonna set up the table definitions, the columns, their types, sizes, indexes, that sort of thing. A factory, which is where I can set up different model states. So I can say verified user, paying user, free user, and I can set up all of those, those states for seeding local data. And then S is gonna let me create a seeder as well, so I can take those factories and use them in a seeder to fill up the database with a bunch of stuff.

**Aaron Francis:** We're not gonna do any of the factory or seeder. We're gonna drop down a level and go a little more intense so that we can fill up this database a lot more quickly. And in fact, Laravel comes with a user model and migration, so we don't even need that. It's already made for us. What we're gonna do instead is we're gonna use my shorthand, AR, PHP Artisan make command, and we're gonna make a command called seed.

**Aaron Francis:** Now, if we run Artisan again, and we see all of the commands that are available to us, and if we look for the one we just made, we see that Laravel has that DB Seed command. That's the one that it uses to seed the database. We're gonna write our own because we're doing something a little bit outside of the box here. So we have our App Seed command here. Let's switch over to PHP Storm and look at that.

**Aaron Francis:** What we need to do here is we need to set up a bunch of data that's relatively evenly distributed, because if we have all of our users having the same created at timestamp, then that's not necessarily indicative of what a real-world application may look like. If we look in the project directory, you'll see in the database folder, we do have the user factory, but we also have the user migration.

**Aaron Francis:** Whenever I'm making a video, I always take the time to write out the full migration, because I wanna be able to just blow it away and start over if I'm trying to show two different things or if I've just completely messed something up, which has happened. Let's look at the user factory. This is the interesting part. So this is the one that Laravel made for us, but we're just gonna yoink this and take it over to our command.

**Aaron Francis:** Importantly, it does have this fake method, and this fake method is a, a wrapper around the Faker library, and this gives us all kinds of good stuff. So you can see name, number between, numerify, opera, whatever that means. I don't actually ... Oh, user agents. That's a user agent that it's giving us. Um, color name, city suffix, company email, company suffix.

**Aaron Francis:** So this library is the crux of me generating pretty reasonable, pretty realistic data that we can then test our queries against. We're gonna take that definition that we yoinked and just paste it in there. We don't need these nice, pretty comments. Email verified at now, not random enough. We gotta make this more random. So let's sub hours between zero and 9,999 hours.

**Aaron Francis:** So this is gonna change that timestamp to be more realistic, and so that they're not all on the same minute, let's do the same thing, sub minutes. This is going to introduce a problem, and I'll show you what it is later. Uh, let's also do that with created at, because by default, the database is gonna set that to a timestamp of now, which again, we want to be more evenly distributed.

**Aaron Francis:** If you needed a little bit more control over the dates, let's say you wanted to generate dates between the year 2000 and today, Faker does have a date between function, but this is good enough for now. Now, when it comes to password, I'm actually gonna do the opposite of what I just said. So by default, it looks for a static password. Otherwise, it hashes the word password. That's fine.

**Aaron Francis:** However, hashes are created to be expensive. This is a bcrypt hash. And so if I'm trying to seed five million records and I'm bcrypting a string five million times, that's gonna be really slow. So I'm just gonna hard code the word password in there because this is a seeder. Don't do this anywhere else, please. I don't wanna get in trouble for you doing this somewhere. This is looking pretty good to me.

**Aaron Francis:** We have a fake name. We have a fake, unique, safe email. Safe email just means example.com. Doesn't really matter. We could change it to company email since we're not sending emails from this application. Unique is kept track of in this single process, so when we do start spawning multiple processes, which we'll do in a second, we'll have to keep track of uniqueness on our own. The email verified and created at are evenly distributed.

**Aaron Francis:** Password we just hard-coded. Remember token, doesn't matter. That's great. Let's do a user create. And in here, we'll just put all of these attributes that we have been working with. We'll make this a little bit prettier. And one of the nice things that we can do now is we can switch over and say, Artisan migrate fresh.

**Aaron Francis:** That's gonna blow away the database. Love to see that. And then we can say, Artisan app seed, which is our command, and we should see one and now two records in the database. And there you go. So you see, we've got the name, we've got the email that is just, kind of, random. Verified at and created at are spread out evenly, and then password we hard-coded.

**Aaron Francis:** This is all well and good. I'm happy with the definition. I think this is gonna work, but boy, do we need to go faster. We must accelerate. I can't be doing this one by one. So now we're gonna look at doing it in a batch of 10,000 and then spawning multiple processes to hammer the database so that we can get millions of records in there as quickly as possible. Okay, but let's start by extracting this guy out into a method that we will call insert, and then we'll open a for loop and put that inside of it.

**Aaron Francis:** And then I think we're gonna do batches of 10,000, so that seems right. And then I'm gonna do just a really simple status indicator. I could do a progress bar here, but seems like overkill, and we won't be able to use it later when we start spawning multiple children processes. So this should insert 10,000 records, but I bet, if I had to guess, we're gonna run into an issue.

**Aaron Francis:** So if we run app seed, you'll see we are inserting... Great. There we go. There's a problem. Okay, so incorrect date time value. Whenever you see an invalid date time like this that falls between the hours of 2:00 AM and 3:00 AM, what has happened is you've generated a date time that does not exist. So the hour of 2:00 AM on this particular date doesn't exist because of daylight savings time.

**Aaron Francis:** Now, if this were a real app, I would debug the time zone issues. This isn't a real app. And so to cover this and potentially inserting a duplicate email key while seeding, we're just gonna wrap the whole thing in a try, in a, in a try catch block, and we're just gonna power through it. So we're gonna wrap this entire thing in a try catch block and catch literally every exception that comes out and just eat it.

**Aaron Francis:** Now that, that's how you do error handling. I want this command to spawn copies of itself so we can go super fast. So we'll say processes by default equals zero, and then we'll come down here and say that processes equals this option processes, and that's gonna come in as a string, and so we'll throw that on an integer.

**Aaron Francis:** And we're just gonna dump it out and see if that works. We'll clear that out, and we'll say app seed zero. Okay? So by default, and then app seed processes five. Great. So now we can control how many child processes this command is going to spawn, and that way we can really start hammering the database.

**Aaron Francis:** If there are processes, we're gonna assume that this is the parent command, and so we will return early and spawn the children processes. Otherwise, it'll fall through and start inserting 10,000 records. Now, we need to know how many processes to spawn, so now we go down here and say public function spawn, and we will accept the processes.

**Aaron Francis:** And what we're gonna do down here is we're gonna use Laravel's pool. Uh, w- it, Laravel has a process facade, and you can call pool on it and then pass through a closure, and it will take the pool, pool, and we can start adding the commands. So we can say pool command PHP, PHP artisan.

**Aaron Francis:** What is the name of this one? It's called app seed, and we're not gonna pass through the processes because this will be a default zero because this is a child process. So let's give it a runtime, uh, what is it? Timeout of five minutes, 'cause we could end up doing a lot here. So this is creating a pool of processes.

**Aaron Francis:** So far, we've only put one process in it, but instead of putting one process in it, now we're gonna fill it up with as many processes as we've requested, and we will run those, um, we'll run those in parallel so that they all start to seed the database at once. And to do that, we're just gonna do a simple for loop i equals zero. I is less than processes, which is not in scope.

**Aaron Francis:** I plus plus. We'll wrap that up, and now we're spawning that many processes. But we need to bring this into the closure PHP thing. I love PHP. Have I mentioned that yet? Now, we should have exactly what we're looking for, but at this point, we've just created the pool. We actually need to run it. And so we're gonna say that it should start, and in fact, we will just wait for it all to finish, 'cause we're not doing anything else here.

**Aaron Francis:** This is the entire point. Now, we should be able to switch back over here and say processes five, and I think our batches are 10,000. So if we run that, it's gonna sit for a second. But if we look at the database, we should see stuff just flying in. And if we say processes 10, we're gonna see a lot more.

**Aaron Francis:** This is how I see the database. Hopefully, this is interesting or maybe even fun to see behind the scenes. In reality, I'll turn those, I'll turn those batches up to 50 or 100,000 and run 10 processes, and I'll get a million rows. If I'm doing some sort of relational structure, I'll put all of that information in that insert function. So I'll insert a user and a blog post and some comments all inside that function, in which case I'll turn the batch size down, 'cause that function does generate, you know, 10 or 20 rows on its own.

**Aaron Francis:** If you think that there's something that I could be doing to make my life more easy, let me know. I'm super interested in making my life easy. Making these videos is hard, and so if you have an idea, please let me know. If you think PHP is stupid, please email me at aaron@example.com. I would love to read it. I'll get back to you as soon as I can. If you love database content, you found the right channel.

**Aaron Francis:** Please stick around, subscribe for more. See ya.

---

## Related on aaronfrancis.com

- [MySQL for Developers](/courses) *(Course · 2023-03-01 · shared: mysql, planetscale, database optimization)*
- [Being a developer, content creator, and entrepreneur with Aaron Francis](/podcast/being-a-developer-content-creator-and-entrepreneur-with-aaron-francis-gtj8ip) *(Podcast · 2022-09-19 · shared: database optimization, planetscale, mysql)*
- [Do The Hardest Thing with Jesse Hanley](/podcast/do-the-hardest-thing-with-jesse-hanley-9b42d310) *(Podcast · 2026-05-26 · shared: planetscale, mysql)*
- [Bury The Hatchet](/podcast/bury-the-hatchet-8ee6484d) *(Podcast · 2025-09-23 · shared: planetscale, mysql)*
- [Making videos developers actually want to watch](/podcast/making-videos-developers-actually-want-to-watch-6e3ad549) *(Podcast · 2024-06-03 · shared: mysql, planetscale)*

## 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` ([M-N](/llms/keywords/m-n.txt)), `performance testing` ([O-P](/llms/keywords/o-p.txt)), `database seeding` ([D-E](/llms/keywords/d-e.txt)), `random data generation` ([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).
