{"id":129,"date":"2024-02-02T11:11:03","date_gmt":"2024-02-02T16:11:03","guid":{"rendered":"https:\/\/kimsal.com\/blog\/?p=129"},"modified":"2024-02-02T11:11:03","modified_gmt":"2024-02-02T16:11:03","slug":"laravel-query-performance-tobase","status":"publish","type":"post","link":"https:\/\/kimsal.com\/blog\/2024\/02\/02\/laravel-query-performance-tobase\/","title":{"rendered":"Laravel query performance &#8211; toBase"},"content":{"rendered":"\n<p>I&#8217;ve scoured many of the top results for &#8220;Laravel performance&#8221; in google, bing, duck duck go, etc&#8230; and am flummoxed that one of the key things to help boost Laravel performance is almost never mentioned.  Specifically, I&#8217;m talking about the toBase() method in the query builder.<\/p>\n\n\n\n<p>The toBase() method will instruct the query builder to skip the step of turning the query results in to full blown Eloquent objects, and instead, return standard PHP classes &#8211; the &#8216;stdClass&#8217;.<\/p>\n\n\n\n<p>The resulting objects are bare.  You can&#8217;t call any methods on them, because there are none to call.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Client::query()-&gt;where('owner_id', 7)-&gt;get();<\/code><\/pre>\n\n\n\n<p>let&#8217;s say this returns 70 client records.  Each of those needs to be instantiated in to an Eloquent object.  If you *need* that functionality &#8211; for example, you&#8217;re going to loop through this client list and call some operation on each client (sending a notification, or attaching resources, or whatnot..) the full Eloquent objects may be needed.<\/p>\n\n\n\n<p>More often than not, however, I&#8217;ll see <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$clients = Client::query()-&gt;where('owner_id',7)-&gt;get();\nreturn response()-&gt;json(&#91;'clients'=&gt;$clients);<\/code><\/pre>\n\n\n\n<p>You&#8217;ve just iterated through 70 records, created new Eloquent objects, then immediately set about converting them to JSON.<\/p>\n\n\n\n<p>toBase() saves a tremendous amount of CPU time in the case above<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$clients = Client::toBase()-&gt;query()-&gt;where('owner_id',7)-&gt;get();\nreturn response()-&gt;json(&#91;'clients'=&gt;$clients);<\/code><\/pre>\n\n\n\n<p>I&#8217;m not going to give full detailed benchmarks here &#8211; you can simply try this out for yourself.<\/p>\n\n\n\n<p>I can say that with a minimally complex &#8216;Client&#8217; object and about 600 records in the database:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$c = Client::query()-&gt;toBase()-&gt;get();<\/code><\/pre>\n\n\n\n<p>is roughly twice as fast as<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$c = Client::query()-&gt;get(); <\/code><\/pre>\n\n\n\n<p>A 50% speed up is nothing to sneeze at.<\/p>\n\n\n\n<p>I recently took an API endpoint having to do with retrieving client records and associated addresses.  With a little bit of rewriting &#8211; including toBase() &#8211; this went from 1.1 seconds down to ~200ms.  This gets called multiple times per minute at heavy times, and having faster response times for the end users has been duly appreciated.<\/p>\n\n\n\n<p>I believe this can be achieved in Doctrine with &#8220;<a href=\"https:\/\/www.doctrine-project.org\/projects\/doctrine-orm\/en\/2.17\/reference\/dql-doctrine-query-language.html#hydration-modes\" data-type=\"link\" data-id=\"https:\/\/www.doctrine-project.org\/projects\/doctrine-orm\/en\/2.17\/reference\/dql-doctrine-query-language.html#hydration-modes\">HYDRATE_ARRAY<\/a>&#8221; mode, and likely similar in other tech stacks (hibernate, etc).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve scoured many of the top results for &#8220;Laravel performance&#8221; in google, bing, duck duck go, etc&#8230; and am flummoxed that one of the key things to help boost Laravel performance is almost never mentioned. Specifically, I&#8217;m talking about the toBase() method in the query builder. The toBase() method will instruct the query builder to&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-129","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/kimsal.com\/blog\/wp-json\/wp\/v2\/posts\/129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kimsal.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kimsal.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kimsal.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kimsal.com\/blog\/wp-json\/wp\/v2\/comments?post=129"}],"version-history":[{"count":0,"href":"https:\/\/kimsal.com\/blog\/wp-json\/wp\/v2\/posts\/129\/revisions"}],"wp:attachment":[{"href":"https:\/\/kimsal.com\/blog\/wp-json\/wp\/v2\/media?parent=129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kimsal.com\/blog\/wp-json\/wp\/v2\/categories?post=129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kimsal.com\/blog\/wp-json\/wp\/v2\/tags?post=129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}