<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Query-Performance on EXPLAIN ANALYZE</title><link>https://explainanalyze.com/tags/query-performance/</link><description>Recent content in Query-Performance on EXPLAIN ANALYZE</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Fri, 11 Sep 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://explainanalyze.com/tags/query-performance/index.xml" rel="self" type="application/rss+xml"/><item><title>One Column Added to a SELECT: 1,020 Page Reads Became 166,667</title><link>https://explainanalyze.com/p/one-column-added-to-a-select-1020-page-reads-became-166667/</link><pubDate>Fri, 11 Sep 2026 00:00:00 +0000</pubDate><guid>https://explainanalyze.com/p/one-column-added-to-a-select-1020-page-reads-became-166667/</guid><description>&lt;img src="https://explainanalyze.com/" alt="Featured image of post One Column Added to a SELECT: 1,020 Page Reads Became 166,667" /&gt;&lt;div class="tldr-box"&gt;
 &lt;strong&gt;TL;DR&lt;/strong&gt;
 &lt;div&gt;An index-only scan reads the index and never touches the table. Add one column to the SELECT list that the index doesn&amp;rsquo;t carry and the engine has to visit the table itself (the heap, in Postgres terms) for every matching row, and past a certain row count the planner gives up on the index entirely and scans the table. The fix is to extend the index or shrink the projection; both cost something, and on Postgres the extended index still isn&amp;rsquo;t enough by itself.&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Take a query behind a list page:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-sql" data-lang="sql"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;created_at&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;&lt;code&gt;orders&lt;/code&gt; has a composite index on &lt;code&gt;(customer_id, status, created_at)&lt;/code&gt;. Every column the query touches is in it, so the engine walks the index and never opens the table. That is a covering index, one that holds every column the query needs. Then say a feature asks for the order total on the list. The diff is one column.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-sql" data-lang="sql"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;created_at&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;total_cents&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Here are both, run warm against a 5-million-row &lt;code&gt;orders&lt;/code&gt; table (1.3 GB heap, 194 MB index) on PostgreSQL 18. The table is a fixture I built for this post on a scratch instance, not a production incident, and every number below comes from runs against it. Customer 42 has 266 orders.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;span class="lnt"&gt;12
&lt;/span&gt;&lt;span class="lnt"&gt;13
&lt;/span&gt;&lt;span class="lnt"&gt;14
&lt;/span&gt;&lt;span class="lnt"&gt;15
&lt;/span&gt;&lt;span class="lnt"&gt;16
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;-- before
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Index Only Scan using idx_orders_cust_status_created on orders
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; (actual time=0.046..0.093 rows=266 loops=1)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Index Cond: (customer_id = 42)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Heap Fetches: 0
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Buffers: shared hit=34
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Execution Time: 0.134 ms
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;-- after: one column added
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Bitmap Heap Scan on orders (actual time=0.071..0.472 rows=266 loops=1)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Recheck Cond: (customer_id = 42)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Heap Blocks: exact=265
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Buffers: shared hit=270
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; -&amp;gt; Bitmap Index Scan on idx_orders_cust_status_created
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Index Cond: (customer_id = 42)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Execution Time: 0.519 ms
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;The plan node changed shape, 34 buffers became 270 (a buffer here is one page read), and the query is four times slower. Nobody notices. Half a millisecond is invisible on any dashboard, and this is where most write-ups of covering indexes stop, with a slowdown that doesn&amp;rsquo;t hurt.&lt;/p&gt;
&lt;p&gt;Customer 777 stands in for a marketplace seller: 200,239 orders in the fixture, interleaved with everyone else&amp;rsquo;s over the same year. Same two queries:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;span class="lnt"&gt;12
&lt;/span&gt;&lt;span class="lnt"&gt;13
&lt;/span&gt;&lt;span class="lnt"&gt;14
&lt;/span&gt;&lt;span class="lnt"&gt;15
&lt;/span&gt;&lt;span class="lnt"&gt;16
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;-- before
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Index Only Scan using idx_orders_cust_status_created on orders
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; (actual time=0.052..16.070 rows=200239 loops=1)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Heap Fetches: 0
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Buffers: shared hit=1020
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Execution Time: 22.440 ms
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;-- after: one column added
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Gather (actual time=0.388..160.668 rows=200239 loops=1)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Workers Planned: 2
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Workers Launched: 2
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Buffers: shared hit=14884 read=151783 written=273
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; -&amp;gt; Parallel Seq Scan on orders (actual time=0.363..151.954 rows=66746 loops=3)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Filter: (customer_id = 777)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Rows Removed by Filter: 1599920
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Execution Time: 167.623 ms
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;The index is gone from the plan. Three workers read all 166,667 pages of the table and threw away 4.8 million rows to keep 200 thousand. Reads went from 1,020 pages to 166,667, and &lt;code&gt;written=273&lt;/code&gt; means the scan pushed dirty pages out of a 128 MB buffer pool to make room, which is the part the other queries on the box will feel.&lt;/p&gt;
&lt;p&gt;The reflexive fix is already forming: add &lt;code&gt;total_cents&lt;/code&gt; to the index. That is the fix tried below, but it isn&amp;rsquo;t free and on Postgres it isn&amp;rsquo;t sufficient. The other reflex, &amp;ldquo;only select what you need,&amp;rdquo; is correct and ignored in practice, because the ORM emits &lt;code&gt;SELECT *&lt;/code&gt; unless someone tells it otherwise. What follows is why the plan flips, why &lt;code&gt;Heap Fetches: 0&lt;/code&gt; is a claim about this moment and not about the index, and what each fix costs.&lt;/p&gt;
&lt;h2 id="why-the-planner-walks-away-from-the-index"&gt;Why the planner walks away from the index
&lt;/h2&gt;&lt;p&gt;An index entry knows the row&amp;rsquo;s location in the heap. It doesn&amp;rsquo;t know &lt;code&gt;total_cents&lt;/code&gt;. Once the query needs a column the index doesn&amp;rsquo;t carry, every matching entry becomes a heap page visit, and the cost of the query stops being &amp;ldquo;how many index entries match&amp;rdquo; and becomes &amp;ldquo;how many distinct heap pages hold those rows.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;For customer 42 that&amp;rsquo;s 265 pages, so a bitmap scan over the index (collect the matching row locations first, then visit each page once) is still the cheapest route. For customer 777 the arithmetic changes. The 5 million rows sit about 30 to a page, and one in every 25 belongs to customer 777, which puts one of their rows on nearly every page in the table. Whether the engine reaches those pages through the index or by reading the file front to back, it touches 166,667 pages either way, and the sequential read is the cheaper way to do it. With &lt;code&gt;enable_seqscan = off&lt;/code&gt; the planner&amp;rsquo;s fallback was a parallel bitmap heap scan that took 292 ms, touched the same 167 thousand pages, and went lossy (tracking whole pages instead of individual rows) because the bitmap outgrew the 4 MB &lt;code&gt;work_mem&lt;/code&gt;. The planner&amp;rsquo;s choice was right. The query is just expensive now, in a way it wasn&amp;rsquo;t when the index alone could answer it.&lt;/p&gt;
&lt;p&gt;MySQL&amp;rsquo;s optimizer took the other branch on the same data (2 million rows, InnoDB, MySQL 8.4). It stayed on the index and paid per row:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;span class="lnt"&gt;12
&lt;/span&gt;&lt;span class="lnt"&gt;13
&lt;/span&gt;&lt;span class="lnt"&gt;14
&lt;/span&gt;&lt;span class="lnt"&gt;15
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;-- 106 rows, covered
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;-&amp;gt; Covering index lookup on orders using idx_orders_cust_status_created (customer_id=42)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; (cost=11.2 rows=106) (actual time=0.0779..0.091 rows=106 loops=1)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;-- 106 rows, one column added
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;-&amp;gt; Index lookup on orders using idx_orders_cust_status_created (customer_id=42)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; (cost=90 rows=106) (actual time=0.182..0.339 rows=106 loops=1)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;-- 80,115 rows, covered
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;-&amp;gt; Covering index lookup on orders using idx_orders_cust_status_created (customer_id=777)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; (cost=15515 rows=151198) (actual time=0.141..12.8 rows=80115 loops=1)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;-- 80,115 rows, one column added
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;-&amp;gt; Index lookup on orders using idx_orders_cust_status_created (customer_id=777)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; (cost=86898 rows=151198) (actual time=7.93..471 rows=80115 loops=1)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;The tree format names the difference outright: &lt;code&gt;Covering index lookup&lt;/code&gt; becomes &lt;code&gt;Index lookup&lt;/code&gt;. Each of the 80,115 secondary-index entries carries the primary key, and each one now costs a second descent into the clustered index to fetch &lt;code&gt;total_cents&lt;/code&gt;. Thirteen milliseconds became 471. And these are warm numbers. The first time the 106-row uncovered query ran against a cold buffer pool it took 259 ms, because 106 scattered heap pages had to come off disk. The cold number is the one the p99 sees.&lt;/p&gt;
&lt;div class="note-box"&gt;
 &lt;strong&gt;Where the ORM comes in&lt;/strong&gt;
 &lt;div&gt;ActiveRecord needs &lt;code&gt;.select(:status, :created_at)&lt;/code&gt;, Django needs &lt;code&gt;.only(...)&lt;/code&gt;, Prisma needs an explicit &lt;code&gt;select&lt;/code&gt; block; without them every one of these frameworks fetches every column, and no index covers &lt;code&gt;SELECT *&lt;/code&gt; on a real table. The generated SQL is the contract with the index, not the method call. On the handful of queries that carry the most traffic, the generated statement is worth reading once.&lt;/div&gt;
&lt;/div&gt;

&lt;h2 id="heap-fetches-0-is-a-property-of-the-moment"&gt;Heap Fetches: 0 is a property of the moment
&lt;/h2&gt;&lt;p&gt;The Postgres version of this story has a second act that has nothing to do with the SELECT list. A Postgres index carries no visibility information. It cannot tell whether the row an entry points to is visible to the current transaction, so an index-only scan consults the visibility map, a bitmap with one bit per heap page that says &amp;ldquo;every tuple on this page is visible to everyone.&amp;rdquo; Pages with the bit set are skipped. Pages without it get a heap fetch to check.&lt;/p&gt;
&lt;p&gt;Any write to a page clears its bit. Here is the covered query for customer 42, the one at 0.134 ms with zero heap fetches, after an update to 84 of their orders that touched &lt;code&gt;notes&lt;/code&gt;, a column that isn&amp;rsquo;t in the index and isn&amp;rsquo;t in the query:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Index Only Scan using idx_orders_cust_status_created on orders
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; (actual time=0.071..0.442 rows=266 loops=1)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Heap Fetches: 168
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Buffers: shared hit=350
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Execution Time: 0.442 ms
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;The node still says &lt;code&gt;Index Only Scan&lt;/code&gt;. It went to the heap 168 times. Nothing about the query or the index changed; a write to an unrelated column on the same pages did it. &lt;code&gt;VACUUM&lt;/code&gt; restores the bits (168 fetches dropped to 49 after one pass and to 0 after a second), which is why autovacuum health and index-only scan performance are the same problem on a busy table.&lt;/p&gt;
&lt;div class="warning-box"&gt;
 &lt;strong&gt;Warning&lt;/strong&gt;
 &lt;div&gt;&lt;code&gt;Heap Fetches: 0&lt;/code&gt; in a plan is a statement about the visibility map right now, not about the index. The same query can show zero at 09:00 and thousands at 09:05 after a batch update, with the node name unchanged. When an index-only scan is slower than it should be, read the &lt;code&gt;Heap Fetches&lt;/code&gt; line before touching the index definition, and check &lt;code&gt;pg_stat_user_tables&lt;/code&gt; for when the table was last vacuumed. The &lt;code&gt;pg_visibility&lt;/code&gt; extension will tell you exactly how many pages have lost their bit.&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;This is also why adding the column to the index was not the end of the test. With the extended index, customer 777&amp;rsquo;s query came up at 29.5 ms, an 80 percent recovery, but with &lt;code&gt;Heap Fetches: 4101&lt;/code&gt;, because a 100,000-row update I had run against an unrelated range of orders had cleared the bit on 4,101 pages that also happened to hold seller 777&amp;rsquo;s rows. The next autovacuum cleared it. Coverage got the plan back; vacuum got the performance back.&lt;/p&gt;
&lt;h2 id="extend-the-index-or-shrink-the-projection"&gt;Extend the index, or shrink the projection
&lt;/h2&gt;&lt;p&gt;Two fixes hold up, and they fail in different places.&lt;/p&gt;
&lt;p&gt;Extending the index puts &lt;code&gt;total_cents&lt;/code&gt; in the leaf entries so the query never needs the heap. Postgres has &lt;code&gt;INCLUDE&lt;/code&gt; for this since version 11: the column rides along in the leaf pages without becoming part of the B-tree ordering, so it costs nothing on the search path.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-sql" data-lang="sql"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;INDEX&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;idx_orders_cust_status_created_incl&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;ON&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;created_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;INCLUDE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total_cents&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;MySQL has no &lt;code&gt;INCLUDE&lt;/code&gt;. The equivalent is to append the column as a regular key column, &lt;code&gt;(customer_id, status, created_at, total_cents)&lt;/code&gt;, and the optimizer picks the covering form when it&amp;rsquo;s there; on the 80,115-row case that brought 471 ms back to 10.9 ms. InnoDB secondary indexes already carry the primary key at the leaves, so &lt;code&gt;SELECT id&lt;/code&gt; is covered for free on any secondary index, which is worth remembering before adding &lt;code&gt;id&lt;/code&gt; to one.&lt;/p&gt;
&lt;p&gt;The cost is on the write side, and it&amp;rsquo;s specific. The Postgres &lt;code&gt;INCLUDE&lt;/code&gt; index grew from 194 MB to 237 MB for one four-byte column across 5 million rows. More to the point, &lt;code&gt;total_cents&lt;/code&gt; is a column that changes: orders get amended, refunded, partially shipped. Every update to it now has to write a new entry into this index, and on Postgres an update that changes any indexed column can never take the heap-only (HOT) path, so it also writes an entry into every other index on the table. The column that made the list page fast is the column the order-editing path pays for on every save. On a table taking thousands of writes a second, that trade deserves a number before it ships, not after.&lt;/p&gt;
&lt;p&gt;Shrinking the projection avoids all of that. The list view keeps the covered query, and the total is fetched on demand for the one order the user expands, or the list-view columns are frozen as a contract and the index matches the contract. This is the cheaper fix when the new column is decorative and the harder one to hold, because the next feature request also wants one more column. Whichever way it goes, naming the index after the query it serves (&lt;code&gt;idx_orders_list_view&lt;/code&gt;) and pointing a comment on the query back at the index makes the dependency visible to the next person asked to &amp;ldquo;just add a column to the list page.&amp;rdquo;&lt;/p&gt;
&lt;p&gt;For the queries that carry real traffic, the &lt;code&gt;EXPLAIN (ANALYZE, BUFFERS)&lt;/code&gt; output belongs in the pull request next to the SQL. The &lt;code&gt;Buffers&lt;/code&gt; line is the one to compare: 34 pages to 270 is a shrug, 1,020 to 166,667 is the incident, and neither shows up in a diff that adds one column.&lt;/p&gt;</description></item><item><title>It's Almost Always the Queries, Part III: When the CPU Is Pegged</title><link>https://explainanalyze.com/p/its-almost-always-the-queries-part-iii-when-the-cpu-is-pegged/</link><pubDate>Tue, 19 May 2026 00:00:00 +0000</pubDate><guid>https://explainanalyze.com/p/its-almost-always-the-queries-part-iii-when-the-cpu-is-pegged/</guid><description>&lt;img src="https://explainanalyze.com/" alt="Featured image of post It's Almost Always the Queries, Part III: When the CPU Is Pegged" /&gt;&lt;div class="tldr-box"&gt;
 &lt;strong&gt;TL;DR&lt;/strong&gt;
 &lt;div&gt;A database pinned on CPU is rarely running one expensive query. More often a cheap query runs very often, and both the slow-query log and any list sorted by average time put it at the bottom. Sorting by total time finds it, and the fixes are ways to run it less or to make each run read fewer rows. A workload of evenly expensive queries is the exception, and that one needs the cores.&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;The habit when a database is busy is to look for the slow query. It is a reasonable habit, because the slow-query log exists for exactly that and a 500 ms statement is easy to feel bad about. But CPU is spent by time per call multiplied by calls, and the log only looks at the first number.&lt;/p&gt;
&lt;p&gt;To see how far apart those two views get, I ran four statements against the 5-million-row &lt;code&gt;orders&lt;/code&gt; table from &lt;a class="link" href="https://explainanalyze.com/p/its-almost-always-the-queries-part-i-why-metal-doesnt-help/" &gt;Part I&lt;/a&gt; for a minute, at 4,300 queries a second on a 2-CPU PostgreSQL 18 server. The mix is an ordinary one: a count for a badge, a lookup by primary key, an order list showing an account&amp;rsquo;s 20 newest orders, and a 30-day revenue report that scans the whole table and ran about once every two seconds.&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Statement&lt;/th&gt;
 &lt;th&gt;Calls in 60 s&lt;/th&gt;
 &lt;th&gt;Mean per call&lt;/th&gt;
 &lt;th&gt;Total time&lt;/th&gt;
 &lt;th&gt;Share of total&lt;/th&gt;
 &lt;th&gt;In the slow log&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;Order list, 20 newest&lt;/td&gt;
 &lt;td&gt;17,718&lt;/td&gt;
 &lt;td&gt;3.0 ms&lt;/td&gt;
 &lt;td&gt;53.8 s&lt;/td&gt;
 &lt;td&gt;72%&lt;/td&gt;
 &lt;td&gt;0&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;30-day report&lt;/td&gt;
 &lt;td&gt;35&lt;/td&gt;
 &lt;td&gt;503.6 ms&lt;/td&gt;
 &lt;td&gt;17.6 s&lt;/td&gt;
 &lt;td&gt;24%&lt;/td&gt;
 &lt;td&gt;35&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Open-order count&lt;/td&gt;
 &lt;td&gt;179,190&lt;/td&gt;
 &lt;td&gt;0.013 ms&lt;/td&gt;
 &lt;td&gt;2.3 s&lt;/td&gt;
 &lt;td&gt;3%&lt;/td&gt;
 &lt;td&gt;0&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Lookup by id&lt;/td&gt;
 &lt;td&gt;59,874&lt;/td&gt;
 &lt;td&gt;0.009 ms&lt;/td&gt;
 &lt;td&gt;0.5 s&lt;/td&gt;
 &lt;td&gt;1%&lt;/td&gt;
 &lt;td&gt;0&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;em&gt;PostgreSQL 18, 2 CPUs, &lt;code&gt;pg_stat_statements&lt;/code&gt; after one 60 s run, slow log threshold 100 ms. The server averaged 55% CPU.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="Two bar panels over the same four statements. Sorted by mean time: the 30-day report at 503.6 ms, then the order list at 3.0 ms, the count at 0.013 ms and the lookup at 0.009 ms. Sorted by total time: the order list at 53.8 s, the report at 17.6 s, the count at 2.3 s and the lookup at 0.5 s." loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://explainanalyze.com/p/its-almost-always-the-queries-part-iii-when-the-cpu-is-pegged/mean-vs-total.svg"&gt;&lt;/p&gt;
&lt;p&gt;The slow log recorded 35 statements in that minute and every one was the report. Sorted by mean time the report is 166 times slower than anything else. And it accounts for a quarter of the time the server spent running queries. The order list, at 3 ms a call, has nearly three quarters, and it never came within 40 ms of the log&amp;rsquo;s threshold.&lt;/p&gt;
&lt;p&gt;MySQL 8.4 gave the same ranking from &lt;code&gt;events_statements_summary_by_digest&lt;/code&gt;. The order list had 204 s of total time against 27 s for the report, and of the 50 slow-log entries, 31 were the report.&lt;/p&gt;
&lt;h2 id="a-faster-report-would-not-have-helped"&gt;A faster report would not have helped
&lt;/h2&gt;&lt;p&gt;The first move is to fix the report, since it is the only statement with evidence against it. Rewriting it or moving it to a replica is worth doing, and it would give back 24% of the query time. The server would be busy again at the next step in traffic, because the statement that grows with every page view is still there. The bigger instance has the same problem, and &lt;a class="link" href="https://explainanalyze.com/p/its-almost-always-the-queries-part-i-why-metal-doesnt-help/" &gt;Part I&lt;/a&gt; measured it: four times the CPUs served four times the queries.&lt;/p&gt;
&lt;p&gt;What finds the order list is the sort order. In &lt;code&gt;pg_stat_statements&lt;/code&gt; it is &lt;code&gt;total_exec_time&lt;/code&gt;, and on MySQL &lt;code&gt;SUM_TIMER_WAIT&lt;/code&gt; in &lt;code&gt;performance_schema.events_statements_summary_by_digest&lt;/code&gt;. Two more columns say why a statement is there. This is a second run of the same mix, a little slower because later tests had grown the table to 6.1 million rows:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;span class="lnt"&gt;6
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; query | calls | mean_ms | total_ms | pct | pages_per_call | rows_per_call
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;------------------------------------+---------+---------+----------+-----+----------------+---------------
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; SELECT id, total_cents, created_at | 17900 | 5.520 | 98805 | 77 | 2459 | 20
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; SELECT status, count(*), sum(total | 30 | 732.103 | 21963 | 17 | 81931 | 4
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; SELECT id, account_id, status, tot | 59427 | 0.070 | 4180 | 3 | 4 | 1
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; SELECT count(*) FROM orders WHERE | 180674 | 0.015 | 2759 | 2 | 5 | 1
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;&lt;code&gt;pages_per_call&lt;/code&gt; is &lt;code&gt;(shared_blks_hit + shared_blks_read) / calls&lt;/code&gt;. The order list reads 2,459 pages to return 20 rows, which is a query doing more work than its answer needs. The count reads 5 pages 180,674 times, which is a query that is as cheap as it will get and can only be run less.&lt;/p&gt;
&lt;div class="note-box"&gt;
 &lt;strong&gt;Note&lt;/strong&gt;
 &lt;div&gt;Two things about these views. &lt;code&gt;pg_stat_statements&lt;/code&gt; adds up everything since the last &lt;code&gt;pg_stat_statements_reset()&lt;/code&gt; or restart, so the top of the list is history. During an incident, compare two snapshots a minute apart, or reset and wait. And total time is elapsed time, so it includes waiting on locks and disk. A statement can lead the list while using no CPU at all. The pages column separates the two.&lt;/div&gt;
&lt;/div&gt;

&lt;h2 id="what-a-count-costs-even-with-the-right-index"&gt;What a count costs, even with the right index
&lt;/h2&gt;&lt;p&gt;The count in that mix was cheap because Part I gave it an index on &lt;code&gt;(account_id, status)&lt;/code&gt; and the account had 98 open orders. That index does not make a count free. It makes the cost follow the number of rows counted, and that number belongs to the customer.&lt;/p&gt;
&lt;p&gt;A database cannot keep one stored total to hand back. Under MVCC two transactions can correctly see different counts at the same moment, and the &lt;a class="link" href="https://wiki.postgresql.org/wiki/Slow_Counting" target="_blank" rel="noopener"
 &gt;PostgreSQL wiki&lt;/a&gt; says so directly: &amp;ldquo;PostgreSQL must walk through all rows to determine visibility.&amp;rdquo; The &lt;a class="link" href="https://dev.mysql.com/doc/refman/8.4/en/aggregate-functions.html" target="_blank" rel="noopener"
 &gt;MySQL manual&lt;/a&gt; gives the same reason for InnoDB, which &amp;ldquo;does not keep an internal count of rows in a table because concurrent transactions might &amp;lsquo;see&amp;rsquo; different numbers of rows at the same time.&amp;rdquo; (MyISAM did keep one, which is where the old advice that counting is free came from.)&lt;/p&gt;
&lt;p&gt;I added four larger accounts to the table and timed three ways of answering &amp;ldquo;how many open orders&amp;rdquo;: the exact count, a count capped at 100, and the planner&amp;rsquo;s estimate read out of &lt;code&gt;EXPLAIN&lt;/code&gt;.&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Open orders in the account&lt;/th&gt;
 &lt;th&gt;Exact count&lt;/th&gt;
 &lt;th&gt;Capped at 100&lt;/th&gt;
 &lt;th&gt;Planner estimate&lt;/th&gt;
 &lt;th&gt;Estimate with extended statistics&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;98&lt;/td&gt;
 &lt;td&gt;0.04 ms&lt;/td&gt;
 &lt;td&gt;0.04 ms&lt;/td&gt;
 &lt;td&gt;540&lt;/td&gt;
 &lt;td&gt;529&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;1,000&lt;/td&gt;
 &lt;td&gt;0.06 ms&lt;/td&gt;
 &lt;td&gt;0.04 ms&lt;/td&gt;
 &lt;td&gt;540&lt;/td&gt;
 &lt;td&gt;529&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;10,000&lt;/td&gt;
 &lt;td&gt;0.32 ms&lt;/td&gt;
 &lt;td&gt;0.04 ms&lt;/td&gt;
 &lt;td&gt;2,215&lt;/td&gt;
 &lt;td&gt;13,444&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;100,000&lt;/td&gt;
 &lt;td&gt;2.80 ms&lt;/td&gt;
 &lt;td&gt;0.04 ms&lt;/td&gt;
 &lt;td&gt;23,215&lt;/td&gt;
 &lt;td&gt;99,408&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;1,000,000&lt;/td&gt;
 &lt;td&gt;17.65 ms&lt;/td&gt;
 &lt;td&gt;0.04 ms&lt;/td&gt;
 &lt;td&gt;221,520&lt;/td&gt;
 &lt;td&gt;992,854&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;em&gt;PostgreSQL 18, one connection, median of an 8 s run each, all pages marked all-visible (the fast-path condition explained below). The estimate took 0.06 ms at every size. The last two columns are the estimated row counts, not times.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="Bar chart of count time by number of matching rows. Exact count: 0.04 ms for 98 rows, 0.06 ms for 1,000, 0.32 ms for 10,000, 2.80 ms for 100,000 and 17.65 ms for 1,000,000. The count capped at 100 takes 0.04 ms at every size." loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://explainanalyze.com/p/its-almost-always-the-queries-part-iii-when-the-cpu-is-pegged/count-cost.svg"&gt;&lt;/p&gt;
&lt;p&gt;The exact count is fine for the 98-order account and costs 440 times more for the largest one, so the badge is cheapest for the customers who matter least. The capped version stops after 100 index entries and stays at 0.04 ms:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-sql" data-lang="sql"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;&lt;span class="w"&gt; &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="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;account_id&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;AND&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;open&amp;#39;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;LIMIT&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;The estimate surprised me. I expected it to be close, and it was off by roughly five times at both ends: 540 for a real 98, and 221,520 for a real million. The planner multiplies the share of rows with that account by the share with that status as if the two were unrelated, and here they are not. &lt;code&gt;CREATE STATISTICS ... (mcv, dependencies) ON account_id, status&lt;/code&gt; brought the three large accounts to within 35% (13,444 for 10,000 was the worst), and left the small ones at 529. An estimate is usable for &amp;ldquo;about a million&amp;rdquo; and not for a badge that says 98.&lt;/p&gt;
&lt;p&gt;Answering from the index alone, which the plan calls an index-only scan, has its own condition. PostgreSQL can do it only for pages the visibility map marks all-visible, meaning every row on the page is visible to every transaction, and it is &lt;code&gt;VACUUM&lt;/code&gt; that sets those marks. For any other page it goes to the table to check the row, and the plan counts each of those trips as a heap fetch. I updated 10% of the 100,000-order account&amp;rsquo;s rows and ran the count again:&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;State of the table&lt;/th&gt;
 &lt;th&gt;Heap fetches&lt;/th&gt;
 &lt;th&gt;Pages read&lt;/th&gt;
 &lt;th&gt;Time&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;All pages all-visible&lt;/td&gt;
 &lt;td&gt;0&lt;/td&gt;
 &lt;td&gt;82&lt;/td&gt;
 &lt;td&gt;5.2 ms&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;After updating 10,000 of the account&amp;rsquo;s rows&lt;/td&gt;
 &lt;td&gt;110,000&lt;/td&gt;
 &lt;td&gt;1,558&lt;/td&gt;
 &lt;td&gt;10.7 ms&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;After a plain &lt;code&gt;VACUUM&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;100,000&lt;/td&gt;
 &lt;td&gt;1,424&lt;/td&gt;
 &lt;td&gt;9.2 ms&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;After &lt;code&gt;VACUUM (INDEX_CLEANUP ON)&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;0&lt;/td&gt;
 &lt;td&gt;90&lt;/td&gt;
 &lt;td&gt;not re-timed&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;em&gt;&lt;code&gt;EXPLAIN (ANALYZE, BUFFERS)&lt;/code&gt; on PostgreSQL 18, autovacuum off for the table during the test. These times are higher than the table above because &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; adds its own overhead.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The third row is the one I did not expect. &lt;code&gt;VACUUM&lt;/code&gt; ran, finished, and the count still went to the table for every row. Its verbose output says why: &lt;code&gt;index scan bypassed: 1334 pages from table (1.63% of total) have 10000 dead item identifiers&lt;/code&gt;. When few pages hold dead rows, &lt;code&gt;VACUUM&lt;/code&gt; skips the index pass to save work (the &lt;a class="link" href="https://www.postgresql.org/docs/current/sql-vacuum.html" target="_blank" rel="noopener"
 &gt;documentation&lt;/a&gt; describes this as the default &lt;code&gt;AUTO&lt;/code&gt; behavior of &lt;code&gt;INDEX_CLEANUP&lt;/code&gt;), and a page that still holds dead item identifiers, the stubs removed rows leave behind until the index pass clears them, cannot be marked all-visible. So on a big table where a small, busy slice keeps changing, the slice a count reads can stay off the fast path through several vacuums. It doubled the cost here. It did not break anything, and it is the sort of thing that makes a count slower over months with no change to the query.&lt;/p&gt;
&lt;h2 id="more-connections-than-cores"&gt;More connections than cores
&lt;/h2&gt;&lt;p&gt;The other way to waste CPU has nothing to do with any one statement. I ran the order list from 2 connections up to 500 against the same 2-CPU server, each connection sending as fast as it could.&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Connections&lt;/th&gt;
 &lt;th&gt;Queries per second&lt;/th&gt;
 &lt;th&gt;p50&lt;/th&gt;
 &lt;th&gt;p95&lt;/th&gt;
 &lt;th&gt;p99&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;2&lt;/td&gt;
 &lt;td&gt;961&lt;/td&gt;
 &lt;td&gt;1.9 ms&lt;/td&gt;
 &lt;td&gt;2.8 ms&lt;/td&gt;
 &lt;td&gt;4.8 ms&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;4&lt;/td&gt;
 &lt;td&gt;915&lt;/td&gt;
 &lt;td&gt;4.3 ms&lt;/td&gt;
 &lt;td&gt;7.3 ms&lt;/td&gt;
 &lt;td&gt;12.0 ms&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;16&lt;/td&gt;
 &lt;td&gt;882&lt;/td&gt;
 &lt;td&gt;19.1 ms&lt;/td&gt;
 &lt;td&gt;32.3 ms&lt;/td&gt;
 &lt;td&gt;42.8 ms&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;64&lt;/td&gt;
 &lt;td&gt;917&lt;/td&gt;
 &lt;td&gt;73.5 ms&lt;/td&gt;
 &lt;td&gt;126.3 ms&lt;/td&gt;
 &lt;td&gt;161.4 ms&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;200&lt;/td&gt;
 &lt;td&gt;845&lt;/td&gt;
 &lt;td&gt;236.5 ms&lt;/td&gt;
 &lt;td&gt;417.0 ms&lt;/td&gt;
 &lt;td&gt;539.3 ms&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;500&lt;/td&gt;
 &lt;td&gt;666&lt;/td&gt;
 &lt;td&gt;639.6 ms&lt;/td&gt;
 &lt;td&gt;1,555.4 ms&lt;/td&gt;
 &lt;td&gt;2,468.7 ms&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;em&gt;PostgreSQL 18, 2 CPUs, 25 s per run, CPU at 95–100% throughout.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="Bar chart of queries per second by connection count on 2 CPUs: 961 with 2 connections, 915 with 4, 856 with 8, 882 with 16, 917 with 64, 845 with 200 and 666 with 500. The 95th-percentile response time under each label rises from 2.8 ms to 1,555 ms." loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://explainanalyze.com/p/its-almost-always-the-queries-part-iii-when-the-cpu-is-pegged/pool-size.svg"&gt;&lt;/p&gt;
&lt;p&gt;This came out milder than the usual warning. From 4 to 200 connections the server finished within 12% of its best run, so a pool ten times too large cost little throughput. At 500 it finished 31% fewer queries, with the CPU graph still reading 96%. That missing third is the server switching between 500 processes on 2 cores.&lt;/p&gt;
&lt;p&gt;The response times need a caveat, because in a test like this more connections always means more waiting. What the table shows is where the waiting happens. With 500 connections each query waits inside the database, holding a server process (a backend) and its memory. With a pooler such as PgBouncer set near the core count, the same queries wait in the pooler and the database runs the 4-connection row. The cost is a queue to watch, and a pool set too small becomes its own outage when every slot is held by something slow.&lt;/p&gt;
&lt;h2 id="the-fixes-and-what-each-one-costs"&gt;The fixes, and what each one costs
&lt;/h2&gt;&lt;p&gt;The order list was the statement worth fixing. An index is not the only way to make a statement cheaper, and which way fits depends on what the answer is used for. The one I measured for the list is an index that hands back rows already in order:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-sql" data-lang="sql"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;INDEX&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;orders_account_created_idx&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;ON&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;account_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;created_at&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;The plan went from an index scan plus a sort over 2,500 rows (2,461 pages, 4.98 ms) to reading the first 20 entries (23 pages, 0.14 ms). On the same 2 CPUs with 4 connections, the ceiling for that query went from 915 a second to 63,913. The index is 158 MB on the 6.1-million-row table and every insert now maintains it, a write cost I did not measure.&lt;/p&gt;
&lt;p&gt;A cache in front of the query is the other common route. The first page of an account&amp;rsquo;s orders, kept in the application or a cache server for a few seconds, takes the statement off the top of the list without touching the schema. What it costs is a list that can be stale by the cache lifetime, plus invalidation on every write wherever stale is not acceptable. I did not measure it.&lt;/p&gt;
&lt;p&gt;A count has more options than a list does, because many counts do not need to be exact, or current, or either.&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Way to answer &amp;ldquo;how many&amp;rdquo;&lt;/th&gt;
 &lt;th&gt;What comes back&lt;/th&gt;
 &lt;th&gt;What it costs&lt;/th&gt;
 &lt;th&gt;Where it fits&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;Exact count on the covering index&lt;/td&gt;
 &lt;td&gt;Exact, current&lt;/td&gt;
 &lt;td&gt;Grows with the rows counted: 0.04 ms for 98, 17.65 ms for 1,000,000&lt;/td&gt;
 &lt;td&gt;Small counts, or a number shown rarely&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Count capped with &lt;code&gt;LIMIT 100&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Exact up to the cap&lt;/td&gt;
 &lt;td&gt;Flat at 0.04 ms; the interface says &amp;ldquo;99+&amp;rdquo;&lt;/td&gt;
 &lt;td&gt;Badges&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Cached count, in the application or a summary table&lt;/td&gt;
 &lt;td&gt;Exact as of the last refresh&lt;/td&gt;
 &lt;td&gt;One count per refresh interval, and a number that is stale by that interval&lt;/td&gt;
 &lt;td&gt;Dashboards, and anything nobody acts on within the minute&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Counter kept up to date on write&lt;/td&gt;
 &lt;td&gt;Exact, current&lt;/td&gt;
 &lt;td&gt;Every insert and status change also updates the counter row, and writers sharing one counter queue on it&lt;/td&gt;
 &lt;td&gt;A number that must be exact and is read far more often than it changes&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Planner estimate for the filter, read from &lt;code&gt;EXPLAIN&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Approximate&lt;/td&gt;
 &lt;td&gt;Flat at 0.06 ms; off by about five times here until extended statistics were added&lt;/td&gt;
 &lt;td&gt;Order-of-magnitude decisions about a filtered set&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Table statistics from the catalog&lt;/td&gt;
 &lt;td&gt;Approximate, whole table only&lt;/td&gt;
 &lt;td&gt;One catalog read; as fresh as the last &lt;code&gt;ANALYZE&lt;/code&gt; or autovacuum run&lt;/td&gt;
 &lt;td&gt;Guardrails&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;em&gt;The exact, capped and planner-estimate rows are the measurements from the count section above. The cached count, the counter and the catalog read are reasoned from how they work; I did not run them. A per-account counter spreads the write queue out, and a single global one concentrates it.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The last row is the one that tends to be overlooked. A lot of counts are guardrails. A build or migration job that checks how big a table is, to decide whether it can run now or has to wait until after hours, does not need the number. It needs to know whether the table holds ten thousand rows or a hundred million. &lt;code&gt;SELECT reltuples FROM pg_class WHERE relname = 'orders'&lt;/code&gt; answers that from the catalog without reading the table, and on MySQL &lt;code&gt;information_schema.TABLES.TABLE_ROWS&lt;/code&gt; does the same job, as an estimate that the manual itself calls rough for InnoDB. Running &lt;code&gt;count(*)&lt;/code&gt; for that question reads the whole table to find out whether reading the whole table is safe.&lt;/p&gt;
&lt;div class="warning-box"&gt;
 &lt;strong&gt;Warning&lt;/strong&gt;
 &lt;div&gt;A badge in the global navigation is the expensive place for a count. It runs on every page of every signed-in user, so its call count follows total page views instead of visits to one screen. That is the pattern to look for in the calls column: one statement with ten times the calls of anything near it.&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Some servers are busy because the work is real. A reporting database, or a nightly load, will sit at high CPU with a top-ten list of statements that each read about as many pages as their answers need. That list has no outlier in calls and none in pages per row, and more cores is the right purchase. The check before buying them is one query, sorted by total time, with calls and pages per call beside it. In the run above the order list is the first line of that output: 17,900 calls, 2,459 pages each, 20 rows back.&lt;/p&gt;</description></item><item><title>It's Almost Always the Queries, Part II: Troubleshooting Steps</title><link>https://explainanalyze.com/p/its-almost-always-the-queries-part-ii-troubleshooting-steps/</link><pubDate>Sun, 17 May 2026 00:00:00 +0000</pubDate><guid>https://explainanalyze.com/p/its-almost-always-the-queries-part-ii-troubleshooting-steps/</guid><description>&lt;img src="https://explainanalyze.com/" alt="Featured image of post It's Almost Always the Queries, Part II: Troubleshooting Steps" /&gt;&lt;div class="tldr-box"&gt;
 &lt;strong&gt;TL;DR&lt;/strong&gt;
 &lt;div&gt;Seven steps for a database that is slow right now: protect your own session, list what is running, look for obvious offenders, check the schema, read the execution plan, check query volume, and read the dashboards. Each step says what to run on PostgreSQL and MySQL and what the output means. It is written for engineers who don&amp;rsquo;t do this often.&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;When an application slows down and nothing was deployed, the usual first moves are to kill the longest-running query, restart the connection pool, or add an index. Each of those is a guess. A guess during an incident can make things worse, because the longest-running query is often a victim waiting on something else, and killing it leaves the cause in place. The steps below go in a fixed order so that by the time you act, you know what you are acting on.&lt;/p&gt;
&lt;p&gt;None of the seven steps is comprehensive. The full diagnostic surface (lock graphs, planner cost tuning, statistics, vacuum internals) takes years to learn and a book to lay out. What&amp;rsquo;s here is enough to triage with confidence and escalate with specifics, not enough to skip the call to whoever owns the database.&lt;/p&gt;
&lt;h2 id="the-seven-steps"&gt;The seven steps
&lt;/h2&gt;&lt;p&gt;Steps 1 to 6 assume you can connect to the primary (or a replica that exposes the same views) and run SQL. System tables show what is happening right now. Dashboards show what happened over the past hour, and Step 7 covers those. If you have no SQL access at all, start at Step 7.&lt;/p&gt;
&lt;h3 id="step-1-protect-your-own-session"&gt;Step 1: Protect your own session
&lt;/h3&gt;&lt;p&gt;Before you run anything, put a time limit on your own queries:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-sql" data-lang="sql"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;-- PostgreSQL
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;SET&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;statement_timeout&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;5s&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;-- MySQL (per-query)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cm"&gt;/*+ MAX_EXECUTION_TIME(5000) */&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;If the database is under heavy pressure, your diagnostic query competes for the same resources. A five-second timeout means your debugging doesn&amp;rsquo;t pile onto the problem. And if a diagnostic query can&amp;rsquo;t finish in five seconds, that tells you something too: the server is badly contended, or short of memory or disk throughput.&lt;/p&gt;
&lt;h3 id="step-2-list-what-is-running"&gt;Step 2: List what is running
&lt;/h3&gt;&lt;p&gt;The first thing you want to see is the process list, filtered to active queries and ordered by time. On PostgreSQL that&amp;rsquo;s &lt;code&gt;pg_stat_activity&lt;/code&gt;. On MySQL that&amp;rsquo;s &lt;code&gt;SHOW PROCESSLIST&lt;/code&gt; or, better, &lt;code&gt;performance_schema.processlist&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Pull the active sessions with the columns that help you categorize them:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;span class="lnt"&gt;12
&lt;/span&gt;&lt;span class="lnt"&gt;13
&lt;/span&gt;&lt;span class="lnt"&gt;14
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-sql" data-lang="sql"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;-- PostgreSQL
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;wait_event_type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;wait_event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;xact_start&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;AS&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;xact_duration&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;query_start&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;AS&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;query_duration&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;pg_blocking_pids&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;AS&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;blocked_by&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;LEFT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;AS&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;query_snippet&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;pg_stat_activity&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;state&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;!=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;idle&amp;#39;&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;AND&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;!=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;pg_backend_pid&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;ORDER&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;BY&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;xact_start&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;NULLS&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;LAST&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Two columns need a word. &lt;code&gt;state&lt;/code&gt; is &lt;code&gt;active&lt;/code&gt; for a query that is running, and &lt;code&gt;idle in transaction&lt;/code&gt; for a session that opened a transaction, finished its last statement, and has not committed. That session is doing nothing, but it still holds its locks.&lt;/p&gt;
&lt;p&gt;Most diagnostic snippets you&amp;rsquo;ll find online show &lt;code&gt;pid&lt;/code&gt;, &lt;code&gt;query&lt;/code&gt;, &lt;code&gt;state&lt;/code&gt;, and duration. They skip the two columns that say why a query is taking long: &lt;code&gt;wait_event_type&lt;/code&gt; is the category of thing the query is waiting on, and &lt;code&gt;wait_event&lt;/code&gt; is the specific thing. Empty values mean the query is not waiting on anything the server tracks, which usually means it is using CPU. &lt;code&gt;Lock&lt;/code&gt; with &lt;code&gt;transactionid&lt;/code&gt; means it is waiting on another transaction&amp;rsquo;s row lock, and &lt;code&gt;blocked_by&lt;/code&gt; names the session that holds it. &lt;code&gt;IO&lt;/code&gt; with &lt;code&gt;DataFileRead&lt;/code&gt; means it is reading a page from disk because the page was not in memory. PostgreSQL documents &lt;a class="link" href="https://www.postgresql.org/docs/current/monitoring-stats.html#WAIT-EVENT-TABLE" target="_blank" rel="noopener"
 &gt;the full list&lt;/a&gt;, with dozens of values.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;span class="lnt"&gt;12
&lt;/span&gt;&lt;span class="lnt"&gt;13
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-sql" data-lang="sql"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;-- MySQL (use performance_schema, not INFORMATION_SCHEMA)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PROCESSLIST_ID&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;AS&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PROCESSLIST_STATE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;AS&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PROCESSLIST_TIME&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;AS&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;duration_sec&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;BLOCKING_THREAD_ID&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;AS&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;blocked_by_thread&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;LEFT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PROCESSLIST_INFO&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;AS&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;query_snippet&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;performance_schema&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;threads&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;LEFT&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;JOIN&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;performance_schema&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data_lock_waits&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;ON&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;THREAD_ID&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;REQUESTING_THREAD_ID&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PROCESSLIST_COMMAND&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;!=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;Sleep&amp;#39;&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;AND&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;TYPE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;FOREGROUND&amp;#39;&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;ORDER&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;BY&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PROCESSLIST_TIME&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;div class="warning-box"&gt;
 &lt;strong&gt;Warning&lt;/strong&gt;
 &lt;div&gt;On MySQL, use &lt;code&gt;performance_schema.threads&lt;/code&gt; or &lt;code&gt;performance_schema.processlist&lt;/code&gt; (8.0.22+), not &lt;code&gt;INFORMATION_SCHEMA.PROCESSLIST&lt;/code&gt;. &lt;a class="link" href="https://bugs.mysql.com/bug.php?id=94077" target="_blank" rel="noopener"
 &gt;MySQL Bug #94077&lt;/a&gt; (January 2019) documented a 70% performance drop from polling &lt;code&gt;INFORMATION_SCHEMA.PROCESSLIST&lt;/code&gt; under load. &lt;a class="link" href="https://bugs.mysql.com/bug.php?id=100049" target="_blank" rel="noopener"
 &gt;Bug #100049&lt;/a&gt; (June 2020) showed the same query causing pending queries to pile up until the server became unresponsive. Both trace to a mutex held during execution. The diagnostic query you run during an incident should not itself become part of the incident.&lt;/div&gt;
&lt;/div&gt;

&lt;h3 id="step-3-look-for-obvious-offenders"&gt;Step 3: Look for obvious offenders
&lt;/h3&gt;&lt;p&gt;Start with the longest-running queries in the output. Some problems are visible from the query snippet alone. A &lt;code&gt;SELECT *&lt;/code&gt; with no WHERE clause on a large table, a function wrapping a column in the predicate (&lt;a class="link" href="https://explainanalyze.com/p/non-sargable-predicates-how-a-function-in-where-kills-your-index/" &gt;non-SARGable&lt;/a&gt;), a COUNT(*) over millions of rows, an N+1 pattern showing up as dozens of identical queries with different IDs. If you recognize the shape, you already know what to fix.&lt;/p&gt;
&lt;p&gt;Also look for DDL in the list. An &lt;code&gt;ALTER TABLE&lt;/code&gt;, &lt;code&gt;CREATE INDEX&lt;/code&gt;, or &lt;code&gt;DROP INDEX&lt;/code&gt; that&amp;rsquo;s been running longer than you&amp;rsquo;d expect is usually not doing the work it looks like it&amp;rsquo;s doing. It&amp;rsquo;s waiting on a lock. On MySQL this shows up as a metadata lock (MDL): the DDL waits for every transaction still holding the table open to commit or roll back, and meanwhile every new query against the table queues behind the DDL. On PostgreSQL the equivalent is an &lt;code&gt;ACCESS EXCLUSIVE&lt;/code&gt; lock at the relation level, with the same cascade: the DDL waits on active transactions, and everything else waits on the DDL.&lt;/p&gt;
&lt;p&gt;The non-obvious version is when no heavy query is running but the server is wedged anyway. The trigger is usually something innocuous: a connection pooler holding a session &lt;code&gt;idle in transaction&lt;/code&gt;, an analytics job that opened a transaction and never closed it, a long SELECT (which holds a lock on its table until it finishes, &lt;code&gt;ACCESS SHARE&lt;/code&gt; on PostgreSQL), or an autovacuum touching the same table. The DDL blocks on whichever of those it is, then every new query against the table queues behind the DDL. The process list shows a wall of waiting sessions against one table, the DDL at the head of the queue, and the actual root cause somewhere further down (or in a connection that doesn&amp;rsquo;t look problematic at all).&lt;/p&gt;
&lt;p&gt;If nothing jumps out from the query text alone, move to Step 4 and Step 5 to dig deeper into the suspect query. If nothing looks slow at all, skip to Step 6.&lt;/p&gt;
&lt;h3 id="step-4-check-the-schema"&gt;Step 4: Check the schema
&lt;/h3&gt;&lt;p&gt;Once you have a suspect query, look at the table it&amp;rsquo;s hitting. Pull the table definition (&lt;code&gt;\d tablename&lt;/code&gt; in psql, &lt;code&gt;SHOW CREATE TABLE tablename&lt;/code&gt; in MySQL) and compare it against what the query needs.&lt;/p&gt;
&lt;p&gt;Walk the clauses one at a time. WHERE columns need indexes the optimizer can actually use, and &amp;lsquo;usable&amp;rsquo; depends on the predicate shape: equality on a high-cardinality column is the straightforward case, range predicates (&lt;code&gt;&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;&lt;/code&gt;, &lt;code&gt;BETWEEN&lt;/code&gt;) work but constrain what can follow them in a composite, and a large &lt;code&gt;IN (...)&lt;/code&gt; list may flip the planner to a sequential scan (reading the whole table from start to end) even when an index exists. JOIN columns need an index on the inner side of the join (the side being looked up once per outer row); in PostgreSQL, foreign-key columns are not indexed automatically, which is a frequent cause of joins that ran fine at low volume and fell over at scale. ORDER BY can sometimes use an index to skip the sort entirely, but only when the index&amp;rsquo;s leading columns line up with the ORDER BY columns. GROUP BY is the same shape: an index on the grouping columns lets the planner stream the aggregation instead of building a hash, which on a multi-million-row table can be the difference between a sub-second query and one that exhausts &lt;code&gt;work_mem&lt;/code&gt; (the memory PostgreSQL allows one sort or hash before it writes to temporary files) and spills to disk.&lt;/p&gt;
&lt;p&gt;A missing index on a high-cardinality filter column (one with many distinct values) is a very common cause of queries that worked fine at low volume and fell over at scale. Composite index column order is the runner-up. An index on &lt;code&gt;(status, created_at)&lt;/code&gt; serves &lt;code&gt;WHERE status = 'pending' ORDER BY created_at&lt;/code&gt;. An index on &lt;code&gt;(created_at, status)&lt;/code&gt; does not, even though it contains the same columns. The general rule is equality columns first, then the range column, then columns used only for sort.&lt;/p&gt;
&lt;p&gt;Also check for &lt;a class="link" href="https://explainanalyze.com/p/one-column-added-to-a-select-1020-page-reads-became-166667/" &gt;covering index gaps&lt;/a&gt;: an index that covered the query last month might have stopped covering it after a column was added to the SELECT list, forcing a visit to the table for every row, where the query used to be answered from the index alone (an index-only scan).&lt;/p&gt;
&lt;h3 id="step-5-read-the-execution-plan"&gt;Step 5: Read the execution plan
&lt;/h3&gt;&lt;p&gt;If the schema looks right and you still can&amp;rsquo;t explain the behavior, ask the database what it&amp;rsquo;s actually doing. &lt;a class="link" href="https://www.postgresql.org/docs/current/sql-explain.html" target="_blank" rel="noopener"
 &gt;&lt;code&gt;EXPLAIN&lt;/code&gt;&lt;/a&gt; (PostgreSQL) or &lt;a class="link" href="https://dev.mysql.com/doc/refman/8.0/en/explain.html" target="_blank" rel="noopener"
 &gt;&lt;code&gt;EXPLAIN&lt;/code&gt;&lt;/a&gt; (MySQL) shows the planner&amp;rsquo;s chosen strategy without executing the query. &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; executes it and shows actual row counts alongside the estimates. Reading these outputs is the most important skill in query troubleshooting. Every claim about what a query &amp;ldquo;should&amp;rdquo; do is a guess until the plan confirms it; the optimizer might pick a different index than you expect, fall back to a sequential scan because of stale statistics, or choose a join order you&amp;rsquo;d never write by hand. The plan is the ground truth.&lt;/p&gt;
&lt;p&gt;Run this on a replica if you have one. The plan will be the same (assuming similar data and stats), and you avoid adding load to a primary that&amp;rsquo;s already under pressure. If you don&amp;rsquo;t have a replica, plain &lt;code&gt;EXPLAIN&lt;/code&gt; (without ANALYZE) gives you the plan without executing the query. It&amp;rsquo;s an estimate, not a measurement, but it&amp;rsquo;s often enough to spot the problem.&lt;/p&gt;
&lt;p&gt;What to look for in the output: sequential scans on large tables (the planner couldn&amp;rsquo;t find a usable index), rows estimated vs. rows actual diverging by orders of magnitude (stale statistics or a bad cardinality guess), reading a million rows to return ten (missing or ignored index), nested loops where each iteration does its own index lookup against a large table (the N+1 shape at the engine level).&lt;/p&gt;
&lt;div class="warning-box"&gt;
 &lt;strong&gt;Warning&lt;/strong&gt;
 &lt;div&gt;&lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; executes the query fully. On a SELECT that takes 30 seconds in production, it will take 30 seconds when you run it too. If the query modifies data (INSERT, UPDATE, DELETE), wrap it in a transaction and roll back: &lt;code&gt;BEGIN; EXPLAIN ANALYZE UPDATE ...; ROLLBACK;&lt;/code&gt;. On a system already under pressure, be deliberate about what you choose to execute.&lt;/div&gt;
&lt;/div&gt;

&lt;h3 id="step-6-if-nothing-looks-slow-check-volume"&gt;Step 6: If nothing looks slow, check volume
&lt;/h3&gt;&lt;p&gt;Sometimes every query in the process list finishes in a few milliseconds and nothing looks wrong individually. The problem isn&amp;rsquo;t one slow query. It&amp;rsquo;s thousands of fast ones hitting the same resources concurrently.&lt;/p&gt;
&lt;p&gt;This is where digest-level views help. &lt;code&gt;pg_stat_statements&lt;/code&gt; (PostgreSQL) and &lt;code&gt;performance_schema.events_statements_summary_by_digest&lt;/code&gt; (MySQL) aggregate queries by their normalized pattern and track call counts. Sort by &lt;code&gt;total_exec_time&lt;/code&gt; (PostgreSQL) or &lt;code&gt;SUM_TIMER_WAIT&lt;/code&gt; (MySQL), not by mean time. A query that averages 5 ms but fires 10,000 times per second consumes 50 CPU-seconds per wall-second. It will never show up in a &amp;ldquo;longest running&amp;rdquo; list, but it dominates the workload.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;span class="lnt"&gt;12
&lt;/span&gt;&lt;span class="lnt"&gt;13
&lt;/span&gt;&lt;span class="lnt"&gt;14
&lt;/span&gt;&lt;span class="lnt"&gt;15
&lt;/span&gt;&lt;span class="lnt"&gt;16
&lt;/span&gt;&lt;span class="lnt"&gt;17
&lt;/span&gt;&lt;span class="lnt"&gt;18
&lt;/span&gt;&lt;span class="lnt"&gt;19
&lt;/span&gt;&lt;span class="lnt"&gt;20
&lt;/span&gt;&lt;span class="lnt"&gt;21
&lt;/span&gt;&lt;span class="lnt"&gt;22
&lt;/span&gt;&lt;span class="lnt"&gt;23
&lt;/span&gt;&lt;span class="lnt"&gt;24
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-sql" data-lang="sql"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;-- PostgreSQL
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;LEFT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;AS&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;query_snippet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;calls&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;total_exec_time&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;AS&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;total_ms&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total_exec_time&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;calls&lt;/span&gt;&lt;span class="p"&gt;)::&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;AS&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;avg_ms&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;pg_stat_statements&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;ORDER&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;BY&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;total_exec_time&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;LIMIT&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;-- MySQL (join current statements to their digest summary)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;LEFT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DIGEST_TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;AS&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;query_snippet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;COUNT_STAR&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;AS&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;calls&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ROUND&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SUM_TIMER_WAIT&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="n"&gt;e9&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;AS&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;total_ms&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ROUND&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AVG_TIMER_WAIT&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="n"&gt;e9&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;AS&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;avg_ms&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;performance_schema&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;events_statements_current&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;esc&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;JOIN&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;performance_schema&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;events_statements_summary_by_digest&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;ON&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;esc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DIGEST&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DIGEST&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;JOIN&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;performance_schema&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;threads&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;ON&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;esc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;THREAD_ID&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;THREAD_ID&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PROCESSLIST_COMMAND&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;!=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;Sleep&amp;#39;&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;ORDER&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;BY&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SUM_TIMER_WAIT&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;LIMIT&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;This is the case &lt;a class="link" href="https://explainanalyze.com/p/its-almost-always-the-queries-part-i-why-metal-doesnt-help/" &gt;Part I&lt;/a&gt; measures: a count that takes 3 ms, which nobody would open a ticket for, and a slow-query log that stayed nearly silent until the server was saturated and then filled with whatever was queued. &lt;a class="link" href="https://explainanalyze.com/p/its-almost-always-the-queries-part-iii-when-the-cpu-is-pegged/" &gt;Part III&lt;/a&gt; ranks a mixed workload by total time and by mean time and shows how far apart the two lists are.&lt;/p&gt;
&lt;h3 id="step-7-read-the-dashboards"&gt;Step 7: Read the dashboards
&lt;/h3&gt;&lt;p&gt;Dashboards see what the system tables don&amp;rsquo;t: history. By the time you query &lt;code&gt;pg_stat_activity&lt;/code&gt;, you&amp;rsquo;ve lost the picture of what was happening five minutes ago. Whatever you have (Datadog DBM, Aurora and RDS Performance Insights, pganalyze, Grafana with the right exporters), this is where you bring it in. If you have no SQL access at all, the dashboard is your entire diagnostic toolkit; everything you can learn, you&amp;rsquo;ll learn here.&lt;/p&gt;
&lt;p&gt;Start with timing. A graph with a hard step-change at 14:23 is a different problem from one that climbed slowly over the past hour. The hard step points at a discrete event: a deploy, a config change, a single long-running query that started blocking others. The slow climb points at a workload trend or a plan regression that compounded as the working set grew. Overlay deployment markers, autoscaling events, and scheduled job runs on the latency graph if you can. A latency jump that exactly tracks a deploy is the deploy until proven otherwise.&lt;/p&gt;
&lt;p&gt;Active session count is the other timing graph worth pulling. A flat baseline that doubles at 14:23 points at a blocking event. A slow climb over an hour points at workload growth or queue buildup. Session count is harder to lie to than latency: a single slow query can hide in an average, but it can&amp;rsquo;t hide in the count of sessions waiting on it.&lt;/p&gt;
&lt;p&gt;Which resource is pinned tells you which dimension is the cause and which is following. CPU at 100% with IOPS low and stable is a CPU-bound workload, often a missing index causing repeated sorting or hashing, or a regex or JSON predicate doing per-row work the planner can&amp;rsquo;t push down. IOPS pinned with CPU low and waits on &lt;code&gt;IO:DataFileRead&lt;/code&gt; (PostgreSQL) is a buffer-cache miss problem: the working set has outgrown RAM and every query is going to disk. RAM climbing steadily for days while the buffer cache hit rate falls at the same rate is a forecast, not an incident.&lt;/p&gt;
&lt;p&gt;Waits are where dashboards earn their keep. Aurora Performance Insights, pganalyze, and Datadog DBM all stack active sessions by wait class over time, which is exactly the information &lt;code&gt;pg_stat_activity&lt;/code&gt; can&amp;rsquo;t give you historically. Locks dominating means contention: a deadlock storm, or an open long-running transaction blocking everything that touches the same rows. IO dominating means buffer pressure or storage saturation. CPU dominating means the queries themselves are doing more work per call than they used to. Client reads dominating points at a slow consumer: the app isn&amp;rsquo;t reading results fast enough and sessions stack up waiting to send the next page.&lt;/p&gt;
&lt;p&gt;WAL and checkpoint pressure are easy to miss because they don&amp;rsquo;t appear in the active query list. The WAL (write-ahead log) is where PostgreSQL records every change before it reaches the data files, and a checkpoint is the periodic flush of changed pages into those files. WAL generation rate climbing with no proportional traffic increase points at write amplification: a runaway UPDATE rewriting the same rows, a hot index getting bloated, a trigger writing more than it needs to. Checkpoint duration climbing, or checkpoint frequency increasing, means the system can&amp;rsquo;t keep pace with the write rate. On MySQL the same signal shows up as InnoDB log file utilization approaching its configured size, with checkpoint-related stalls visible in &lt;code&gt;SHOW ENGINE INNODB STATUS&lt;/code&gt;. These often correlate with sudden IOPS spikes, because the checkpoint flush is what saturates the disk, not the workload directly.&lt;/p&gt;
&lt;p&gt;Workload composition completes the picture. Most monitoring breaks queries-per-second down by type. A 10x spike in write QPS with read QPS flat is a different incident from a 10x read spike. Within reads, the ratio of index scans to sequential scans is a leading indicator of plan regression: in PostgreSQL, &lt;code&gt;pg_stat_user_tables.seq_scan&lt;/code&gt; climbing on a table that previously got index scans; in MySQL, &lt;code&gt;Handler_read_rnd_next&lt;/code&gt; rising relative to &lt;code&gt;Handler_read_key&lt;/code&gt;. A jump in read-ahead activity (InnoDB&amp;rsquo;s &lt;code&gt;Innodb_buffer_pool_read_ahead&lt;/code&gt;, Aurora&amp;rsquo;s read-IOPS metrics) often signals large scans that weren&amp;rsquo;t there before: a new query, or an old query whose plan changed.&lt;/p&gt;
&lt;p&gt;You&amp;rsquo;re not trying to find the exact query from the dashboard. The goal is to narrow the category before going back to the system tables: read-side vs write-side, query-level vs workload-level, lock contention vs buffer pressure vs CPU saturation. That tells you where to focus in the earlier steps, and if you have no SQL access, it&amp;rsquo;s enough to escalate with specifics. &amp;ldquo;40 sessions in Lock wait starting at 2:43, no deploy in the window, getting worse&amp;rdquo; is something whoever owns the database can act on. &amp;ldquo;It&amp;rsquo;s slow&amp;rdquo; is not.&lt;/p&gt;
&lt;h2 id="before-the-next-incident"&gt;Before the next incident
&lt;/h2&gt;&lt;p&gt;Everything above assumes you can run the queries when you need them, that you know what their output means, and that you&amp;rsquo;ve seen what &amp;ldquo;normal&amp;rdquo; looks like so the abnormal stands out. None of that is true in the middle of an incident unless you&amp;rsquo;ve done the work before then.&lt;/p&gt;
&lt;p&gt;Start with access. Can you connect to the primary, or only to a replica? Read-only, or with permission to call &lt;code&gt;pg_terminate_backend&lt;/code&gt;? Does your role have access to &lt;code&gt;pg_stat_statements&lt;/code&gt; (which requires &lt;code&gt;pg_read_all_stats&lt;/code&gt; on PostgreSQL 10 and later) and to the MySQL &lt;code&gt;performance_schema&lt;/code&gt; tables? On managed services, some catalog views are hidden behind parameter groups that take a restart to enable, and the moment you need &lt;code&gt;pg_stat_statements&lt;/code&gt; is a bad time to discover you don&amp;rsquo;t have it.&lt;/p&gt;
&lt;p&gt;Then find out what your team already has. Ops teams often wrap the queries from this article into named views: &lt;code&gt;active_queries&lt;/code&gt;, &lt;code&gt;blocking_chains&lt;/code&gt;, &lt;code&gt;long_transactions&lt;/code&gt;, top-N digest views. If yours exist, learn the column names and what they filter out (some hide replication workers, autovacuum, or your own session, which can mislead during an incident if you don&amp;rsquo;t know). If they don&amp;rsquo;t, ask your DBA whether they&amp;rsquo;d take a pull request, or write them yourself. A view you can &lt;code&gt;SELECT * FROM&lt;/code&gt; is faster to run and harder to typo under pressure than a 15-line join assembled from memory.&lt;/p&gt;
&lt;p&gt;Run the queries on a quiet system too. Pull &lt;code&gt;pg_stat_activity&lt;/code&gt; against your dev database while nothing stressful is happening, and note what idle connections look like (the pool&amp;rsquo;s keep-alives, your IDE&amp;rsquo;s introspection queries, your monitoring&amp;rsquo;s polling traffic). Pull a &lt;code&gt;pg_stat_statements&lt;/code&gt; snapshot and read through the top 20. The point is to know what your environment looks like at rest, so during an incident the abnormal jumps out instead of getting lost in baseline noise.&lt;/p&gt;
&lt;p&gt;The column documentation is worth reading once. Step 2 covered three wait events, and the PostgreSQL list linked there has dozens more. MySQL&amp;rsquo;s &lt;a class="link" href="https://dev.mysql.com/doc/refman/8.0/en/performance-schema-instrument-naming.html" target="_blank" rel="noopener"
 &gt;performance_schema instruments&lt;/a&gt; follow a naming convention you can learn in fifteen minutes.&lt;/p&gt;
&lt;p&gt;Last, practice reading EXPLAIN output. Pull a slow-ish query from your own codebase and run &lt;code&gt;EXPLAIN (ANALYZE, BUFFERS)&lt;/code&gt; on it in a quiet environment. The &amp;ldquo;what to look for&amp;rdquo; in Step 5 is more useful when you&amp;rsquo;ve spent thirty minutes staring at a plan in a low-stakes context first. Visualizers like &lt;a class="link" href="https://explain.depesz.com" target="_blank" rel="noopener"
 &gt;explain.depesz.com&lt;/a&gt; and &lt;a class="link" href="https://explain.dalibo.com" target="_blank" rel="noopener"
 &gt;explain.dalibo.com&lt;/a&gt; help with PostgreSQL output, and MySQL&amp;rsquo;s &lt;a class="link" href="https://dev.mysql.com/doc/refman/8.0/en/explain.html#explain-tree-output" target="_blank" rel="noopener"
 &gt;tree-format EXPLAIN&lt;/a&gt; is more readable than the default table format. None of those substitutes for knowing what each node type means, so read the docs for sequential scan, index scan, bitmap heap scan, nested loop, hash join, and merge join once.&lt;/p&gt;</description></item><item><title>It's Almost Always the Queries, Part I: Why Metal Doesn't Help</title><link>https://explainanalyze.com/p/its-almost-always-the-queries-part-i-why-metal-doesnt-help/</link><pubDate>Sat, 16 May 2026 00:00:00 +0000</pubDate><guid>https://explainanalyze.com/p/its-almost-always-the-queries-part-i-why-metal-doesnt-help/</guid><description>&lt;img src="https://explainanalyze.com/" alt="Featured image of post It's Almost Always the Queries, Part I: Why Metal Doesn't Help" /&gt;&lt;div class="tldr-box"&gt;
 &lt;strong&gt;TL;DR&lt;/strong&gt;
 &lt;div&gt;A capacity alert on a relational database usually comes from what the queries do, and a bigger server only moves the point where the same queries fail. In the test below, four times the CPUs served four times the queries and one index served 94 times. The exception is a workload whose queries already do the minimum work, and the last section says how to tell.&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;A database at 100% CPU gets a bigger instance. It is the fastest fix there is, it works within minutes, and on the day of the incident it is often the right call. But the alert tends to come back at the new size, and I wanted to put a number on what the resize buys compared with fixing the query that caused it.&lt;/p&gt;
&lt;p&gt;So I built the plainest case I could. An &lt;code&gt;orders&lt;/code&gt; table with 5 million rows, 2,000 accounts with 2,500 orders each, about 4% of them open, loaded in random order so one account&amp;rsquo;s rows are spread across the whole table. The table has a primary key and an index on &lt;code&gt;account_id&lt;/code&gt;, which is how most tables like it start out. The query is the one behind an &amp;ldquo;Open (98)&amp;rdquo; badge on an orders page:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-sql" data-lang="sql"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;&lt;span class="w"&gt; &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="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;WHERE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;account_id&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;AND&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;open&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;On PostgreSQL 18 with everything in memory, it runs in under 3 ms:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;span class="lnt"&gt;6
&lt;/span&gt;&lt;span class="lnt"&gt;7
&lt;/span&gt;&lt;span class="lnt"&gt;8
&lt;/span&gt;&lt;span class="lnt"&gt;9
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Aggregate (actual time=2.772..2.773 rows=1.00 loops=1)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Buffers: shared hit=2458
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; -&amp;gt; Bitmap Heap Scan on orders (actual time=0.345..2.766 rows=98.00 loops=1)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Recheck Cond: (account_id = 42)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Filter: (status = &amp;#39;open&amp;#39;::text)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Rows Removed by Filter: 2402
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Heap Blocks: exact=2453
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; -&amp;gt; Bitmap Index Scan on orders_account_idx (actual time=0.162..0.162 rows=2500.00 loops=1)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Execution Time: 2.802 ms
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Nobody would open a ticket for a 3 ms query. It reads 2,458 pages to count 98 rows, because the index finds all 2,500 of the account&amp;rsquo;s orders and the status of each one lives in the table.&lt;/p&gt;
&lt;h2 id="what-a-bigger-server-bought"&gt;What a bigger server bought
&lt;/h2&gt;&lt;p&gt;I gave the database 1 CPU, then 2, then 4, and pushed the count at it from 32 connections as fast as they would go. MySQL 8.4 got the same rows and the same test.&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Server&lt;/th&gt;
 &lt;th&gt;PostgreSQL 18&lt;/th&gt;
 &lt;th&gt;MySQL 8.4&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;1 CPU&lt;/td&gt;
 &lt;td&gt;497 queries/s&lt;/td&gt;
 &lt;td&gt;316 queries/s&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;2 CPUs&lt;/td&gt;
 &lt;td&gt;1,040&lt;/td&gt;
 &lt;td&gt;561&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;4 CPUs&lt;/td&gt;
 &lt;td&gt;1,996&lt;/td&gt;
 &lt;td&gt;932&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;1 CPU, plus an index on &lt;code&gt;(account_id, status)&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;46,618&lt;/td&gt;
 &lt;td&gt;26,525&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;em&gt;Queries finished per second at full load, 30 s per run, CPU at 99–100% in every row.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;The resize does what it says. Twice the CPUs served twice the queries on PostgreSQL, almost to the query, and MySQL came in a little under that at 4 CPUs (2.95 times the 1-CPU figure). Which means the price of this query is fixed, at about 2 ms of CPU per call on PostgreSQL, and a bigger server sells more milliseconds at the same price.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Bar chart of queries per second at full load. PostgreSQL: 497 on 1 CPU, 1,040 on 2, 1,996 on 4, and 46,618 on 1 CPU with an index on account_id and status. MySQL: 316, 561, 932 and 26,525." loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://explainanalyze.com/p/its-almost-always-the-queries-part-i-why-metal-doesnt-help/ceiling.svg"&gt;&lt;/p&gt;
&lt;p&gt;The second test sent requests at a fixed rate and measured how long each one took, counting the time a request spent waiting behind schedule. That is closer to what users feel.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Line chart of 95th-percentile response time against queries per second sent, log scale. On 1 CPU the time climbs from about 30 ms at 300 per second to over 500 ms at 400. On 2 CPUs it reaches seconds near 1,200 per second, on 4 CPUs near 2,400. With the index on 1 CPU it stays near 13 ms through 2,000 per second." loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://explainanalyze.com/p/its-almost-always-the-queries-part-i-why-metal-doesnt-help/load-curve.svg"&gt;&lt;/p&gt;
&lt;p&gt;On 1 CPU the 95th-percentile response time was 31 ms at 300 queries a second and 509 ms at 400. On 4 CPUs the same jump happened between 1,600 and 1,800. Each resize moved the cliff to the right by the amount of CPU purchased and left its shape alone. If traffic grows 40% a year, a doubling buys about two years, and the next doubling costs twice what this one did.&lt;/p&gt;
&lt;p&gt;More read replicas are the same purchase in a different form. The count does its 2 ms of work on whichever server runs it, so a replica adds another server&amp;rsquo;s worth of ceiling and the per-call price does not move.&lt;/p&gt;
&lt;h2 id="what-one-index-bought"&gt;What one index bought
&lt;/h2&gt;&lt;p&gt;The fix here is small. With an index on &lt;code&gt;(account_id, status)&lt;/code&gt; the count never touches the table:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;span class="lnt"&gt;6
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Aggregate (actual time=0.053..0.056 rows=1.00 loops=1)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Buffers: shared hit=6
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; -&amp;gt; Index Only Scan using orders_account_status_idx on orders (actual time=0.043..0.047 rows=98.00 loops=1)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Index Cond: ((account_id = 42) AND (status = &amp;#39;open&amp;#39;::text))
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Heap Fetches: 0
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Execution Time: 0.084 ms
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Six pages where there were 2,458. On the same single CPU the ceiling went from 497 queries a second to 46,618, which is 94 times, and at 2,000 queries a second (four times what that CPU could serve before) it was using about a third of the core. MySQL went from 316 to 26,525, or 84 times.&lt;/p&gt;
&lt;div class="note-box"&gt;
 &lt;strong&gt;Note&lt;/strong&gt;
 &lt;div&gt;The index has a price, and I did not measure all of it. On MySQL it added 141 MB next to a 462 MB table. Every insert and every status change now maintains one more index, and I ran no write load in this test, so the numbers above are the read side only. The 94 times also belongs to this query on this table. A query that is already reading the minimum will get nothing like it, and the 4-CPU run with the index is missing from the table because my load generator ran out of CPU before the database did.&lt;/div&gt;
&lt;/div&gt;

&lt;h2 id="what-the-slow-query-log-saw"&gt;What the slow-query log saw
&lt;/h2&gt;&lt;p&gt;Part of why the resize wins by default is that the cheap query is hard to see. The slow-query log was on for the whole test with a 100 ms threshold, and I counted its entries at each load level on the 1-CPU server before the index existed.&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Queries sent per second&lt;/th&gt;
 &lt;th&gt;CPU used&lt;/th&gt;
 &lt;th&gt;Queries run in 30 s&lt;/th&gt;
 &lt;th&gt;Logged as slow (over 100 ms)&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;100&lt;/td&gt;
 &lt;td&gt;42%&lt;/td&gt;
 &lt;td&gt;2,996&lt;/td&gt;
 &lt;td&gt;1&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;200&lt;/td&gt;
 &lt;td&gt;57%&lt;/td&gt;
 &lt;td&gt;5,932&lt;/td&gt;
 &lt;td&gt;20&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;300&lt;/td&gt;
 &lt;td&gt;67%&lt;/td&gt;
 &lt;td&gt;9,052&lt;/td&gt;
 &lt;td&gt;35&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;400&lt;/td&gt;
 &lt;td&gt;82%&lt;/td&gt;
 &lt;td&gt;11,916&lt;/td&gt;
 &lt;td&gt;691&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;450&lt;/td&gt;
 &lt;td&gt;90%&lt;/td&gt;
 &lt;td&gt;13,473&lt;/td&gt;
 &lt;td&gt;1,293&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;500&lt;/td&gt;
 &lt;td&gt;100%&lt;/td&gt;
 &lt;td&gt;14,713&lt;/td&gt;
 &lt;td&gt;6,847&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;em&gt;PostgreSQL 18, 1 CPU, &lt;code&gt;log_min_duration_statement = 100&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;I expected the log to stay empty, since the query takes 3 ms. It did, nearly, while the server was healthy: at two-thirds CPU, 35 of 9,052 executions crossed the threshold. Then at the ceiling it logged 6,847 in thirty seconds, because a 3 ms query that waits its turn behind dozens of others takes longer than 100 ms. So the log says nothing during the months when the query is quietly using most of a CPU, and on the day of the incident it fills with whatever was unlucky enough to be queued. In this test that was one statement. On a real server it is every statement, the cause and its victims together.&lt;/p&gt;
&lt;p&gt;What does see the query early is the per-statement totals: &lt;code&gt;pg_stat_statements&lt;/code&gt; sorted by &lt;code&gt;total_exec_time&lt;/code&gt;, or &lt;code&gt;performance_schema.events_statements_summary_by_digest&lt;/code&gt; sorted by &lt;code&gt;SUM_TIMER_WAIT&lt;/code&gt; on MySQL. Part III runs that comparison on a mixed workload.&lt;/p&gt;
&lt;h2 id="the-same-reading-for-the-other-alerts"&gt;The same reading for the other alerts
&lt;/h2&gt;&lt;p&gt;CPU is the easy case to measure. The same two readings exist for the other alerts, and the rest of the series takes them one at a time.&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Alert&lt;/th&gt;
 &lt;th&gt;Hardware reading&lt;/th&gt;
 &lt;th&gt;Query reading, usually the right one&lt;/th&gt;
 &lt;th&gt;Where&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;CPU at 100%&lt;/td&gt;
 &lt;td&gt;Too few cores&lt;/td&gt;
 &lt;td&gt;A cheap query run very often, or one reading far more rows than it returns&lt;/td&gt;
 &lt;td&gt;&lt;a class="link" href="https://explainanalyze.com/p/its-almost-always-the-queries-part-iii-when-the-cpu-is-pegged/" &gt;Part III&lt;/a&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Out of memory&lt;/td&gt;
 &lt;td&gt;Too little RAM&lt;/td&gt;
 &lt;td&gt;Sort and hash memory multiplied by concurrent connections&lt;/td&gt;
 &lt;td&gt;&lt;a class="link" href="https://explainanalyze.com/p/its-almost-always-the-queries-part-iv-when-the-sort-spills/" &gt;Part IV&lt;/a&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Disk full or slow&lt;/td&gt;
 &lt;td&gt;Too little storage or IOPS&lt;/td&gt;
 &lt;td&gt;Dead rows, no retention, queries that fetch from the table row by row&lt;/td&gt;
 &lt;td&gt;&lt;a class="link" href="https://explainanalyze.com/p/its-almost-always-the-queries-part-v-disk-has-two-alarms-not-one/" &gt;Part V&lt;/a&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Replica lag&lt;/td&gt;
 &lt;td&gt;Too few replicas&lt;/td&gt;
 &lt;td&gt;The shape of the writes on the primary&lt;/td&gt;
 &lt;td&gt;Its own series&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;em&gt;Only the CPU row is measured in this post. The others are argued in their own parts.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;a class="link" href="https://explainanalyze.com/p/its-almost-always-the-queries-part-ii-troubleshooting-steps/" &gt;Part II&lt;/a&gt; is the procedure for an incident in progress.&lt;/p&gt;
&lt;div class="warning-box"&gt;
 &lt;strong&gt;When renting the bigger server is the right call&lt;/strong&gt;
 &lt;div&gt;A known, bounded surge (a launch, a sale weekend) can cost less to ride out on a larger instance than to engineer around before the date. So can a team with no one free to read a query plan this month. Both are fine as long as the resize comes with a ticket for the query and a date to size back down. The expensive version is the resize with neither, where the larger instance becomes the baseline.&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;There is also a workload that really does need the cores, and the per-statement totals show the difference. If the top of the list is one statement with a call count far above everything else, or one statement reading thousands of pages to return a handful of rows, the server is paying for that query. If the top is a spread of statements that each read about what they return, the work is real and a bigger server is the honest answer. The number I would look at before changing the instance type is the one from the first plan above: 2,458 pages read for 98 rows counted.&lt;/p&gt;</description></item><item><title>Hidden Database Costs of an AI Rollout: Storage, CPU, Memory, and Cache</title><link>https://explainanalyze.com/p/hidden-database-costs-of-an-ai-rollout-storage-cpu-memory-and-cache/</link><pubDate>Sun, 10 May 2026 00:00:00 +0000</pubDate><guid>https://explainanalyze.com/p/hidden-database-costs-of-an-ai-rollout-storage-cpu-memory-and-cache/</guid><description>&lt;img src="https://explainanalyze.com/" alt="Featured image of post Hidden Database Costs of an AI Rollout: Storage, CPU, Memory, and Cache" /&gt;&lt;div class="tldr-box"&gt;
 &lt;strong&gt;TL;DR&lt;/strong&gt;
 &lt;div&gt;Adding RAG to your existing Postgres multiplies the storage on the affected tables (about 6 KB per 1536-dimension row before any index), leaves the HNSW index one memory shortfall away from a cliff that doesn&amp;rsquo;t degrade gracefully, and pollutes the buffer cache hard enough that unrelated OLTP queries can regress at p95. Half-precision vectors, binary quantization with a rerank step, and a separate replica recover most of it, after the bill has already arrived.&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;A pgvector user opened &lt;a class="link" href="https://github.com/pgvector/pgvector/issues/666" target="_blank" rel="noopener"
 &gt;issue #666&lt;/a&gt; in September 2024. They had one million records, 512-dimensional vectors, and an HNSW index, the graph index pgvector uses for approximate nearest-neighbor search. Cold-cache search took 83 seconds. Warm cache, the same query returned in roughly 100 milliseconds. Three orders of magnitude. The index had not grown and the query had not changed. The reporter&amp;rsquo;s reading, which a maintainer agreed with, was that other applications on the same machine had pushed the index pages out of memory, and the next search read the HNSW graph back from disk one block at a time. A capacity plan for an AI feature usually has a line for storage and a line for CPU. It rarely has one for the vector index competing for memory with everything else on the box, and losing.&lt;/p&gt;
&lt;p&gt;The two usual answers are &amp;ldquo;throw a bigger instance at it&amp;rdquo; and &amp;ldquo;stop running this on the OLTP Postgres and use a dedicated vector DB&amp;rdquo;. Both are right in some configurations. A bigger instance buys headroom for the working set without changing the slope of the cost curve as the corpus grows. A dedicated vector store removes the cache-pollution problem and adds a network hop, a second consistency model, and another piece of infrastructure to back up. The choice being made is between paying the cost on the OLTP Postgres in the form of a bigger instance and worse p95s, or paying it on a second system in the form of more operational surface area. The conversation that didn&amp;rsquo;t happen is the one about what the cost actually consists of and where it accrues. That is what the rest of this article is.&lt;/p&gt;
&lt;h2 id="the-four-cost-categories"&gt;The four cost categories
&lt;/h2&gt;&lt;p&gt;Every pgvector &lt;code&gt;vector&lt;/code&gt; row takes &lt;code&gt;4 * dimensions + 8&lt;/code&gt; bytes. A 1536-dimensional embedding from text-embedding-3-small lands at 6,152 bytes. A 50-million-row table that occupied 40 GB on its existing columns becomes 350 GB after the embedding column is added, before any index. Supabase published a &lt;a class="link" href="https://supabase.com/blog/fewer-dimensions-are-better-pgvector" target="_blank" rel="noopener"
 &gt;case study in August 2023&lt;/a&gt; on 224,482 embeddings where Postgres RAM consumption went from 4 GB on 384-dimensional vectors to 7.5 GB on 1536-dimensional vectors, on the same hardware and the same row count. The dimensions alone were the difference.&lt;/p&gt;
&lt;p&gt;Vector index builds cost CPU at a scale that breaks the assumptions of any normal migration window. Jonathan Katz &lt;a class="link" href="https://jkatz05.com/post/postgres/pgvector-overview-2024/" target="_blank" rel="noopener"
 &gt;benchmarked HNSW build&lt;/a&gt; on the dbpedia 1M corpus across pgvector versions: 7,479 seconds on 0.5.0, 250 seconds on 0.7.0 with the parallel-build improvements, 49 seconds with binary quantization (one bit per dimension, covered below). AWS published &lt;a class="link" href="https://aws.amazon.com/blogs/database/load-vector-embeddings-up-to-67x-faster-with-pgvector-and-amazon-aurora/" target="_blank" rel="noopener"
 &gt;Aurora numbers&lt;/a&gt; on a 5M OpenAI dataset showing 29,752 seconds on 0.5.1 versus 445 seconds on 0.7.0 with binary quantization. Versions matter. They also bound how bad it can get on older versions. pgvector &lt;a class="link" href="https://github.com/pgvector/pgvector/issues/300" target="_blank" rel="noopener"
 &gt;issue #300&lt;/a&gt; reports a 24-plus-hour HNSW build on 10M rows of 768-dimensional vectors with 4 CPU and 16 GB RAM. &lt;a class="link" href="https://github.com/pgvector/pgvector/issues/807" target="_blank" rel="noopener"
 &gt;Issue #807&lt;/a&gt; reports the connection dropping after roughly two hours of an HNSW build on 17M rows of 1536-dimensional vectors with 48 CPU and 192 GB RAM. The build is the part of the workload least visible to the production dashboard and most painful to retry.&lt;/p&gt;
&lt;p&gt;HNSW does not degrade gracefully when its working set exceeds RAM. The graph is designed to be traversed in random order, which is fast when every page is in &lt;code&gt;shared_buffers&lt;/code&gt; and falls off a cliff when it isn&amp;rsquo;t. pgvector &lt;a class="link" href="https://github.com/pgvector/pgvector/issues/844" target="_blank" rel="noopener"
 &gt;issue #844&lt;/a&gt; caught the in-build version of the same problem: the user got the message &lt;code&gt;hnsw graph no longer fits into maintenance_work_mem after 5908085 tuples&lt;/code&gt;, and the build slowed dramatically from that tuple onward. The query-side equivalent has the same shape. Crunchy Data&amp;rsquo;s &lt;a class="link" href="https://www.crunchydata.com/blog/hnsw-indexes-with-postgres-and-pgvector" target="_blank" rel="noopener"
 &gt;HNSW write-up&lt;/a&gt; reports index sizes of roughly 8 GB per million rows on typical AI embeddings. The exact latency-vs-RAM curve past the cliff is not published in any vendor source I can find. The shape is well-known to anyone who has watched it happen, and the absence of a published curve is itself a sign of how much of this knowledge lives in incident channels rather than docs.&lt;/p&gt;
&lt;p&gt;The cache problem is the inverse of the build problem. Once the index exists, every approximate nearest-neighbor query touches thousands of pages chased through a graph traversal. A handful of vector queries running concurrently with OLTP traffic can be enough to evict the heap pages the OLTP queries depend on, and the OLTP p95 regresses without any change to OLTP code. The pgvector #666 numbers from the opening are the cleanest single data point on this. The instructive piece is the magnitude. Not 2x worse, not 5x worse. Three orders of magnitude depending on cache state. There is no other workload class on a typical OLTP Postgres that produces that swing.&lt;/p&gt;
&lt;p&gt;All four cost categories converge on the managed-service bill. Aurora storage runs $0.10 per GB-month at the base tier. 100 million 1536-dimensional full-precision vectors require roughly 6.15 GB raw per million rows, plus an HNSW index closer to 8 GB per million on typical configurations. That is about 1.4 TB before backups, replicas, or growth, around $140/month in raw storage at the base rate. Storage is the smaller part. The larger part is the instance class needed to keep the active portion of that index in &lt;code&gt;shared_buffers&lt;/code&gt;, which on the memory-cliff curve above means an instance one or two tiers above what the rest of the workload required.&lt;/p&gt;
&lt;div class="note-box"&gt;
 &lt;strong&gt;Note&lt;/strong&gt;
 &lt;div&gt;All four costs assume a vector workload colocated with an OLTP Postgres at production scale, which is the configuration most teams ship first because it requires the fewest decisions. Under roughly 100,000 vectors, or on a low-QPS internal tool that searches a few times a minute, none of this is large enough to matter. Teams on a dedicated vector store from day one (Pinecone, Weaviate, Qdrant Cloud, pgvectorscale on a separate instance) paid the operational price up front and have a different cost structure.&lt;/div&gt;
&lt;/div&gt;

&lt;h2 id="the-two-events-the-capacity-plan-didnt-budget-for"&gt;The two events the capacity plan didn&amp;rsquo;t budget for
&lt;/h2&gt;&lt;p&gt;Embedding model upgrades rewrite the embedding column. text-embedding-3-small was &lt;a class="link" href="https://openai.com/index/new-embedding-models-and-api-updates/" target="_blank" rel="noopener"
 &gt;released January 2024&lt;/a&gt; alongside text-embedding-3-large, and any team that wanted the better recall on the larger model also wanted to re-embed the existing corpus. The migration is a full rewrite of the largest column on the largest table, plus an HNSW index rebuild on the new vectors, plus the API call cost of generating the new embeddings, plus double storage during cutover unless the team is willing to take a recall regression by deleting the old embeddings before the new ones are validated. There is no published postmortem from a named company giving real numbers on this event. The closest public signal is pgvector &lt;a class="link" href="https://github.com/pgvector/pgvector/issues/559" target="_blank" rel="noopener"
 &gt;issue #559&lt;/a&gt; from May 2024, where a user reports that individual inserts on a 1M-row HNSW table went from millisecond-scale before the index existed to &amp;ldquo;5–8s&amp;rdquo; afterward. The same write amplification applies to the migration, except now it applies to every row at once. The absence of postmortems is itself worth noticing. The event is recent enough that most teams haven&amp;rsquo;t lived through their second model upgrade yet.&lt;/p&gt;
&lt;p&gt;The other unpriced event is connection pool starvation when the application holds a Postgres connection while waiting on an LLM call. The pattern is straightforward. A request needs context from the database, the application opens a transaction, fetches the rows, builds a prompt, calls the model, gets a 4-second response, writes the result back, commits. The connection is held for the entire round trip. A pool sized for 200ms transactions exhausts at one-twentieth the request rate it was sized for, and the failure surfaces as &lt;code&gt;too many connections&lt;/code&gt; errors on requests that have nothing to do with the AI feature. There is no named-company postmortem in the public record for this one either. The pattern is recognizable to anyone who has run a database behind a synchronous LLM call. The fix is structural. Do the LLM call outside the transaction. Release the connection before the model call begins and acquire a new one after. Or move to a connection pooler that explicitly supports this pattern. None of those is free, and none of them is what the first version of the feature ships with.&lt;/p&gt;
&lt;h2 id="what-actually-moves-the-bill"&gt;What actually moves the bill
&lt;/h2&gt;&lt;p&gt;Three levers do most of the work, and each one carries a trade-off the AI-feature team would rather not own.&lt;/p&gt;
&lt;p&gt;Halfvec is the cheapest move. The pgvector &lt;code&gt;halfvec&lt;/code&gt; type stores each component as a 16-bit float instead of 32-bit, which halves storage. Neon&amp;rsquo;s &lt;a class="link" href="https://neon.com/blog/dont-use-vector-use-halvec-instead-and-save-50-of-your-storage-cost" target="_blank" rel="noopener"
 &gt;July 2024 post on halfvec&lt;/a&gt; reports nearly 50% lower storage, 23% faster index build, 50% faster prewarming, and equivalent recall on a 1M DBpedia 1536-dimensional corpus. The trade-off is that halfvec only buys 2x. The corpus growth that earned the bill in the first place still applies, so halfvec lowers the bill without changing its slope.&lt;/p&gt;
&lt;p&gt;Binary quantization with reranking is the next lever, and it is the one with a published tension worth understanding. Qdrant&amp;rsquo;s &lt;a class="link" href="https://qdrant.tech/articles/binary-quantization/" target="_blank" rel="noopener"
 &gt;binary quantization article&lt;/a&gt; from September 2023 reports recall of 0.985 on text-embedding-3-small at 3x oversampling with reranking, and 0.997 on text-embedding-3-large at the same setting. Storage drops by roughly 32x relative to full precision. The same Neon post tried binary quantization on 1536-dimensional vectors and concluded that recall &amp;ldquo;is not significant enough to use this in production&amp;rdquo;. The two results are compatible. Qdrant&amp;rsquo;s figures include a rerank stage that re-evaluates the top-k binary candidates against full-precision vectors held elsewhere, and binary quantization without that stage gives up too much recall. With rerank, it requires keeping a full-precision copy of the vectors somewhere accessible, which is a storage problem again in a different shape.&lt;/p&gt;
&lt;p&gt;Separating the vector workload onto its own physical Postgres replica or onto a dedicated vector store addresses cache pollution directly. The trade-off is operational. A second system to back up, a second consistency model to reason about, a second incident-response runbook. On a small team this can dominate the cost it was meant to save. On a larger team where the AI feature has its own owners and the OLTP database has its own owners, the separation aligns infrastructure boundaries with team boundaries and is usually worth it on those grounds alone, before any cache argument is made.&lt;/p&gt;
&lt;p&gt;A scalar quantization note worth keeping in view: Jonathan Katz&amp;rsquo;s &lt;a class="link" href="https://jkatz05.com/post/postgres/pgvector-scalar-binary-quantization/" target="_blank" rel="noopener"
 &gt;scalar and binary quantization benchmark&lt;/a&gt; from April 2024 measured halfvec recall at 0.968 versus full-precision 0.968 on dbpedia-openai-1000k-angular at the same &lt;code&gt;ef_search&lt;/code&gt; (the size of the candidate list a query keeps). The author&amp;rsquo;s verdict was direct: &amp;ldquo;Scalar quantization from 4-byte to 2-byte floats looks like a clear winner.&amp;rdquo; On most embedding models, halfvec is the move you can make today without touching application code or rerank pipelines, and the bill drops by half.&lt;/p&gt;</description></item></channel></rss>