Is it worth to set Eloquent fields explicitly?

You know, I love Eloquent so much! It provides very elegant way to link you database to models. And it does a lot of things for you too, for example:

  • guessing table name by class name;
  • guessing which field should be used for foreign keys.

Have you ever wondered, how much overhead do these things create? I did. And I figured it out with table name case (I guess should be similar with foreign key column guessing. Take a look at my experiment!

TL;DR: It is worth.

Idea

First and foremost, I believe in numbers. I am in love with objective proofs. Been this way since primary school with me. If numbers says so, then it is so!

So I decided to do little experiment and find out is Eloquent smarter than me. Basically I wanted to know how big specifying table name is performance-wise.

Environment

I realized that, in order to give reliable results, benchmark tests should be run in same environment. I used Scotch Box as a virtual environment on my Dell Inspiron 15R 5537.
Host specs:

  • Dell Inspiron 15R 5537 Laptop
  • Archlinux OS
  • VirtualBox version 5.0.24_OSEr108355
  • Intel Core i7-4500U 1.8 GHz
  • 8 GB DDR3L-1600 RAM

Rules

  • Test must be run with 1.000, 10.000, 100.000 and 1.000.000 entries 3 times each.
  • Before every test, migrate:refresh must be ran.
  • Laravel 5.2

The purpose of this benchmark test is to tell how more (or less) efficient specifying table name is. To do so, I wrote a simple command that would create given amount of entries and get the time. You can find code with installation instructions at GitHub.

Test #1:

php artisan benchmark:table-names 1000
[table]
Run #,Create With,Create Without,Delete With,Delete Without
1,0.760499 s,0.947851 s,0.428701 s,0.448024 s
2,0.764343 s,0.871627 s,0.350178 s,0.327648 s
3,0.787460 s,0.871644 s,0.415487 s,0.356445 s
[/table]
Interpretation: performance improved by about 16%. Not too bad, I’d say.

Test #2

php artisan benchmark:table-names 10000
[table]
Run #,Create With,Create Without,Delete With,Delete Without
1,8.900200 s,9.556860 s,3.637363 s,4.309061 s
2,8.075793 s,8.436275 s,3.482921 s,3.771987 s
3,7.731485 s,8.353145 s,3.550109 s,3.888283 s
[/table]
Interpretation: performance improved by about 6%.

Test #3

php artisan benchmark:table-names 100000
[table]
Run #,Create With,Create Without,Delete With,Delete Without
1,83.124970 s,91.055072 s,37.641744 s,41.426391 s
2,76.741776 s,84.055893 s,36.565190 s,41.268333 s
3,77.190507 s,84.287409 s,35.712079 s,40.605010 s
[/table]
Conclusion: performance improved by about 9%.

Test #4

php artisan benchmark:table-names 1000000
[table]
Run #,Create With,Create Without,Delete With,Delete Without
1,851.729584 s,922.643061 s,604.799272 s,612.457177 s
2,852.024533 s,906.196686 s,572.282368 s,606.131770 s
3,778.703869 s,816.698185 s,501.491298 s,596.274623 s
[/table]
Interpretation: performance improved by about 6.5%.

Conclusion

So, performance increased by 6-9%.
If you have big enterprise application and every millisecond count, then you should definitely specify table names.
However, if you are seasoned user and want to try out Laravel, then you shouldn’t be worried about things like this and focus more on big picture.

Have you ever benchmarked Laravel or it’s components? How does it went?
Let me know! Leave a comment below. 🙂

2 Comments

  • Julius

    24th November 2016

    Aš nesuprantu ka jūs tam laravel’ije matot. Du metus kodinau su symfony, pakeičiau darbą, dabar su laraveliu tenka dirbt. Ir visiem kolegom patinka. Sakau, symfony esat bandę? Truputį. Seniai.
    Turi modelį, bet nežinai kokius jis fieldus turi tol, kol nepasižiūri duombazėj. IDE facade’ų nekomplytina, reikia atskirų toolsų kažkokiam failui sugeneruot kad pradėtu komplyinti (aišku ctrl+click nuveda į tą sugeneruotą failą, o ne failą vendoriuose). Formų kūrimas kaip prieš dešimt metų, ne tik kiekvieną fieldą atskirai aprašinėti reikia, bet ir jo html’ą, kiekvieną kartą. Aš tau siūlau pamėginti prie kokio bent jau mažesnio projekto išbandyti symfony. Nėra bardako, kuris laravelyje žmonėm, nesuprantu kaip, bet akių nedrąsko, kai pem modelių tam pačiam folderyje guli. Migracijos – vienas malonumas, formos – viena eilutė ir tavo visa forma surenderinta ir jei reikia, prie kiekvieno fieldo rodomos su juo susijusios klaidos. Viena faking eilutė, ir man rodos out of the box. Iš DB gauni ne kažkokį neaiškų objektą, su neaiškiais properčiais, o savo entitį, objektą kurį turi atsidaręs savo IDE, tik su užpildytom reikšmėm. Galėčiau tęsti ir tęsti, bet kad dirbt jau laikas.

    Parašiau komentarą dėl susikaupusio pykčio, kad laravelis taip liaupsinamas tų, kurie nėra dorai dirbę su symfony, ir dėl to, kad netyčia čia užklydau iš google, ir prisiminiau tave iš unreal-x laikų.

    Cheers

    Reply
    • tomas

      20th March 2017

      Labas, Juliau.

      Ačiū už komentarą. 🙂 Bandžiau ir symfony, ir laravel. Galiausiai nusprendžiau iš vis judėt tolyn nuo PHP 🙂

      Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.