<?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: devtoolkits</title>
    <description>The latest articles on DEV Community by devtoolkits (@devtoolkits_app).</description>
    <link>https://dev.to/devtoolkits_app</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%2F4277662%2F3421555b-2d71-4ff2-a2c0-3668a64483b2.png</url>
      <title>DEV Community: devtoolkits</title>
      <link>https://dev.to/devtoolkits_app</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/devtoolkits_app"/>
    <language>en</language>
    <item>
      <title>Reading EXPLAIN as a tree: the slowest node is never the root</title>
      <dc:creator>devtoolkits</dc:creator>
      <pubDate>Wed, 02 Dec 2026 06:23:44 +0000</pubDate>
      <link>https://dev.to/devtoolkits_app/reading-explain-as-a-tree-the-slowest-node-is-never-the-root-4l8h</link>
      <guid>https://dev.to/devtoolkits_app/reading-explain-as-a-tree-the-slowest-node-is-never-the-root-4l8h</guid>
      <description>&lt;p&gt;Which node in an EXPLAIN plan is actually slow? Reading &lt;code&gt;actual time&lt;/code&gt; at face value gets it wrong, and I only noticed while parsing plans into a tree for a browser-only EXPLAIN visualizer (nothing you paste leaves the page).&lt;/p&gt;

&lt;p&gt;Parsing the text back into a tree is the easy half. Here are the three things that bit me.&lt;/p&gt;

&lt;h2&gt;
  
  
  Trap 1: &lt;code&gt;actual time&lt;/code&gt; includes the children
&lt;/h2&gt;

&lt;p&gt;In &lt;code&gt;EXPLAIN (ANALYZE)&lt;/code&gt;, &lt;code&gt;actual time=0.010..40.000&lt;/code&gt; includes the time the node spent waiting on its children. If you turn that into a percentage, the root (&lt;code&gt;Sort&lt;/code&gt;, &lt;code&gt;Limit&lt;/code&gt;) is always near 100% and tells you nothing.&lt;/p&gt;

&lt;p&gt;A node's &lt;strong&gt;own&lt;/strong&gt; time is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;own = (time per loop × loops) − Σ(child time per loop × child loops)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A real PostgreSQL 16 plan (trimmed):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Merge Join  (… ) (actual time=3.426..63.571 rows=18000 loops=1)
  -&amp;gt;  Index Scan using idx_orders_user on orders o  (… ) (actual time=0.010..44.488 rows=99996 loops=1)
  -&amp;gt;  Sort  (… ) (actual time=2.873..3.233 rows=3600 loops=1)
        -&amp;gt;  Seq Scan on users u  (… ) (actual time=0.018..2.266 rows=3600 loops=1)
Execution Time: 64.868 ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Read raw, the &lt;code&gt;Merge Join&lt;/code&gt; is "98% of the query" (63.6 of 64.9 ms). Subtract its children and its own time is 63.57 − 44.49 − 3.23 ≈ &lt;strong&gt;15.9 ms (25%)&lt;/strong&gt;. The &lt;code&gt;Index Scan&lt;/code&gt; is the real hot spot at &lt;strong&gt;44.5 ms (70%)&lt;/strong&gt;, which is a very different thing to go fix.&lt;/p&gt;

&lt;h2&gt;
  
  
  Trap 2: &lt;code&gt;loops&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The inner side of a &lt;code&gt;Nested Loop&lt;/code&gt; reports &lt;strong&gt;per-loop&lt;/strong&gt; time: &lt;code&gt;actual time=0.010..0.050 loops=2000&lt;/code&gt;. If you forget to multiply by &lt;code&gt;loops&lt;/code&gt;, the node that ran 2,000 times and dominates the query looks like the lightest one.&lt;/p&gt;

&lt;p&gt;(Row counts are per loop too, so compare estimated and actual rows &lt;em&gt;without&lt;/em&gt; multiplying. I got this backwards at first and flagged every nested-loop inner side as a bad estimate.)&lt;/p&gt;

&lt;h2&gt;
  
  
  Trap 2b: under a &lt;code&gt;Gather&lt;/code&gt;, don't multiply by &lt;code&gt;loops&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Edit: a reader pointed out that the loops multiplication breaks under parallel plans. They're right.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Below a &lt;code&gt;Gather&lt;/code&gt; / &lt;code&gt;Gather Merge&lt;/code&gt;, &lt;code&gt;loops&lt;/code&gt; is the number of participants (workers plus the leader), and they all run at the same time. Multiplying per-loop time by &lt;code&gt;loops&lt;/code&gt; adds up time that overlapped. On a parallel &lt;code&gt;GROUP BY&lt;/code&gt; over 3M rows (495 ms total) that gave the &lt;code&gt;Gather&lt;/code&gt; an own time of about −957 ms and the &lt;code&gt;Seq Scan&lt;/code&gt; 833 ms.&lt;/p&gt;

&lt;p&gt;The fix: the node right under the &lt;code&gt;Gather&lt;/code&gt; runs once per participant, so its &lt;code&gt;loops&lt;/code&gt; is the participant count &lt;code&gt;N&lt;/code&gt;. For everything below it, use &lt;code&gt;per-loop time × loops / N&lt;/code&gt;. For that first node it's just the per-loop time. An inner &lt;code&gt;Nested Loop&lt;/code&gt; side that ran 4,000 times across 2 participants counts as 2,000 iterations of wall-clock work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Trap 3: the text tree is defined by the &lt;code&gt;-&amp;gt;&lt;/code&gt; column
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hash Join  (cost=…) (actual time=…)
  Hash Cond: (o.user_id = u.id)
  -&amp;gt;  Seq Scan on orders o  (cost=…)
        Filter: (total &amp;gt; 100)
  -&amp;gt;  Hash  (cost=…)
        -&amp;gt;  Seq Scan on users u  (cost=…)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The root is the first line without an arrow, and a child is any &lt;code&gt;-&amp;gt;&lt;/code&gt; whose &lt;strong&gt;column&lt;/strong&gt; is to the right of its parent's. Push columns on a stack and pop everything at or beyond the new arrow's column. Indentation of the detail lines varies per node, so don't use it.&lt;/p&gt;

&lt;p&gt;Things I only found by running real output through the parser:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;CTE t&lt;/code&gt;, &lt;code&gt;InitPlan 1&lt;/code&gt;, &lt;code&gt;SubPlan 2&lt;/code&gt; are headings, not nodes with a cost, and their subplans hang below them.&lt;/li&gt;
&lt;li&gt;A trailing &lt;code&gt;Planning:&lt;/code&gt; / &lt;code&gt;Buffers:&lt;/code&gt; / &lt;code&gt;JIT:&lt;/code&gt; block must not be glued onto the last node's details.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Bitmap Index Scan on idx_orders_user&lt;/code&gt;: the name after &lt;code&gt;on&lt;/code&gt; is an index, not a table.&lt;/li&gt;
&lt;li&gt;JSON output names nodes differently from text (&lt;code&gt;Aggregate&lt;/code&gt; + &lt;code&gt;Strategy: Hashed&lt;/code&gt; is &lt;code&gt;HashAggregate&lt;/code&gt; in text), so normalize both.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  MySQL and MariaDB
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;EXPLAIN FORMAT=TREE&lt;/code&gt; has the same arrows, but the root has one too, and &lt;code&gt;Filter: (…)&lt;/code&gt; / &lt;code&gt;Limit: 20 row(s)&lt;/code&gt; lines are the node type plus its detail. MariaDB's &lt;code&gt;ANALYZE FORMAT=JSON&lt;/code&gt; is different again: the per-table times are already &lt;em&gt;exclusive&lt;/em&gt;, so subtracting children would be wrong.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;

&lt;p&gt;The tool is here, runs entirely in your browser, and reads PostgreSQL (text/JSON), MySQL, MariaDB and SQLite plans: &lt;a href="https://devtoolkits.app/en/tools/explain-visualizer/?utm_source=devto&amp;amp;utm_medium=social&amp;amp;utm_campaign=explain-self-time-tree" rel="noopener noreferrer"&gt;EXPLAIN Visualizer&lt;/a&gt;&lt;/p&gt;

</description>
      <category>sql</category>
      <category>postgres</category>
      <category>mysql</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
