<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ali Haider</title>
    <description>The latest articles on DEV Community by Ali Haider (@alihaider8014).</description>
    <link>https://dev.to/alihaider8014</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3852313%2F4bc86982-f568-45cf-8015-8c22430707d3.jpeg</url>
      <title>DEV Community: Ali Haider</title>
      <link>https://dev.to/alihaider8014</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alihaider8014"/>
    <language>en</language>
    <item>
      <title>Stop Optimizing the Wrong Things: Practical Database Optimization Techniques Every Backend Developer Should Know</title>
      <dc:creator>Ali Haider</dc:creator>
      <pubDate>Mon, 03 Aug 2026 19:30:08 +0000</pubDate>
      <link>https://dev.to/alihaider8014/stop-optimizing-the-wrong-things-practical-database-optimization-techniques-every-backend-4p7j</link>
      <guid>https://dev.to/alihaider8014/stop-optimizing-the-wrong-things-practical-database-optimization-techniques-every-backend-4p7j</guid>
      <description>&lt;p&gt;We've all been there.&lt;/p&gt;

&lt;p&gt;An API that used to respond in milliseconds suddenly becomes slower as the database grows. The application logic hasn't changed much, but performance keeps degrading.&lt;/p&gt;

&lt;p&gt;In many cases, the problem isn't the framework or the programming language—it's how we're interacting with the database.&lt;/p&gt;

&lt;p&gt;This article shares a few practical optimization techniques I've used while working on backend systems. These aren't advanced tricks; they're small improvements that can make a noticeable difference in production environments.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Don't Apply Functions to Indexed Columns
&lt;/h2&gt;

&lt;p&gt;Imagine you have an indexed column named &lt;code&gt;updated&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A common approach is filtering records like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="nb"&gt;DATE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;updated&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'2026-07-30'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Although it works, there's a hidden cost.&lt;/p&gt;

&lt;p&gt;Because &lt;code&gt;updated&lt;/code&gt; is wrapped inside a function, the database usually can't use the index efficiently. Instead of performing an index range scan, it may need to evaluate every row.&lt;/p&gt;

&lt;p&gt;A better approach is to compare the raw timestamp:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;updated&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="s1"&gt;'2026-07-30 00:00:00'&lt;/span&gt;
&lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;updated&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="s1"&gt;'2026-07-31 00:00:00'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This allows the database optimizer to use the index effectively.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Convert Input Instead of Every Database Row
&lt;/h2&gt;

&lt;p&gt;I recently reviewed code that converted every timestamp inside SQL using time zone functions.&lt;/p&gt;

&lt;p&gt;While technically correct, it forced the database to perform a conversion for every matching row.&lt;/p&gt;

&lt;p&gt;Instead, convert the user's input once in your application:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$fromLocal&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;setTimezone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;DateTimeZone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'UTC'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then execute a simple query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;updated&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt;
&lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;updated&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result is easier to read, more maintainable, and usually much faster.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Don't Load Thousands of Rows Just to Count Them
&lt;/h2&gt;

&lt;p&gt;I've seen code similar to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$Model&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$sql&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It works, but think about what's happening.&lt;/p&gt;

&lt;p&gt;The database sends every matching row to your application.&lt;/p&gt;

&lt;p&gt;Your application allocates memory for every record.&lt;/p&gt;

&lt;p&gt;Finally, PHP counts the array.&lt;/p&gt;

&lt;p&gt;If the query returns 200,000 rows, you've transferred and stored 200,000 records simply to obtain a number.&lt;/p&gt;

&lt;p&gt;Instead, let the database do the counting:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;COUNT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or, if you're wrapping an existing query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;COUNT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The database returns a single integer instead of thousands of records.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Write Queries That Scale
&lt;/h2&gt;

&lt;p&gt;A query that performs well with 500 rows may become a bottleneck when your database reaches millions of records.&lt;/p&gt;

&lt;p&gt;Whenever you review a query, ask yourself:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Can this use an index?&lt;/li&gt;
&lt;li&gt;Am I processing unnecessary data?&lt;/li&gt;
&lt;li&gt;Can the database perform this operation more efficiently than my application?&lt;/li&gt;
&lt;li&gt;Am I transferring more data than I actually need?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These questions often reveal opportunities for optimization.&lt;/p&gt;




&lt;h2&gt;
  
  
  5. Readability Is Also an Optimization
&lt;/h2&gt;

&lt;p&gt;Performance isn't the only goal.&lt;/p&gt;

&lt;p&gt;Code that is easy to understand is easier to maintain, debug, and improve.&lt;/p&gt;

&lt;p&gt;For example, converting user input once in the application layer is often simpler than embedding multiple conditional time zone conversions inside SQL.&lt;/p&gt;

&lt;p&gt;Future developers—including your future self—will thank you.&lt;/p&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Database optimization doesn't always require rewriting your architecture.&lt;/p&gt;

&lt;p&gt;Often, the biggest gains come from small improvements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Avoid wrapping indexed columns in functions.&lt;/li&gt;
&lt;li&gt;Convert user input instead of every database row.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;COUNT(*)&lt;/code&gt; when you only need totals.&lt;/li&gt;
&lt;li&gt;Return only the data you actually need.&lt;/li&gt;
&lt;li&gt;Think about how the database executes your query—not just whether the query works.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As your application grows, these habits become increasingly valuable. Small optimizations applied to frequently executed queries can have a meaningful impact on performance, resource usage, and overall scalability.&lt;/p&gt;

&lt;p&gt;What optimization technique has had the biggest impact on your applications? I'd love to hear your experiences in the comments.&lt;/p&gt;

</description>
      <category>database</category>
      <category>queryoptimization</category>
      <category>webdev</category>
      <category>performance</category>
    </item>
    <item>
      <title>Flexible Boolean Validation for Laravel APIs — Handling “true”/“false” Strings Without the Headache</title>
      <dc:creator>Ali Haider</dc:creator>
      <pubDate>Mon, 30 Mar 2026 20:52:18 +0000</pubDate>
      <link>https://dev.to/alihaider8014/flexible-boolean-validation-for-laravel-2oad</link>
      <guid>https://dev.to/alihaider8014/flexible-boolean-validation-for-laravel-2oad</guid>
      <description>&lt;p&gt;Every Laravel developer has hit this at some point.&lt;/p&gt;

&lt;p&gt;You’re working on a clean API. Everything looks good. Then suddenly validation fails because the frontend sent "true" instead of true.&lt;/p&gt;

&lt;p&gt;Laravel’s default boolean rule is excellent for strictness, but it rejects common string values like "true" and "false" that come from JavaScript, React/Vue forms, query strings, or external services.&lt;/p&gt;

&lt;p&gt;The result? Scattered filter_var() calls, custom rules, or manual casting — repeated across every project.&lt;/p&gt;

&lt;p&gt;I got tired of the same workaround and decided to fix it once and for all.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Introducing flexible-boolean&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;I built a small, focused package that extends Laravel’s boolean validation to support real-world API inputs while keeping everything clean and secure.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Supported values now include:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"true" / "false" (strings)&lt;/li&gt;
&lt;li&gt;1 / 0&lt;/li&gt;
&lt;li&gt;Native true / false&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It integrates seamlessly with Laravel’s validation system — no side effects, no performance hit.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Quick Example&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Install via Composer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;composer require alihaider/flexible-boolean
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then use it in your Form Requests:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;rules&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="s1"&gt;'is_active'&lt;/span&gt;     &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'required'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'boolean:flexible'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
        &lt;span class="s1"&gt;'newsletter'&lt;/span&gt;    &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;FlexibleBooleanRule&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
        &lt;span class="s1"&gt;'is_published'&lt;/span&gt;  &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'boolean'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// still works as before&lt;/span&gt;
    &lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also register the rule globally if you prefer.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Before vs After&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Before&lt;/strong&gt;: Random 422 errors + extra controller logic&lt;br&gt;
&lt;strong&gt;After&lt;/strong&gt;: Predictable validation that just works with frontend data&lt;br&gt;
It’s a tiny package, but it saves a surprising amount of debugging time when building production APIs.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Let’s Discuss&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;How are you handling boolean values from the frontend in your Laravel projects today?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Manual casting?&lt;/li&gt;
&lt;li&gt;Custom rules per project?&lt;/li&gt;
&lt;li&gt;Something else entirely?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Drop your preferred approach in the comments — I’m curious to see the different solutions the community uses.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Package&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;https://packagist.org/packages/alihaider/flexible-boolean
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you found this useful, give it a like or save it for later. Happy coding!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>php</category>
      <category>laravel</category>
      <category>api</category>
    </item>
  </channel>
</rss>
