<?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: wilfridterry</title>
    <description>The latest articles on DEV Community by wilfridterry (@wilfridterry).</description>
    <link>https://dev.to/wilfridterry</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3982610%2F220ef38f-0e2c-414f-92b1-7052a5649f27.png</url>
      <title>DEV Community: wilfridterry</title>
      <link>https://dev.to/wilfridterry</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/wilfridterry"/>
    <language>en</language>
    <item>
      <title>I Built a Free Laravel Package That Catches Slow Queries and Writes the Migration For You</title>
      <dc:creator>wilfridterry</dc:creator>
      <pubDate>Sat, 13 Jun 2026 12:41:01 +0000</pubDate>
      <link>https://dev.to/wilfridterry/i-built-a-free-laravel-package-that-catches-slow-queries-and-writes-the-migration-for-you-4ogp</link>
      <guid>https://dev.to/wilfridterry/i-built-a-free-laravel-package-that-catches-slow-queries-and-writes-the-migration-for-you-4ogp</guid>
      <description>&lt;p&gt;Your production database is slow. You know it. Your users know it. But you don't know &lt;em&gt;when&lt;/em&gt; it started or &lt;em&gt;which deploy&lt;/em&gt; broke it.&lt;/p&gt;

&lt;p&gt;Yesterday's deploy made some query 4x slower. Telescope shows "current state" — but not the regression. Pulse tells you "queries are slow" — but not what changed. You're left digging through git logs and running EXPLAIN manually.&lt;/p&gt;

&lt;p&gt;I built &lt;strong&gt;Cardinal&lt;/strong&gt; to answer one question: "Which deploy made which query slow?"&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;Let's say you deploy at 3pm. By 6pm, your error tracking shows increased timeout exceptions. You check Telescope — queries look fine now. You check git history — 47 commits since 3pm. You run EXPLAIN on 10 queries manually. Stress.&lt;/p&gt;

&lt;p&gt;This is the "production detective work" pattern. Every team does it. Most solve it by:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Keeping spreadsheets of "known slow queries"&lt;/li&gt;
&lt;li&gt;Setting up separate query monitoring (expensive SaaS, usually APM)&lt;/li&gt;
&lt;li&gt;Running &lt;code&gt;slower:analyze&lt;/code&gt; locally in dev (misses production patterns)&lt;/li&gt;
&lt;li&gt;Doing nothing and accepting slow deploys&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Cardinal does the detective work for you.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What It Does
&lt;/h2&gt;

&lt;p&gt;Install the free Laravel package:&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 cardinal/laravel
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it. No setup. It hooks into your database automatically.&lt;/p&gt;

&lt;h3&gt;
  
  
  It Catches Three Things
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1. Slow queries&lt;/strong&gt; — tracks every query, alerts when p95 crosses your threshold (default 500ms).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. N+1 queries&lt;/strong&gt; — detects when the same fingerprinted query repeats inside a single request.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Warning: N+1: SELECT * FROM products WHERE id = ? [150 repeats in single request]
Location: app/Http/Controllers/OrderController.php:42
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Missing indexes&lt;/strong&gt; — parses WHERE and JOIN conditions, checks your schema, suggests an index if a leading column is uncovered.&lt;/p&gt;

&lt;h3&gt;
  
  
  The AI Part
&lt;/h3&gt;

&lt;p&gt;Run &lt;code&gt;php artisan cardinal:analyze {id}&lt;/code&gt; and Cardinal:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Gets the query template and its EXPLAIN output&lt;/li&gt;
&lt;li&gt;Reads your table schema (columns, indexes, constraints)&lt;/li&gt;
&lt;li&gt;Sends context to Claude or OpenAI (your API key, your cost)&lt;/li&gt;
&lt;li&gt;Returns a structured diagnosis + ready-to-run migration
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;php artisan cardinal:analyze slow-query-123
&lt;span class="go"&gt;
Diagnosis:
  Query was doing a full table scan of 2.1M rows.
  Missing composite index on (user_id, status).

Fix migration generated:
  php artisan cardinal:fix slow-query-123

Expected improvement:
  p95 latency: 1200ms to 120ms (~10x faster)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Real Example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="c1"&gt;// OrderController.php&lt;/span&gt;
&lt;span class="nv"&gt;$orders&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'items'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$orders&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;$order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$order&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;customer&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// triggers N+1&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cardinal catches this automatically:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;N+1 Detected
Query:    SELECT * FROM customers WHERE id = ?
Repeats:  247 times in single request
Location: app/Http/Controllers/OrderController.php:18

Fix: Order::with('items', 'customer')-&amp;gt;get();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How It Works
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Hooks into &lt;code&gt;DB::listen&lt;/code&gt; — zero changes to your code&lt;/li&gt;
&lt;li&gt;Fingerprints each query (strips all literals, normalizes whitespace)&lt;/li&gt;
&lt;li&gt;Buffers per-request in memory — no DB write on every query&lt;/li&gt;
&lt;li&gt;Detects patterns in &lt;code&gt;terminating()&lt;/code&gt; hook&lt;/li&gt;
&lt;li&gt;Stores only issues and aggregates, never raw query values&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Overhead:&lt;/strong&gt; listener adds less than 0.05ms per query in recording mode. EXPLAIN and schema inspection run only on-demand via artisan commands, never automatically in production.&lt;/p&gt;

&lt;h3&gt;
  
  
  Privacy
&lt;/h3&gt;

&lt;p&gt;The fingerprinter strips all literals before anything is stored or sent. This is verified by a dedicated CI test that asserts no raw values pass through. Only normalized templates like &lt;code&gt;select * from orders where user_id = ?&lt;/code&gt; are ever stored.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;composer require cardinal/laravel
php artisan migrate
php artisan cardinal:report
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Supports Laravel 10, 11, 12, 13 and PHP 8.2+.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Free?
&lt;/h2&gt;

&lt;p&gt;The free package is complete and useful forever. You get continuous monitoring, AI analysis with your own key, and generated migrations. No account required.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cardinal Cloud&lt;/strong&gt; (paid, waitlist open) will add production history, deploy-to-regression tracking, and team alerts — starting at $19/month.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GitHub:&lt;/strong&gt; &lt;a href="https://github.com/wilfridterry/cardinal" rel="noopener noreferrer"&gt;https://github.com/wilfridterry/cardinal&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Packagist:&lt;/strong&gt; &lt;a href="https://packagist.org/packages/cardinal/laravel" rel="noopener noreferrer"&gt;https://packagist.org/packages/cardinal/laravel&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Open an issue if something doesn't work. Feedback directly shapes what gets built next.&lt;/p&gt;

&lt;h1&gt;
  
  
  laravel #php #database #performance #opensource
&lt;/h1&gt;

</description>
      <category>database</category>
      <category>laravel</category>
      <category>performance</category>
      <category>showdev</category>
    </item>
  </channel>
</rss>
