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

Поділитися
Вставка
  • Опубліковано 30 січ 2025

КОМЕНТАРІ • 143

  • @ahmad-murery
    @ahmad-murery Рік тому +68

    Some people hate PHP only because someone told them you should.
    Thanks Aaron!

  • @bopuc
    @bopuc Рік тому +27

    literally laughing out loud several times. absolute gold.
    (partner's like "what are you watching?!" "a guy seeding a database!" 🤣)

    • @daleryanaldover6545
      @daleryanaldover6545 Рік тому +1

      same, I can't hide the smile on my face while watching this 😂

  • @robinator18ps3
    @robinator18ps3 Рік тому +43

    My first thought was to make the single thread command and throw GNU Parallel at it. Assuming crossplatform isn't a factor.
    PHP for the win btw! It was my first love and still going strong 12+ years later.

    • @dealloc
      @dealloc Рік тому

      Yeah was thinking the same. This way you only have to think about the single concern instead of thinking about pooling etc.

    • @soniablanche5672
      @soniablanche5672 Рік тому

      The funny thing is that webservers that run php (apache/nginx) do not allow/support PHP multithreading, so it can only be used in command line lol

  • @completemaster
    @completemaster Рік тому +10

    PHP is wonderful. Your videos even better. Keep rocking.

  • @eatenpancreas
    @eatenpancreas Рік тому +68

    PHP is underrated atm, so much hate even though its gotten quite good :)

    • @parlor3115
      @parlor3115 Рік тому +5

      A nominally-typed TypeScript blows PHP out of the water. Shame we don't have such language.

    • @JLinhart
      @JLinhart Рік тому

      ​@@parlor3115in what sense?

    • @demian.succs0
      @demian.succs0 Рік тому

      ​@@parlor3115wym 'nominally'?

    • @eatenpancreas
      @eatenpancreas Рік тому

      @@parlor3115 PHP can be nominally typed 🤔

    • @edcarlo
      @edcarlo Рік тому +6

      they hate what they don't understand

  • @MT87840
    @MT87840 10 місяців тому

    I was doing this yesterday night and I amazed by your video now!
    1- You're totally right about not using the Hash facade. Here, the slower is the better so they can slow down us dramatically.
    2- Using models is not that efficient here. Why didn't you use the DB facade instead?
    Thanks for your great videos. I learn a lot from you.

  • @swebruh
    @swebruh Рік тому +17

    I love both PHP and MySQL, and using them every single day, but also C# of course ^^

    • @jondycz
      @jondycz Рік тому +3

      Same here. I just wish PHP added native async support. Even with Fibers stuff like sleep() will sleep the main thread and lot of native functions like file_get_contents are IO blocking

  • @_vicary
    @_vicary Рік тому

    Love the fact that there is still a place for the PHP experts out there in the field.
    I hope you find peace in teaching all these amazing and evolving technologies with a language you love.
    True love means you love it no matter what. Even if the language itself is no longer evolving, and the ecosystem around it is in decline, you still love it as a language all by itself.

  • @wfl-junior
    @wfl-junior Рік тому +21

    How about batch insert direct in the database? instead of 10000 insert queries, use 1 insert queries with 10000 values set.
    I believe it is better for performance.
    But this could run into issues of limited characters or parameters for a query, in this case I would limit the amount per batch.

    • @PlanetScale
      @PlanetScale  Рік тому +10

      It is better for performance but I like going through the model for casting and convenience

    • @MihailTirdea
      @MihailTirdea Рік тому

      I ran some benchmarks on this a while ago - 1 million entries, batches of 2500, 1 process ~7 seconds & 500 MB memory usage. I got the same numbers using Singlestore local & MySQL 8.

    • @davismiljantejs3395
      @davismiljantejs3395 Рік тому

      It is faster, yes.

  • @grim.reaper
    @grim.reaper Рік тому +6

    Do you have a php course, I really enjoy your teaching style so asking 🥺

  • @soniablanche5672
    @soniablanche5672 Рік тому +1

    I think one improvement is to a single sql query to insert multiple records with Model::insert() instead of looping and using Model::create(), this might not affect performance if the database in on localhost, but it should if it's somewhere in the cloud.

  • @BatmanBruceWayne
    @BatmanBruceWayne Рік тому +20

    I know this is not the channel for it, but please! More Laravel content! (whether it's on your channel or Laravel's). Love your videos!

    • @Laflamablanca969
      @Laflamablanca969 Рік тому +1

      Go to his personal channel. He posts laravel content there, but agreed, definitely more laravel here too!

  • @filipivanovic7083
    @filipivanovic7083 Рік тому

    the only MySQL tutorials i can watch and enjoy!

  • @scorpio8428
    @scorpio8428 Рік тому +3

    I haven't heard back yet. /s
    Nice vids, keep 'em coming!

  • @atreusduvelll600
    @atreusduvelll600 Рік тому

    Great video! One way to solve the expensive bcrypting of the password, would be to do it once and set the value as a property on the class for re-use.

  • @alexrusin
    @alexrusin Рік тому +6

    I'm still waiting on a girl to tell me "I love PHP!", so I can reply "I love Vue!"

  • @beefmcbutchface6497
    @beefmcbutchface6497 Рік тому +7

    My email got rejected, it says your inbox is full

  • @sheldon98c
    @sheldon98c Рік тому +5

    Why not generate a large array of data and bulk insert them at the end? For the dates, generated an arrays of 1B elements will take fee moments and this will make timestamps unique.

    • @TheOnlyJura
      @TheOnlyJura Рік тому +1

      This can take up a lot of ram, you would probably also need to increase php script memory limit

  • @tcarreira
    @tcarreira Рік тому +1

    What about turning off database constraints checks?
    This example didn't mention, but I believe it's fairly common to insert bazillions of data and don't care about checking for unique or primary keys on every insert (if you can be sure about your code producing that well enough)

  • @spamviking8591
    @spamviking8591 Рік тому +3

    Great example of why I use epoch timestamps for my date times in every database.

    • @reed6514
      @reed6514 Рік тому

      I use DATETIME, but i store with utc timezone (mysql doesn't store the timezone) and let PHP's DateTime class figure it out for me.
      Datetime has a lot of query features, and you can easily read dates when working directly with the db.

  • @Szarlus
    @Szarlus Рік тому

    An interesting idea I used in the past a couple of times to seed a lot of records was doing cross join (insert into... Select... Cross join...) . It might be for a very specific situation but if you find a use for it for seed and have a lot of initial data then boy, it goes brrrrr

  • @InMemoryOfNeo
    @InMemoryOfNeo Рік тому +2

    You can insert 50K rows in one second to mysql with LOAD INFILE command. Yes this needs a little bit configuration in mysql side but it is possible.

    • @PlanetScale
      @PlanetScale  Рік тому

      Yep! I'm definitely trading raw speed for convenience. The faker library and the ORM interface make my life super easy

  • @manu144x
    @manu144x Рік тому

    PHP has its own well deserved place in software development. Yes, it's not for everyone and everything, yes, it has a bad reputation because it used to be very loose with its syntax and types, but since php 8 it's really a mature language.

  • @ryanqvincent144
    @ryanqvincent144 Рік тому +1

    I didn't realize Laravel was that powerful. Nice. :) Yes, I like PHP. :)

  • @SmartGuyz
    @SmartGuyz Рік тому

    I never use frameworks, as it takes up a lot of space and time to set up.
    For this I just use PCNTL and raw MySQLi. BTW, love your videos on MySQL, learned a lot!

    • @invinciblemode
      @invinciblemode Рік тому +2

      If you don’t use frameworks you always end up making your own. Otherwise you’ll stay inefficient.

    • @SmartGuyz
      @SmartGuyz Рік тому

      @@invinciblemode that’s true though 👍

    • @RM-zj3zb
      @RM-zj3zb Рік тому

      Setting up Laravel is done in seconds: 'composer create-project laravel/laravel example-app' and then 'php artisan serve'

  • @SebastianPerezG
    @SebastianPerezG Рік тому

    Question , why using a external service like Planet Scale if i already have a Mysql database in my hosting ? how secure it is ? i mean , moving traffic from one place to another is secure ? queries? data ?

  • @IndraPrastha
    @IndraPrastha Рік тому

    Whats the font used in your terminal? It's so clean

  • @vasiovasio
    @vasiovasio Рік тому

    Great video Aaron! PHP for Life! ❤❤❤

  • @conceptrat
    @conceptrat Рік тому +2

    With that generic "password" would it be better to make a constamt of a hashed password and then use that in the function. Also make the "created at" before the "email verified" and then "email verified" can be some random number of hours after "created by".

    • @dhLotan
      @dhLotan Рік тому

      I thought he was gonna do that actually since he mentioned it was expensive. Calculate once and reuse. I would imagine it would be particularly useful if you wanted to login as random users to do things with because the login code is gonna compare hashed values.

  • @gilney.mathias
    @gilney.mathias Рік тому +7

    I'm a php/laravel dev and that 'use (...)' is the most annoying thing ever 😅

    • @FGj-xj7rd
      @FGj-xj7rd Рік тому +1

      When are they adding the full sysntax fn () => { // 😥 }

  • @Kane0123
    @Kane0123 Рік тому

    @Planetscale - why do you prefer PHP? Is it something in particular or just what you’ve got the most exp / comfort with?

    • @PlanetScale
      @PlanetScale  Рік тому +5

      I prefer it because I prefer Laravel. PHP isnt bad, but if I didn't have Laravel I'd probably be a ruby guy

  • @brunocmesquita
    @brunocmesquita Рік тому

    Is this the default robbyrussel prompt?

  • @michailgiannopoulos5274
    @michailgiannopoulos5274 Рік тому +1

    Must have seen alot of your videos lately because i saw you in my sleep too😅

  • @robbie745
    @robbie745 Рік тому +9

    I love how you provide a fake email for php critics xD

  • @nimmneun
    @nimmneun Рік тому +1

    Nice one as always :D
    I use a bunch of common seeds like 90% of the time, when I need consistent stuff e.g.
    customers + addresses + orders(+items), refunds(+items) etc and want them to have proper relations and consistent timestamps + pk's.
    For date(times) is just do something like this.
    $from = strtotime('2020-01-01');
    $until = strtotime('2023-12-01');
    $sets = 1_000_000;
    $stepSize = (int)(($until - $from) / $sets);
    and then loop while `$createdAt = date('Y-m-d H:i:s', $from += $stepSize);`

  • @bboydarknesz
    @bboydarknesz Рік тому

    so is it safe if I use this pool for backfill data in production ?
    What's the impact anyway? It's new for me.
    I usually use chunk to backfill data if the data too much. But it's very slow for sure.
    in this case, I use Laravel.

    • @bboydarknesz
      @bboydarknesz Рік тому

      How to choose the number of processes anyway? I could not be like whatever you like, right ?

  • @henrymaddocks984
    @henrymaddocks984 Рік тому

    Make the database once, dump it out to an sql.gz file and then when you need a fresh one you can drop the database and import your clean one.

  • @ricardor8044
    @ricardor8044 Рік тому

    Sorry for this question, but what's your terminal font, sir ?

  • @huge_letters
    @huge_letters Рік тому

    I get deadlocks when seeding my db with a transaction which does
    insert 1 row in table 1
    insert multiple rows in table 2
    delete multiple rows from table 3
    insert multiple rows from table 3
    Multiple transactions in parallel don't affect the same rows - there's no overlap except for step 2 but it seems it's step 3 or 4 which causes the deadlock. Using drizzle ORM - not sure if that matters tho.
    Not a big deal but I would be glad to at least have an intuition as to why that happens - have to run seeding sequentially for now :(.

  • @Andrew-v4o7k
    @Andrew-v4o7k Рік тому

    What does the phrase "yonk this" mean? I understand that we are taking part of the code. But what is "yonk"?

    • @badpussycat
      @badpussycat Рік тому

      Never heard yonk before. But vi has a yank command (copies text)

    • @PlanetScale
      @PlanetScale  Рік тому +4

      Yank, steal, grab, pluck. Something like that!

    • @Andrew-v4o7k
      @Andrew-v4o7k Рік тому

      @@PlanetScale Thank you! I'm still learning English and I heard this word for the first time

    • @tcarreira
      @tcarreira Рік тому +2

      In vim, the shortcut for copy is 'y', which I've been told stands for yank

  • @COneillCodes
    @COneillCodes Рік тому

    Should look at using gnu parallel command to run multiple processes

    • @PlanetScale
      @PlanetScale  Рік тому +1

      Maybe! What would that do for me in this setup?

    • @COneillCodes
      @COneillCodes Рік тому

      @@PlanetScale less code that needs to be manually written. No need to manually write the pool code and how many to run and such you can write the base task and let parallel handle all that

  • @cloudedge7418
    @cloudedge7418 Рік тому

    I did this exact thing, but was using queues and jobs, but this seems way tidy 🎉

  • @Kane0123
    @Kane0123 Рік тому

    Love it.

  • @sefaiyi
    @sefaiyi Рік тому

    love it

  • @reed6514
    @reed6514 Рік тому

    What's that db viewer you're using?

    • @gebi84
      @gebi84 Рік тому

      😂

    • @eleftrik
      @eleftrik Рік тому +3

      TablePlus

    • @reed6514
      @reed6514 Рік тому +1

      @@eleftrik thanks. I just use the mysql cli but i might try that

  • @GergelyCsermely
    @GergelyCsermely Рік тому

    Thanks

  • @mwahyuindriyanto8877
    @mwahyuindriyanto8877 Рік тому

    How i can insert fake data on multiplle table that have variaous raletipnship like one to many and many to many?

    • @PlanetScale
      @PlanetScale  Рік тому

      Just add all the related data (blog posts, comments, etc) as you're inserting the user

  • @smiley2827
    @smiley2827 Рік тому

    Will this approach work with SQLite ?

  •  Рік тому

    This is why I love Laravel.

  • @mjarkk
    @mjarkk Рік тому +3

    Next time use laravels model insert method to insert many table rows at once it's so much faster than create. Do note though that auto filled properties of a model won't be set like created at

    • @PlanetScale
      @PlanetScale  Рік тому +2

      That's why I like going through the model!

    • @tomroelofs1667
      @tomroelofs1667 Рік тому

      What about Model::make() and then batch insert? Still will go through the model and use batch insert.

  • @FGj-xj7rd
    @FGj-xj7rd Рік тому +4

    Before watching the video, I am assuming you will be using model factories from Laravel.

    • @PlanetScale
      @PlanetScale  Рік тому

      🧠

    • @FGj-xj7rd
      @FGj-xj7rd Рік тому +4

      @@PlanetScaleI didnt know Laravel had that pool function. Thats so cool.

  • @yezperdk
    @yezperdk Рік тому +1

    I sent an e-mail to the address mentioned at the end of the video. Why haven't I heard back from you yet?

    • @PlanetScale
      @PlanetScale  Рік тому +1

      Hmmm weird. Try again!

    • @yezperdk
      @yezperdk Рік тому

      I just did, and NOTHING! Smells fishy@@PlanetScale

  • @ambuj.k
    @ambuj.k Рік тому

    PHP stonks 📈📈

  • @Ciph3rzer0
    @Ciph3rzer0 Рік тому

    Ah yes. Famously no other modern languages have closures. Php so good

  • @chillout69
    @chillout69 Рік тому

    Nice one

  • @hl7297
    @hl7297 Рік тому

    Why db:seed not doing this by default? Any downside to it?

    • @panda-e9h
      @panda-e9h Рік тому +1

      Cannot accept arguments in seeder.

  • @MrNichuya
    @MrNichuya Рік тому +2

    MySQL + PHP = Aaron

  • @johnypatera160
    @johnypatera160 Рік тому

    Joke with email earned you like and comment 😂

  • @daleryanaldover6545
    @daleryanaldover6545 Рік тому

    I hate PHP but was forced to work on it, picked up Laravel and it showed me how an ugly language such as PHP can be wonderful in some ways. My perspective on PHP has changed ever since from personally bad to not quite as bad as it sounds like.

  • @abdulsiyadnp
    @abdulsiyadnp Рік тому +1

    you don't have to hate PHP, in order to love other lang. Just Laravel is enough how PHP is doing nowadays.

  • @RM-zj3zb
    @RM-zj3zb Рік тому

    Wut!? 10_000! Programming PHP for years but did not know that one!!

  • @tamasnogradi4524
    @tamasnogradi4524 Рік тому

    Pretty cool mate, I do the same!

  • @CanRau
    @CanRau Рік тому

    Very jealous of your vanity address 😂

  • @TheROck3052
    @TheROck3052 Рік тому

    No, php is not stuppid. It is perfect for small Websites with less code.

  • @mihailborovik
    @mihailborovik 3 місяці тому

    🎉🎉🎉❤❤❤

  • @SirXtC
    @SirXtC Рік тому +4

    (P)re (H)istoric (P)rogramming

  • @Aalii6
    @Aalii6 Рік тому +1

    👍👍

  •  Рік тому +2

    email sent

  • @vikingthedude
    @vikingthedude Рік тому

    Hey Aaron do you drive a lamborghini?

    • @PlanetScale
      @PlanetScale  Рік тому +1

      Kind of! It's a Honda Odyssey minivan

  • @wilfred-almeida
    @wilfred-almeida Рік тому

    Or, just access consumer data and use that 🧠
    Hire me for more such pragmatic solutions 😎

  • @mourad970
    @mourad970 Рік тому

    I fail to understand people who hate PHP 😅

  • @johndonuts4258
    @johndonuts4258 11 місяців тому

    PHP is fantastic, php haters are just ignorants

  • @lassestube
    @lassestube 9 місяців тому

    😂

  • @luciano_mdc7443
    @luciano_mdc7443 Рік тому

    1k like

  • @clixm
    @clixm Рік тому +1

    Second!

  • @xcrap
    @xcrap Рік тому

    This one I don't like. Again, people are so framework dependent it's horrible. You can do this with PHP easily. People are confusing PHP with Laravel and that's bad, it's the same with JS and React or Next. Frameworks are useful for specific projects, for small things why be framework dependent? I don't understand.

    • @PlanetScale
      @PlanetScale  Рік тому

      That's ok! Hopefully you'll enjoy the next videos more.

  • @shravanuchil
    @shravanuchil Рік тому

    👍👍