<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://bridgetownrb.com/" version="2.2.2">Bridgetown</generator><link href="https://filipsucic.com/feed.xml" rel="self" type="application/atom+xml" /><link href="https://filipsucic.com/" rel="alternate" type="text/html" /><updated>2026-11-29T18:21:00+00:00</updated><id>https://filipsucic.com/feed.xml</id><title type="html">Filip Sučić</title><subtitle>Backend engineer at Productive. Ruby on Rails &amp; MySQL. Notes on databases, performance, and tooling.</subtitle><entry><title type="html">MySQL index_merge, or why two indexes lose to one</title><link href="https://filipsucic.com/posts/mysql-index-merge/" rel="alternate" type="text/html" title="MySQL index_merge, or why two indexes lose to one" /><published>2026-11-28T00:00:00+00:00</published><updated>2026-11-28T00:00:00+00:00</updated><id>repo://posts.collection/_posts/2026-11-04-mysql-index-merge-composite-index.md</id><content type="html" xml:base="https://filipsucic.com/posts/mysql-index-merge/">&lt;p&gt;In &lt;em&gt;Efficient MySQL Performance&lt;/em&gt; (which I highly recommend), Daniel Nichter writes that
“indexes provide the most and the best leverage for MySQL performance.” So when an
endpoint that lists a person’s assignments got slow, an index was the first thing I
reached for. Every assignment belongs to a tenant and to a person:&lt;/p&gt;

&lt;div class=&quot;language-sql highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;CREATE&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;TABLE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;assignments&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;         &lt;span class=&quot;nb&quot;&gt;BIGINT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;PRIMARY&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;KEY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;account_id&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;BIGINT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NOT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;-- tenant&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;person_id&lt;/span&gt;  &lt;span class=&quot;nb&quot;&gt;BIGINT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NOT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;-- belongs to the *same* tenant&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;KEY&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index_assignments_on_account_id&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;account_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Notice that only the &lt;code class=&quot;highlighter-rouge&quot;&gt;account_id&lt;/code&gt; column is indexed. The endpoint runs this query:&lt;/p&gt;

&lt;div class=&quot;language-sql highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;assignments&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;WHERE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;account_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;AND&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;person_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;101&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;With the only existing index, MySQL reads every row in the tenant and checks &lt;code class=&quot;highlighter-rouge&quot;&gt;person_id&lt;/code&gt;
on each one. That gets slow in big tenants, so I added an index on &lt;code class=&quot;highlighter-rouge&quot;&gt;person_id&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-sql highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;CREATE&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;INDEX&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index_assignments_on_person_id&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;ON&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;assignments&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;person_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I left &lt;code class=&quot;highlighter-rouge&quot;&gt;account_id&lt;/code&gt; out of it on purpose. A person lives in a single tenant, which is the
same tenant the assignment belongs to. The &lt;code class=&quot;highlighter-rouge&quot;&gt;account_id&lt;/code&gt; prefix would only make the index wider.
Also, the query isn’t covered either way.&lt;/p&gt;

&lt;p&gt;With the new index, MySQL can find the person’s rows directly and check the tenant on
each one. We know that even the tenant check is redundant, but MySQL doesn’t, because it
knows nothing about how our columns relate, about the nature of our data.
That gap is the point of this post. In production the &lt;code class=&quot;highlighter-rouge&quot;&gt;person_id&lt;/code&gt; index was highly selective,
yet the query wasn’t equally fast for every tenant, and the slow ones weren’t the biggest.&lt;/p&gt;

&lt;p&gt;Leaving the prefix out is fine as long as MySQL sticks to one index. In this post I’ll show you
the optimization MySQL uses to combine two indexes, and why it can end up slower than
using just one.&lt;/p&gt;

&lt;h2 id=&quot;the-data&quot;&gt;The data&lt;/h2&gt;

&lt;p&gt;I will run the queries on MySQL 8.4. There are eight tenants in the &lt;code class=&quot;highlighter-rouge&quot;&gt;assignments&lt;/code&gt; table.
The smallest has 50,000 rows and the biggest 400,000 rows. For every query, the person has
exactly 2,500 rows in their tenant, spread across the whole tenant, and two other people
share the rest. Altogether, the table has 1.8 million rows.&lt;/p&gt;

&lt;p&gt;The plan kept changing between runs of the same query, depending on what was already
in the buffer pool. I wanted the script to give me the same plans every time, so every
query below starts with a cold buffer pool.&lt;/p&gt;

&lt;details&gt;
  &lt;summary&gt;Full schema and seed&lt;/summary&gt;

  &lt;div class=&quot;language-sql highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;CREATE&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;TABLE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;assignments&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;         &lt;span class=&quot;nb&quot;&gt;BIGINT&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AUTO_INCREMENT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;PRIMARY&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;KEY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;account_id&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;BIGINT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NOT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;-- tenant&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;person_id&lt;/span&gt;  &lt;span class=&quot;nb&quot;&gt;BIGINT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NOT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;-- belongs to the *same* tenant&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;role&lt;/span&gt;       &lt;span class=&quot;nb&quot;&gt;VARCHAR&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NOT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NULL&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;DEFAULT&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;&apos;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ENGINE&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;InnoDB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;CREATE&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;INDEX&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index_assignments_on_account_id&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;ON&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;assignments&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;account_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;CREATE&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;INDEX&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index_assignments_on_person_id&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;ON&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;assignments&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;person_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;DELIMITER&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;$$&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;CREATE&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;PROCEDURE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seed_assignments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;BEGIN&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;DECLARE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;INT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;DEFAULT&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;DECLARE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;INT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;WHILE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;DO&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;SET&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;START&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TRANSACTION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;WHILE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;50000&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;DO&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;INSERT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;INTO&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;assignments&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;account_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;person_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;VALUES&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;CASE&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;WHEN&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;THEN&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
                                &lt;span class=&quot;k&quot;&gt;WHEN&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2500&lt;/span&gt;       &lt;span class=&quot;k&quot;&gt;THEN&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;
                                &lt;span class=&quot;k&quot;&gt;ELSE&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;END&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;SET&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;END&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WHILE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;COMMIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;SET&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;END&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WHILE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;END&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;$$&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;DELIMITER&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;CALL&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seed_assignments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

&lt;/details&gt;

&lt;h2 id=&quot;what-it-does-as-the-tenant-grows&quot;&gt;What it does as the tenant grows&lt;/h2&gt;

&lt;p&gt;Let’s run the same query against each tenant and see what happens. Each time, we get
2,500 rows back. Times are medians over 25 runs:&lt;/p&gt;

&lt;figure class=&quot;chart&quot;&gt;
&lt;svg viewBox=&quot;0 0 640 280&quot; role=&quot;img&quot; aria-label=&quot;Query time by tenant size: 9.2, 16.4, 23.1, 30.3, 36.9 and 43.9 ms from 50,000 to 300,000 rows, then 2.4 ms at 350,000 and 2.9 ms at 400,000&quot; style=&quot;width:100%;height:auto;font:12px system-ui,sans-serif&quot;&gt;
&lt;line x1=&quot;48&quot; x2=&quot;632&quot; y1=&quot;236.0&quot; y2=&quot;236.0&quot; style=&quot;stroke:var(--rule);stroke-width:1&quot; /&gt;
&lt;text x=&quot;40&quot; y=&quot;240.0&quot; text-anchor=&quot;end&quot; style=&quot;fill:var(--muted)&quot;&gt;0&lt;/text&gt;
&lt;line x1=&quot;48&quot; x2=&quot;632&quot; y1=&quot;193.6&quot; y2=&quot;193.6&quot; style=&quot;stroke:var(--rule);stroke-width:1&quot; /&gt;
&lt;text x=&quot;40&quot; y=&quot;197.6&quot; text-anchor=&quot;end&quot; style=&quot;fill:var(--muted)&quot;&gt;10&lt;/text&gt;
&lt;line x1=&quot;48&quot; x2=&quot;632&quot; y1=&quot;151.2&quot; y2=&quot;151.2&quot; style=&quot;stroke:var(--rule);stroke-width:1&quot; /&gt;
&lt;text x=&quot;40&quot; y=&quot;155.2&quot; text-anchor=&quot;end&quot; style=&quot;fill:var(--muted)&quot;&gt;20&lt;/text&gt;
&lt;line x1=&quot;48&quot; x2=&quot;632&quot; y1=&quot;108.8&quot; y2=&quot;108.8&quot; style=&quot;stroke:var(--rule);stroke-width:1&quot; /&gt;
&lt;text x=&quot;40&quot; y=&quot;112.8&quot; text-anchor=&quot;end&quot; style=&quot;fill:var(--muted)&quot;&gt;30&lt;/text&gt;
&lt;line x1=&quot;48&quot; x2=&quot;632&quot; y1=&quot;66.4&quot; y2=&quot;66.4&quot; style=&quot;stroke:var(--rule);stroke-width:1&quot; /&gt;
&lt;text x=&quot;40&quot; y=&quot;70.4&quot; text-anchor=&quot;end&quot; style=&quot;fill:var(--muted)&quot;&gt;40&lt;/text&gt;
&lt;line x1=&quot;48&quot; x2=&quot;632&quot; y1=&quot;24.0&quot; y2=&quot;24.0&quot; style=&quot;stroke:var(--rule);stroke-width:1&quot; /&gt;
&lt;text x=&quot;40&quot; y=&quot;28.0&quot; text-anchor=&quot;end&quot; style=&quot;fill:var(--muted)&quot;&gt;50&lt;/text&gt;
&lt;text x=&quot;40&quot; y=&quot;14&quot; text-anchor=&quot;end&quot; style=&quot;fill:var(--muted)&quot;&gt;ms&lt;/text&gt;
&lt;path d=&quot;M62.5 236 V201.0 Q62.5 197.0 66.5 197.0 H102.5 Q106.5 197.0 106.5 201.0 V236 Z&quot; style=&quot;fill:var(--link)&quot;&gt;&lt;title&gt;50k rows: 9.2 ms&lt;/title&gt;&lt;/path&gt;
&lt;text x=&quot;84.5&quot; y=&quot;191.0&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--text)&quot;&gt;9.2&lt;/text&gt;
&lt;text x=&quot;84.5&quot; y=&quot;254&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;50k&lt;/text&gt;
&lt;path d=&quot;M135.5 236 V170.5 Q135.5 166.5 139.5 166.5 H175.5 Q179.5 166.5 179.5 170.5 V236 Z&quot; style=&quot;fill:var(--link)&quot;&gt;&lt;title&gt;100k rows: 16.4 ms&lt;/title&gt;&lt;/path&gt;
&lt;text x=&quot;157.5&quot; y=&quot;160.5&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--text)&quot;&gt;16.4&lt;/text&gt;
&lt;text x=&quot;157.5&quot; y=&quot;254&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;100k&lt;/text&gt;
&lt;path d=&quot;M208.5 236 V142.1 Q208.5 138.1 212.5 138.1 H248.5 Q252.5 138.1 252.5 142.1 V236 Z&quot; style=&quot;fill:var(--link)&quot;&gt;&lt;title&gt;150k rows: 23.1 ms&lt;/title&gt;&lt;/path&gt;
&lt;text x=&quot;230.5&quot; y=&quot;132.1&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--text)&quot;&gt;23.1&lt;/text&gt;
&lt;text x=&quot;230.5&quot; y=&quot;254&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;150k&lt;/text&gt;
&lt;path d=&quot;M281.5 236 V111.5 Q281.5 107.5 285.5 107.5 H321.5 Q325.5 107.5 325.5 111.5 V236 Z&quot; style=&quot;fill:var(--link)&quot;&gt;&lt;title&gt;200k rows: 30.3 ms&lt;/title&gt;&lt;/path&gt;
&lt;text x=&quot;303.5&quot; y=&quot;101.5&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--text)&quot;&gt;30.3&lt;/text&gt;
&lt;text x=&quot;303.5&quot; y=&quot;254&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;200k&lt;/text&gt;
&lt;path d=&quot;M354.5 236 V83.5 Q354.5 79.5 358.5 79.5 H394.5 Q398.5 79.5 398.5 83.5 V236 Z&quot; style=&quot;fill:var(--link)&quot;&gt;&lt;title&gt;250k rows: 36.9 ms&lt;/title&gt;&lt;/path&gt;
&lt;text x=&quot;376.5&quot; y=&quot;73.5&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--text)&quot;&gt;36.9&lt;/text&gt;
&lt;text x=&quot;376.5&quot; y=&quot;254&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;250k&lt;/text&gt;
&lt;path d=&quot;M427.5 236 V53.9 Q427.5 49.9 431.5 49.9 H467.5 Q471.5 49.9 471.5 53.9 V236 Z&quot; style=&quot;fill:var(--link)&quot;&gt;&lt;title&gt;300k rows: 43.9 ms&lt;/title&gt;&lt;/path&gt;
&lt;text x=&quot;449.5&quot; y=&quot;43.9&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--text)&quot;&gt;43.9&lt;/text&gt;
&lt;text x=&quot;449.5&quot; y=&quot;254&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;300k&lt;/text&gt;
&lt;path d=&quot;M500.5 236 V229.8 Q500.5 225.8 504.5 225.8 H540.5 Q544.5 225.8 544.5 229.8 V236 Z&quot; style=&quot;fill:var(--link)&quot;&gt;&lt;title&gt;350k rows: 2.4 ms&lt;/title&gt;&lt;/path&gt;
&lt;text x=&quot;522.5&quot; y=&quot;219.8&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--text)&quot;&gt;2.4&lt;/text&gt;
&lt;text x=&quot;522.5&quot; y=&quot;254&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;350k&lt;/text&gt;
&lt;path d=&quot;M573.5 236 V227.7 Q573.5 223.7 577.5 223.7 H613.5 Q617.5 223.7 617.5 227.7 V236 Z&quot; style=&quot;fill:var(--link)&quot;&gt;&lt;title&gt;400k rows: 2.9 ms&lt;/title&gt;&lt;/path&gt;
&lt;text x=&quot;595.5&quot; y=&quot;217.7&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--text)&quot;&gt;2.9&lt;/text&gt;
&lt;text x=&quot;595.5&quot; y=&quot;254&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;400k&lt;/text&gt;
&lt;text x=&quot;340.0&quot; y=&quot;274&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;tenant rows&lt;/text&gt;
&lt;/svg&gt;
&lt;/figure&gt;

&lt;p&gt;The first six bars grow in a straight line, about 7 ms per 50,000 rows in the tenant.
It seems like MySQL is reading every row in the tenant instead of using only the
super selective &lt;code class=&quot;highlighter-rouge&quot;&gt;person_id&lt;/code&gt; index.&lt;/p&gt;

&lt;p&gt;Then, at 350,000 rows, the query suddenly gets eighteen times faster than at 300,000.
It stays fast at 400,000 too. Now, MySQL surely uses the index we were hoping it would,
the &lt;code class=&quot;highlighter-rouge&quot;&gt;person_id&lt;/code&gt; one.&lt;/p&gt;

&lt;h2 id=&quot;the-plans&quot;&gt;The plans&lt;/h2&gt;

&lt;p&gt;Here are the first six. It’s the same plan on every slide, so as you click through,
watch the &lt;code class=&quot;highlighter-rouge&quot;&gt;rows=&lt;/code&gt; count on each of the two branches:&lt;/p&gt;

&lt;div class=&quot;plan-carousel&quot; data-legend=&quot;tenant rows&quot; data-labels=&quot;50,000|100,000|150,000|200,000|250,000|300,000&quot;&gt;

  &lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;account_id = 1

-&amp;gt; Filter: ((person_id = 101) and (account_id = 1))
     (rows=139) (actual time=0.0379..9.27 rows=2500)
  -&amp;gt; Intersect rows sorted by row ID
       (rows=140) (actual time=0.0374..9.14 rows=2500)
    -&amp;gt; Index range scan using index_assignments_on_person_id
       over (person_id = 101)
         (rows=2500) (actual time=0.0215..0.346 rows=2500)
    -&amp;gt; Index range scan using index_assignments_on_account_id
       over (account_id = 1)
         (rows=100346) (actual time=0.00767..6.16 rows=50000)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;account_id = 2

-&amp;gt; Filter: ((person_id = 201) and (account_id = 2))
     (rows=262) (actual time=0.0376..16 rows=2500)
  -&amp;gt; Intersect rows sorted by row ID
       (rows=262) (actual time=0.0372..15.9 rows=2500)
    -&amp;gt; Index range scan using index_assignments_on_person_id
       over (person_id = 201)
         (rows=2500) (actual time=0.0152..0.339 rows=2500)
    -&amp;gt; Index range scan using index_assignments_on_account_id
       over (account_id = 2)
         (rows=188336) (actual time=0.00846..11.9 rows=100000)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;account_id = 3

-&amp;gt; Filter: ((person_id = 301) and (account_id = 3))
     (rows=442) (actual time=0.0367..23.1 rows=2500)
  -&amp;gt; Intersect rows sorted by row ID
       (rows=442) (actual time=0.0363..22.9 rows=2500)
    -&amp;gt; Index range scan using index_assignments_on_person_id
       over (person_id = 301)
         (rows=2500) (actual time=0.0185..0.339 rows=2500)
    -&amp;gt; Index range scan using index_assignments_on_account_id
       over (account_id = 3)
         (rows=317524) (actual time=0.00542..17.8 rows=150000)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;account_id = 4

-&amp;gt; Filter: ((person_id = 401) and (account_id = 4))
     (rows=549) (actual time=0.0423..30.2 rows=2500)
  -&amp;gt; Intersect rows sorted by row ID
       (rows=550) (actual time=0.0417..30 rows=2500)
    -&amp;gt; Index range scan using index_assignments_on_person_id
       over (person_id = 401)
         (rows=2500) (actual time=0.022..0.35 rows=2500)
    -&amp;gt; Index range scan using index_assignments_on_account_id
       over (account_id = 4)
         (rows=394626) (actual time=0.004..23.8 rows=200000)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;account_id = 5

-&amp;gt; Filter: ((person_id = 501) and (account_id = 5))
     (rows=702) (actual time=0.0411..36.5 rows=2500)
  -&amp;gt; Intersect rows sorted by row ID
       (rows=703) (actual time=0.0407..36.4 rows=2500)
    -&amp;gt; Index range scan using index_assignments_on_person_id
       over (person_id = 501)
         (rows=2500) (actual time=0.0147..0.342 rows=2500)
    -&amp;gt; Index range scan using index_assignments_on_account_id
       over (account_id = 5)
         (rows=504504) (actual time=0.00746..29.5 rows=250000)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;account_id = 6

-&amp;gt; Filter: ((person_id = 601) and (account_id = 6))
     (rows=780) (actual time=0.0411..43.3 rows=2500)
  -&amp;gt; Intersect rows sorted by row ID
       (rows=781) (actual time=0.0406..43.2 rows=2500)
    -&amp;gt; Index range scan using index_assignments_on_person_id
       over (person_id = 601)
         (rows=2500) (actual time=0.0165..0.334 rows=2500)
    -&amp;gt; Index range scan using index_assignments_on_account_id
       over (account_id = 6)
         (rows=560484) (actual time=0.00329..35.2 rows=300000)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

&lt;/div&gt;

&lt;p&gt;MySQL reads the &lt;code class=&quot;highlighter-rouge&quot;&gt;person_id&lt;/code&gt; index for the person &lt;em&gt;and&lt;/em&gt; the &lt;code class=&quot;highlighter-rouge&quot;&gt;account_id&lt;/code&gt; index for the
tenant, keeping only the rows that show up in both. That’s called an index merge, more
specifically an intersection of two indexes. Here’s a cute little animation showing
how it exactly works:&lt;/p&gt;

&lt;figure class=&quot;intersect-demo&quot; data-person=&quot;2,6,10,15,20&quot; data-tenant=&quot;20&quot;&gt;&lt;/figure&gt;

&lt;p&gt;The person branch reads 2,500 rows on every slide, but the account branch reads the
whole tenant every time. That explains the first six bars of the chart. Even on the
smallest tenant, that’s 52,500 index rows read to return 2,500. As expected, the
account scan takes up almost all of the runtime.&lt;/p&gt;

&lt;p&gt;Now let’s look at the last two:&lt;/p&gt;

&lt;div class=&quot;plan-carousel&quot; data-legend=&quot;tenant rows&quot; data-labels=&quot;350,000|400,000&quot;&gt;

  &lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;account_id = 7

-&amp;gt; Filter: (account_id = 7)
     (rows=1027) (actual time=0.234..2.41 rows=2500)
  -&amp;gt; Index lookup using index_assignments_on_person_id (person_id=701)
       (rows=2500) (actual time=0.234..2.33 rows=2500)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;account_id = 8

-&amp;gt; Filter: (account_id = 8)
     (rows=1090) (actual time=0.27..2.88 rows=2500)
  -&amp;gt; Index lookup using index_assignments_on_person_id (person_id=801)
       (rows=2500) (actual time=0.269..2.78 rows=2500)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

&lt;/div&gt;

&lt;p&gt;Here the second branch is gone: MySQL does one index lookup on &lt;code class=&quot;highlighter-rouge&quot;&gt;person_id&lt;/code&gt;, reads 2,500
rows and checks the tenant on each row as it comes back. That’s exactly the optimal
plan we described earlier and MySQL found it on its own.&lt;/p&gt;

&lt;h2 id=&quot;why-it-picks-the-merge&quot;&gt;Why it picks the merge&lt;/h2&gt;

&lt;p&gt;So why doesn’t MySQL find the same plan for the first six tenants? In MySQL’s cost model,
fetching full rows from the clustered index is more expensive than reading entries in a
secondary index.&lt;/p&gt;

&lt;p&gt;In this case, the person index alone finds 2,500 rows, and each of them needs a trip to
the clustered index. If MySQL thinks the two indexes overlap on far fewer rows than that,
it expects the merge to cut those trips down dramatically, which makes it worth it.&lt;/p&gt;

&lt;h3 id=&quot;how-mysql-estimates-the-overlap&quot;&gt;How MySQL estimates the overlap&lt;/h3&gt;

&lt;p&gt;As I said, MySQL doesn’t know the two columns are related. It assumes they’re independent and
multiplies:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&lt;span style=&quot;color:var(--tok-keyword)&quot;&gt;2,500&lt;/span&gt; × &lt;span style=&quot;color:var(--tok-number)&quot;&gt;100,346&lt;/span&gt; / &lt;span style=&quot;color:var(--tok-green)&quot;&gt;1,795,256&lt;/span&gt; ≈ 140 rows

&lt;span style=&quot;color:var(--tok-keyword)&quot;&gt;2,500&lt;/span&gt;       &lt;span style=&quot;color:var(--tok-comment)&quot;&gt;rows for person 101, from an index dive&lt;/span&gt;
&lt;span style=&quot;color:var(--tok-number)&quot;&gt;100,346&lt;/span&gt;     &lt;span style=&quot;color:var(--tok-comment)&quot;&gt;rows for tenant 1, from an index dive&lt;/span&gt;
&lt;span style=&quot;color:var(--tok-green)&quot;&gt;1,795,256&lt;/span&gt;   &lt;span style=&quot;color:var(--tok-comment)&quot;&gt;rows in the table, from table statistics&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;MySQL got the first two numbers using an index dive. The name is quite descriptive
on its own: it counts the rows in an index range by looking inside the index. As
we can see, for a small range like the person’s, it’s exact, but for a big one
like the tenant’s, InnoDB samples a few pages and extrapolates. That’s how tenant
1’s 50,000 rows became 100,346. The table’s row count is also an approximation.&lt;/p&gt;

&lt;p&gt;So MySQL expects to fetch only 140 rows from the clustered index when using the merge,
vs 2,500 with the person index alone. In reality, a person lives in the same tenant as
the assignment, so the overlap is all 2,500 rows, instead of the expected 140.&lt;/p&gt;

&lt;p&gt;MySQL isn’t stupid, it only adds the account index to the intersection if that lowers the cost compared
to the person index alone. So as the tenant grows, the account scan gets more expensive and
the estimated overlap grows with it, so there’s less left to save. Somewhere between
300,000 and 350,000 rows it stops paying off and MySQL falls back to the plain lookup.&lt;/p&gt;

&lt;h2 id=&quot;the-fix&quot;&gt;The fix&lt;/h2&gt;

&lt;p&gt;I tried three things before I got to the one I’d actually recommend, which is fixing
the database schema with a composite &lt;code class=&quot;highlighter-rouge&quot;&gt;(account_id, person_id)&lt;/code&gt; index.&lt;/p&gt;

&lt;h3 id=&quot;turning-it-off-everywhere&quot;&gt;Turning it off everywhere&lt;/h3&gt;

&lt;p&gt;Since this optimization is useless in this case, the first thing I tried was turning intersections
off altogether. Setting &lt;code class=&quot;highlighter-rouge&quot;&gt;optimizer_switch=&apos;index_merge_intersection=off&apos;&lt;/code&gt; stops MySQL
from using it. Interestingly, there is a union variant, which this switch doesn’t touch. A query can
merge two indexes over an &lt;code class=&quot;highlighter-rouge&quot;&gt;OR&lt;/code&gt; and speed up the execution.&lt;/p&gt;

&lt;p&gt;I wanted it off for every query the app sends. Rails has
&lt;code class=&quot;highlighter-rouge&quot;&gt;ActiveRecord.query_transformers&lt;/code&gt;, a list of objects that get every SQL string right
before it’s sent to the database. I added one that put the switch on every query as a
hint:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;IndexMergeIntersectionHint&lt;/span&gt;
  &lt;span class=&quot;no&quot;&gt;HINT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;/*+ SET_VAR(optimizer_switch=&apos;index_merge_intersection=off&apos;) */&quot;&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sql&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_connection&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sql&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sub&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;sr&quot;&gt;/\A(\s*)(SELECT|UPDATE|DELETE)\b/i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\\&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\\&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;2 &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;HINT&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;no&quot;&gt;ActiveRecord&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;query_transformers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;IndexMergeIntersectionHint&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is, of course, just a simplified version.&lt;/p&gt;

&lt;p&gt;It fixed the problematic queries I was looking at and broke a couple I wasn’t. It turns out an
intersection of two indexes that aren’t the tenant’s can be the better plan, so I only
wanted it gone for the tenant index.&lt;/p&gt;

&lt;h3 id=&quot;turning-it-off-for-the-tenant-index&quot;&gt;Turning it off for the tenant index&lt;/h3&gt;

&lt;p&gt;MySQL has a narrower hint for exactly that. &lt;code class=&quot;highlighter-rouge&quot;&gt;NO_INDEX_MERGE&lt;/code&gt; takes index names and blocks
merges using those indexes:&lt;/p&gt;

&lt;div class=&quot;language-sql highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/*+ NO_INDEX_MERGE(assignments index_assignments_on_account_id) */&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;assignments&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;WHERE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;account_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;AND&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;person_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;101&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;-&amp;gt; Filter: (account_id = 1)
   (rows=140) (actual time=0.343..3.57 rows=2500)
  -&amp;gt; Index lookup using index_assignments_on_person_id (person_id=101)
     (rows=2500) (actual time=0.343..3.4 rows=2500)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is the optimal plan we’ve already discussed, 2,500 person rows with the tenant
checked on each. The idea was to name just the tenant index and keep it out of every
intersection, while every other query still uses it as usual.&lt;/p&gt;

&lt;p&gt;Unfortunately, that’s not what happens. The hint also stops MySQL from using the named
index for a plain range scan. Say we put it on a query that reads one tenant, but only
the rows after a certain id (any range condition triggers it):&lt;/p&gt;

&lt;div class=&quot;language-sql highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;cm&quot;&gt;/*+ NO_INDEX_MERGE(assignments index_assignments_on_account_id) */&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;COUNT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;assignments&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;WHERE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;account_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;AND&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Without the hint, this is a range scan over the tenant index, 49,000 entries in about
8 ms. With the hint, MySQL can’t use the tenant index for the range anymore, so it scans
the primary key from &lt;code class=&quot;highlighter-rouge&quot;&gt;id &amp;gt; 1000&lt;/code&gt; instead:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;-&amp;gt; Aggregate: count(0) (actual time=225..225 rows=1)
  -&amp;gt; Filter: ((account_id = 1) and (id &amp;gt; 1000))
     (rows=128233) (actual time=0.737..224 rows=49000)
    -&amp;gt; Index range scan using PRIMARY over (1000 &amp;lt; id)
       (rows=897628) (actual time=0.736..188 rows=1.8e+6)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;That’s obviously not how the hint is supposed to work. I reported it to MySQL as
&lt;a href=&quot;https://bugs.mysql.com/bug.php?id=121576&quot;&gt;bug #121576&lt;/a&gt;. They verified it the next day
on 8.0, 8.4 and 9.7. This &lt;em&gt;forced&lt;/em&gt; me to try the next solution.&lt;/p&gt;

&lt;h3 id=&quot;forcing-the-index&quot;&gt;Forcing the index&lt;/h3&gt;

&lt;p&gt;In the end I forced the selective index on the few queries that desperately needed it:&lt;/p&gt;

&lt;div class=&quot;language-sql highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;assignments&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;FORCE&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;INDEX&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index_assignments_on_person_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;WHERE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;account_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;AND&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;person_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;101&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;-&amp;gt; Filter: (account_id = 1)
   (rows=11151) (actual time=0.268..1.85 rows=2500)
  -&amp;gt; Index lookup using index_assignments_on_person_id (person_id=101)
     (rows=78054) (actual time=0.267..1.75 rows=2500)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It worked, but I didn’t really like it. I feel this is really overcomplicating things.
First, you add an index without the &lt;code class=&quot;highlighter-rouge&quot;&gt;account_id&lt;/code&gt; prefix to optimize the size, but then you
have to watch every query the app sends and add the optimizer hint. Not just you, but every
other developer using the table.&lt;/p&gt;

&lt;h3 id=&quot;a-composite-index&quot;&gt;A composite index&lt;/h3&gt;

&lt;p&gt;I would argue the better option is to fix the schema instead. The merge only happens because there
are two separate indexes for MySQL to combine and neither of them covers the whole
filter. In this case we can replace both with a single composite index, tenant first:&lt;/p&gt;

&lt;div class=&quot;language-sql highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;CREATE&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;INDEX&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index_assignments_on_account_person&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;ON&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;assignments&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;account_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;person_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;-&amp;gt; Index lookup using index_assignments_on_account_person
   (account_id=1, person_id=101)
     (rows=2500) (actual time=0.145..1.77 rows=2500)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s run the same eight tenants again with the composite in place:&lt;/p&gt;

&lt;figure class=&quot;chart&quot;&gt;
&lt;svg viewBox=&quot;0 0 640 296&quot; role=&quot;img&quot; aria-label=&quot;Query time by tenant size, before and with the composite index. Before: 9.2 to 43.9 ms up to 300,000 rows, then 2.4 and 2.9 ms. With the composite: 1.7 to 3.0 ms in every tenant.&quot; style=&quot;width:100%;height:auto;font:12px system-ui,sans-serif&quot;&gt;
&lt;line x1=&quot;48&quot; x2=&quot;632&quot; y1=&quot;236.0&quot; y2=&quot;236.0&quot; style=&quot;stroke:var(--rule);stroke-width:1&quot; /&gt;
&lt;text x=&quot;40&quot; y=&quot;240.0&quot; text-anchor=&quot;end&quot; style=&quot;fill:var(--muted)&quot;&gt;0&lt;/text&gt;
&lt;line x1=&quot;48&quot; x2=&quot;632&quot; y1=&quot;196.8&quot; y2=&quot;196.8&quot; style=&quot;stroke:var(--rule);stroke-width:1&quot; /&gt;
&lt;text x=&quot;40&quot; y=&quot;200.8&quot; text-anchor=&quot;end&quot; style=&quot;fill:var(--muted)&quot;&gt;10&lt;/text&gt;
&lt;line x1=&quot;48&quot; x2=&quot;632&quot; y1=&quot;157.6&quot; y2=&quot;157.6&quot; style=&quot;stroke:var(--rule);stroke-width:1&quot; /&gt;
&lt;text x=&quot;40&quot; y=&quot;161.6&quot; text-anchor=&quot;end&quot; style=&quot;fill:var(--muted)&quot;&gt;20&lt;/text&gt;
&lt;line x1=&quot;48&quot; x2=&quot;632&quot; y1=&quot;118.4&quot; y2=&quot;118.4&quot; style=&quot;stroke:var(--rule);stroke-width:1&quot; /&gt;
&lt;text x=&quot;40&quot; y=&quot;122.4&quot; text-anchor=&quot;end&quot; style=&quot;fill:var(--muted)&quot;&gt;30&lt;/text&gt;
&lt;line x1=&quot;48&quot; x2=&quot;632&quot; y1=&quot;79.2&quot; y2=&quot;79.2&quot; style=&quot;stroke:var(--rule);stroke-width:1&quot; /&gt;
&lt;text x=&quot;40&quot; y=&quot;83.2&quot; text-anchor=&quot;end&quot; style=&quot;fill:var(--muted)&quot;&gt;40&lt;/text&gt;
&lt;line x1=&quot;48&quot; x2=&quot;632&quot; y1=&quot;40.0&quot; y2=&quot;40.0&quot; style=&quot;stroke:var(--rule);stroke-width:1&quot; /&gt;
&lt;text x=&quot;40&quot; y=&quot;44.0&quot; text-anchor=&quot;end&quot; style=&quot;fill:var(--muted)&quot;&gt;50&lt;/text&gt;
&lt;text x=&quot;40&quot; y=&quot;28&quot; text-anchor=&quot;end&quot; style=&quot;fill:var(--muted)&quot;&gt;ms&lt;/text&gt;
&lt;rect x=&quot;56&quot; y=&quot;8&quot; width=&quot;12&quot; height=&quot;12&quot; rx=&quot;2&quot; style=&quot;fill:var(--link)&quot; /&gt;&lt;text x=&quot;74&quot; y=&quot;18&quot; style=&quot;fill:var(--text)&quot;&gt;before&lt;/text&gt;
&lt;rect x=&quot;136&quot; y=&quot;8&quot; width=&quot;12&quot; height=&quot;12&quot; rx=&quot;2&quot; style=&quot;fill:var(--muted)&quot; /&gt;&lt;text x=&quot;154&quot; y=&quot;18&quot; style=&quot;fill:var(--text)&quot;&gt;with composite&lt;/text&gt;
&lt;path d=&quot;M59.5 236 V203.9 Q59.5 199.9 63.5 199.9 H79.5 Q83.5 199.9 83.5 203.9 V236 Z&quot; style=&quot;fill:var(--link)&quot;&gt;&lt;title&gt;50k rows, before: 9.2 ms&lt;/title&gt;&lt;/path&gt;
&lt;path d=&quot;M85.5 236 V232.7 Q85.5 229.3 88.8 229.3 H106.2 Q109.5 229.3 109.5 232.7 V236 Z&quot; style=&quot;fill:var(--muted)&quot;&gt;&lt;title&gt;50k rows, with composite: 1.7 ms&lt;/title&gt;&lt;/path&gt;
&lt;text x=&quot;71.5&quot; y=&quot;193.9&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--text);font-size:11px&quot;&gt;9.2&lt;/text&gt;
&lt;text x=&quot;97.5&quot; y=&quot;223.3&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--text);font-size:11px&quot;&gt;1.7&lt;/text&gt;
&lt;text x=&quot;84.5&quot; y=&quot;254&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;50k&lt;/text&gt;
&lt;path d=&quot;M132.5 236 V175.7 Q132.5 171.7 136.5 171.7 H152.5 Q156.5 171.7 156.5 175.7 V236 Z&quot; style=&quot;fill:var(--link)&quot;&gt;&lt;title&gt;100k rows, before: 16.4 ms&lt;/title&gt;&lt;/path&gt;
&lt;path d=&quot;M158.5 236 V232.5 Q158.5 228.9 162.0 228.9 H179.0 Q182.5 228.9 182.5 232.5 V236 Z&quot; style=&quot;fill:var(--muted)&quot;&gt;&lt;title&gt;100k rows, with composite: 1.8 ms&lt;/title&gt;&lt;/path&gt;
&lt;text x=&quot;144.5&quot; y=&quot;165.7&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--text);font-size:11px&quot;&gt;16.4&lt;/text&gt;
&lt;text x=&quot;170.5&quot; y=&quot;222.9&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--text);font-size:11px&quot;&gt;1.8&lt;/text&gt;
&lt;text x=&quot;157.5&quot; y=&quot;254&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;100k&lt;/text&gt;
&lt;path d=&quot;M205.5 236 V149.4 Q205.5 145.4 209.5 145.4 H225.5 Q229.5 145.4 229.5 149.4 V236 Z&quot; style=&quot;fill:var(--link)&quot;&gt;&lt;title&gt;150k rows, before: 23.1 ms&lt;/title&gt;&lt;/path&gt;
&lt;path d=&quot;M231.5 236 V231.8 Q231.5 227.8 235.5 227.8 H251.5 Q255.5 227.8 255.5 231.8 V236 Z&quot; style=&quot;fill:var(--muted)&quot;&gt;&lt;title&gt;150k rows, with composite: 2.1 ms&lt;/title&gt;&lt;/path&gt;
&lt;text x=&quot;217.5&quot; y=&quot;139.4&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--text);font-size:11px&quot;&gt;23.1&lt;/text&gt;
&lt;text x=&quot;243.5&quot; y=&quot;221.8&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--text);font-size:11px&quot;&gt;2.1&lt;/text&gt;
&lt;text x=&quot;230.5&quot; y=&quot;254&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;150k&lt;/text&gt;
&lt;path d=&quot;M278.5 236 V121.2 Q278.5 117.2 282.5 117.2 H298.5 Q302.5 117.2 302.5 121.2 V236 Z&quot; style=&quot;fill:var(--link)&quot;&gt;&lt;title&gt;200k rows, before: 30.3 ms&lt;/title&gt;&lt;/path&gt;
&lt;path d=&quot;M304.5 236 V231.0 Q304.5 227.0 308.5 227.0 H324.5 Q328.5 227.0 328.5 231.0 V236 Z&quot; style=&quot;fill:var(--muted)&quot;&gt;&lt;title&gt;200k rows, with composite: 2.3 ms&lt;/title&gt;&lt;/path&gt;
&lt;text x=&quot;290.5&quot; y=&quot;111.2&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--text);font-size:11px&quot;&gt;30.3&lt;/text&gt;
&lt;text x=&quot;316.5&quot; y=&quot;221.0&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--text);font-size:11px&quot;&gt;2.3&lt;/text&gt;
&lt;text x=&quot;303.5&quot; y=&quot;254&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;200k&lt;/text&gt;
&lt;path d=&quot;M351.5 236 V95.4 Q351.5 91.4 355.5 91.4 H371.5 Q375.5 91.4 375.5 95.4 V236 Z&quot; style=&quot;fill:var(--link)&quot;&gt;&lt;title&gt;250k rows, before: 36.9 ms&lt;/title&gt;&lt;/path&gt;
&lt;path d=&quot;M377.5 236 V231.4 Q377.5 227.4 381.5 227.4 H397.5 Q401.5 227.4 401.5 231.4 V236 Z&quot; style=&quot;fill:var(--muted)&quot;&gt;&lt;title&gt;250k rows, with composite: 2.2 ms&lt;/title&gt;&lt;/path&gt;
&lt;text x=&quot;363.5&quot; y=&quot;85.4&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--text);font-size:11px&quot;&gt;36.9&lt;/text&gt;
&lt;text x=&quot;389.5&quot; y=&quot;221.4&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--text);font-size:11px&quot;&gt;2.2&lt;/text&gt;
&lt;text x=&quot;376.5&quot; y=&quot;254&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;250k&lt;/text&gt;
&lt;path d=&quot;M424.5 236 V67.9 Q424.5 63.9 428.5 63.9 H444.5 Q448.5 63.9 448.5 67.9 V236 Z&quot; style=&quot;fill:var(--link)&quot;&gt;&lt;title&gt;300k rows, before: 43.9 ms&lt;/title&gt;&lt;/path&gt;
&lt;path d=&quot;M450.5 236 V230.2 Q450.5 226.2 454.5 226.2 H470.5 Q474.5 226.2 474.5 230.2 V236 Z&quot; style=&quot;fill:var(--muted)&quot;&gt;&lt;title&gt;300k rows, with composite: 2.5 ms&lt;/title&gt;&lt;/path&gt;
&lt;text x=&quot;436.5&quot; y=&quot;57.9&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--text);font-size:11px&quot;&gt;43.9&lt;/text&gt;
&lt;text x=&quot;462.5&quot; y=&quot;220.2&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--text);font-size:11px&quot;&gt;2.5&lt;/text&gt;
&lt;text x=&quot;449.5&quot; y=&quot;254&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;300k&lt;/text&gt;
&lt;path d=&quot;M497.5 236 V230.6 Q497.5 226.6 501.5 226.6 H517.5 Q521.5 226.6 521.5 230.6 V236 Z&quot; style=&quot;fill:var(--link)&quot;&gt;&lt;title&gt;350k rows, before: 2.4 ms&lt;/title&gt;&lt;/path&gt;
&lt;path d=&quot;M523.5 236 V230.2 Q523.5 226.2 527.5 226.2 H543.5 Q547.5 226.2 547.5 230.2 V236 Z&quot; style=&quot;fill:var(--muted)&quot;&gt;&lt;title&gt;350k rows, with composite: 2.5 ms&lt;/title&gt;&lt;/path&gt;
&lt;text x=&quot;509.5&quot; y=&quot;220.6&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--text);font-size:11px&quot;&gt;2.4&lt;/text&gt;
&lt;text x=&quot;535.5&quot; y=&quot;220.2&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--text);font-size:11px&quot;&gt;2.5&lt;/text&gt;
&lt;text x=&quot;522.5&quot; y=&quot;254&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;350k&lt;/text&gt;
&lt;path d=&quot;M570.5 236 V228.6 Q570.5 224.6 574.5 224.6 H590.5 Q594.5 224.6 594.5 228.6 V236 Z&quot; style=&quot;fill:var(--link)&quot;&gt;&lt;title&gt;400k rows, before: 2.9 ms&lt;/title&gt;&lt;/path&gt;
&lt;path d=&quot;M596.5 236 V228.2 Q596.5 224.2 600.5 224.2 H616.5 Q620.5 224.2 620.5 228.2 V236 Z&quot; style=&quot;fill:var(--muted)&quot;&gt;&lt;title&gt;400k rows, with composite: 3.0 ms&lt;/title&gt;&lt;/path&gt;
&lt;text x=&quot;582.5&quot; y=&quot;218.6&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--text);font-size:11px&quot;&gt;2.9&lt;/text&gt;
&lt;text x=&quot;608.5&quot; y=&quot;218.2&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--text);font-size:11px&quot;&gt;3.0&lt;/text&gt;
&lt;text x=&quot;595.5&quot; y=&quot;254&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;400k&lt;/text&gt;
&lt;text x=&quot;340.0&quot; y=&quot;274&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;tenant rows&lt;/text&gt;
&lt;/svg&gt;
&lt;/figure&gt;

&lt;p&gt;Every tenant now reads 2,500 rows in 1.7 to 3.0 ms. There’s still a slight increase as
the tenant grows, but that’s not the plan’s fault anymore. Every tenant reads the same
2,500 index entries, but in a 400,000-row tenant the person’s rows are spread over a much
wider range of the primary key, so they end up on more pages.&lt;/p&gt;

&lt;h2 id=&quot;takeaway&quot;&gt;Takeaway&lt;/h2&gt;

&lt;p&gt;I think the index merge intersection is a really smart and useful optimization. But it
can obviously hurt in a multi-tenanted setup. I believe a practical piece of advice we can all learn
from this is to prefix every index with &lt;code class=&quot;highlighter-rouge&quot;&gt;account_id&lt;/code&gt;, otherwise there’s a risk of a harmful,
failed optimization like I’ve shown. In Postgres you can define the relationship between
two columns via &lt;a href=&quot;https://www.postgresql.org/docs/current/sql-createstatistics.html&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;CREATE STATISTICS&lt;/code&gt;&lt;/a&gt;,
but for MySQL, this general rule is the best I came up with.&lt;/p&gt;

&lt;p&gt;So in practice: find tables where &lt;code class=&quot;highlighter-rouge&quot;&gt;account_id&lt;/code&gt; and a foreign key each have their own index,
and check with &lt;code class=&quot;highlighter-rouge&quot;&gt;EXPLAIN ANALYZE&lt;/code&gt; whether MySQL intersects them
(&lt;code class=&quot;highlighter-rouge&quot;&gt;Intersect rows sorted by row ID&lt;/code&gt;). If it does, a FORCE INDEX on the foreign key index stops
it right away. The better long-term fix is to replace that index with one prefixed with account_id,
so you make the optimizer’s job easier.&lt;/p&gt;</content><author><name></name></author></entry><entry><title type="html">The column you never render still costs you</title><link href="https://filipsucic.com/posts/column-you-never-render-still-costs-you/" rel="alternate" type="text/html" title="The column you never render still costs you" /><published>2026-10-12T00:00:00+00:00</published><updated>2026-10-12T00:00:00+00:00</updated><id>repo://posts.collection/_posts/2026-10-12-column-you-never-render-still-costs-you.md</id><content type="html" xml:base="https://filipsucic.com/posts/column-you-never-render-still-costs-you/">&lt;p&gt;I was watching Cristian Planas’s RailsConf talk, &lt;a href=&quot;https://www.youtube.com/watch?v=mJw3al4Ms2o&quot;&gt;A Rails Performance
Guidebook&lt;/a&gt;, where he flags greedy
selects (pulling back more columns from the database than you’ll actually use)
as an antipattern. It reminded me of an endpoint whose p99 was way too
high because of a few very “heavy” columns (columns that contain large amounts of
data, like JSON or TEXT), on a screen that never even showed them.
In this post I’ll demonstrate why Cristian’s point stands.&lt;/p&gt;

&lt;h2 id=&quot;the-setup&quot;&gt;The setup&lt;/h2&gt;

&lt;p&gt;All queries below run against a stripped-down &lt;code class=&quot;highlighter-rouge&quot;&gt;calls&lt;/code&gt; table. Most of
it is narrow, but a call keeps its whole transcript:&lt;/p&gt;

&lt;div class=&quot;language-sql highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;CREATE&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;TABLE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;calls&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;              &lt;span class=&quot;nb&quot;&gt;BIGINT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;PRIMARY&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;KEY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;account_id&lt;/span&gt;      &lt;span class=&quot;nb&quot;&gt;BIGINT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NOT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;summary&lt;/span&gt;         &lt;span class=&quot;nb&quot;&gt;MEDIUMTEXT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;-- 2 KB&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;notes&lt;/span&gt;           &lt;span class=&quot;nb&quot;&gt;MEDIUMTEXT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;-- 1 KB&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;transcript&lt;/span&gt;      &lt;span class=&quot;n&quot;&gt;JSON&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;        &lt;span class=&quot;c1&quot;&gt;-- ~290 KB&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;transcript_text&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;MEDIUMTEXT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;-- 36 KB&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;KEY&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index_calls_on_account_id&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;account_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Every call belongs to an account and the only secondary index is on
&lt;code class=&quot;highlighter-rouge&quot;&gt;account_id&lt;/code&gt;. I measured on MySQL 8.4 running locally, with
the default 128 MB buffer pool. Production gives it far more memory,
but it also has far more data than one 5 GB table, so most of the
transcripts won’t be cached there either.&lt;/p&gt;

&lt;p&gt;One account gets 15,000 calls, 7 in 8 of them completed. The list can be sorted
two ways: by &lt;code class=&quot;highlighter-rouge&quot;&gt;id&lt;/code&gt;, which the index serves, or by &lt;code class=&quot;highlighter-rouge&quot;&gt;started_at&lt;/code&gt;, which needs a
filesort. &lt;code class=&quot;highlighter-rouge&quot;&gt;started_at&lt;/code&gt; is shuffled against &lt;code class=&quot;highlighter-rouge&quot;&gt;id&lt;/code&gt;, so the two orders really return
different pages.&lt;/p&gt;

&lt;details&gt;
  &lt;summary&gt;Full schema and seed&lt;/summary&gt;

  &lt;div class=&quot;language-sql highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;CREATE&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;TABLE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;calls&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;              &lt;span class=&quot;nb&quot;&gt;BIGINT&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AUTO_INCREMENT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;PRIMARY&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;KEY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;account_id&lt;/span&gt;      &lt;span class=&quot;nb&quot;&gt;BIGINT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NOT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;           &lt;span class=&quot;nb&quot;&gt;VARCHAR&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NOT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;status&lt;/span&gt;          &lt;span class=&quot;nb&quot;&gt;VARCHAR&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NOT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;started_at&lt;/span&gt;      &lt;span class=&quot;nb&quot;&gt;DATETIME&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NOT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;deleted_at&lt;/span&gt;      &lt;span class=&quot;nb&quot;&gt;DATETIME&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;summary&lt;/span&gt;         &lt;span class=&quot;nb&quot;&gt;MEDIUMTEXT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;notes&lt;/span&gt;           &lt;span class=&quot;nb&quot;&gt;MEDIUMTEXT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;transcript&lt;/span&gt;      &lt;span class=&quot;n&quot;&gt;JSON&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;transcript_text&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;MEDIUMTEXT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;KEY&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index_calls_on_account_id&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;account_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ENGINE&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;InnoDB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;DELIMITER&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;$$&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;CREATE&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;PROCEDURE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;BEGIN&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;DECLARE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;INT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;DEFAULT&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;START&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TRANSACTION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;WHILE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;15000&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;DO&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;INSERT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;INTO&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;calls&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;account_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;started_at&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                       &lt;span class=&quot;n&quot;&gt;summary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;notes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;transcript&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;transcript_text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;VALUES&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CONCAT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;Call &apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;IF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;scheduled&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;completed&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;s1&quot;&gt;&apos;2024-01-01&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;INTERVAL&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;7919&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;15000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;HOUR&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;REPEAT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;s&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;REPEAT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;n&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;JSON_OBJECT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;text&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;REPEAT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;x&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;290000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)),&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;REPEAT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;m&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;36000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;SET&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;IF&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1000&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;THEN&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;COMMIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;START&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TRANSACTION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;END&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;END&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WHILE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;COMMIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;END&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;$$&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;DELIMITER&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;CALL&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;ANALYZE&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;TABLE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;calls&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

  &lt;p&gt;The sparse variant later on is the same seed with
&lt;code class=&quot;highlighter-rouge&quot;&gt;IF(i &amp;lt;= 2, &apos;completed&apos;, &apos;scheduled&apos;)&lt;/code&gt;, so only the two calls with the lowest ids are
completed.&lt;/p&gt;

&lt;/details&gt;

&lt;h2 id=&quot;sparse-fieldsets-trim-the-response&quot;&gt;Sparse fieldsets trim the response&lt;/h2&gt;

&lt;p&gt;If you’ve ever used an API that follows the JSON:API spec, you’re already aware
of the “sparse fieldset” feature: a client asks for something like
&lt;code class=&quot;highlighter-rouge&quot;&gt;fields[calls]=title,started_at&lt;/code&gt; and only gets those attributes back. On an
endpoint like the one from the intro, that’s the obvious way to avoid
serializing the heavy columns.&lt;/p&gt;

&lt;p&gt;The problem comes when the fieldset only filters the response from the API to
the client. If it doesn’t affect the database query, the query will still run
&lt;code class=&quot;highlighter-rouge&quot;&gt;SELECT *&lt;/code&gt;, MySQL will read the transcripts, and Rails will load them into memory.&lt;/p&gt;

&lt;h2 id=&quot;big-values-go-to-overflow-pages&quot;&gt;Big values go to overflow pages&lt;/h2&gt;

&lt;p&gt;InnoDB stores a table as a B-tree ordered by the primary key (the clustered index),
with the full rows sitting in its leaf pages. A secondary index points to rows by id,
so to read a row found through it, InnoDB looks the id up in the clustered index.&lt;/p&gt;

&lt;p&gt;A page is 16 KB, and a row has to fit in roughly half of one. When it doesn’t,
&lt;code class=&quot;highlighter-rouge&quot;&gt;ROW_FORMAT=DYNAMIC&lt;/code&gt; moves the longest values out to overflow pages and leaves a
20-byte pointer in the row. So the heavy call columns, &lt;code class=&quot;highlighter-rouge&quot;&gt;transcript&lt;/code&gt; and
&lt;code class=&quot;highlighter-rouge&quot;&gt;transcript_text&lt;/code&gt;, live off-page. I’ll call these values blobs from here on.&lt;/p&gt;

&lt;p&gt;Every blob is extra pages on top of the row’s, about 18 for one &lt;code class=&quot;highlighter-rouge&quot;&gt;transcript&lt;/code&gt;,
and each of them comes from disk when it isn’t in the buffer pool (which on an
SSD is roughly a thousand times slower than memory). Reading a lot of blobs also
pollutes the buffer pool by pushing out the row pages other queries need.&lt;/p&gt;

&lt;figure class=&quot;offpage&quot; style=&quot;margin:2.2em 0&quot;&gt;
&lt;svg id=&quot;op-svg&quot; class=&quot;op&quot; viewBox=&quot;0 0 760 300&quot; role=&quot;img&quot; aria-labelledby=&quot;op-t op-d&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; style=&quot;width:100%;height:auto&quot;&gt;
&lt;title id=&quot;op-t&quot;&gt;One call row and its overflow pages&lt;/title&gt;
&lt;desc id=&quot;op-d&quot;&gt;A call row on its 16 KB leaf page holds id, title, status, started_at and a 2 KB summary inline. The transcript and transcript_text columns hold only a 20-byte pointer each, leading to chains of about 18 and 3 overflow pages.&lt;/desc&gt;
&lt;style&gt;
#op-svg{display:block}#op-svg-m{display:none}@media (max-width:600px){#op-svg{display:none}#op-svg-m{display:block}}
.op text{font-family:-apple-system,BlinkMacSystemFont,&quot;Segoe UI&quot;,Roboto,Helvetica,Arial,sans-serif}
.op .ttl{font-size:13px;font-weight:600;fill:var(--text)}
.op .mut{font-size:12px;fill:var(--muted)}
.op .rt{font-size:13px;font-weight:600;fill:var(--link)}
.op .ot{font-size:12px;fill:var(--tok-number)}
.op .ott{font-size:13px;font-weight:600;fill:var(--tok-number)}
.op .box{fill:var(--code-bg);stroke:var(--muted);stroke-width:1.3}
.op .leaf{fill:none;stroke:var(--link);stroke-width:1.5}
.op .ptr{fill:var(--code-bg);stroke:var(--tok-number);stroke-width:1.5}
.op .ovf{fill:var(--code-bg);stroke:var(--tok-number);stroke-width:1.3}
.op .edge{fill:none;stroke:var(--tok-number);stroke-width:1.4}
.op .brace{fill:none;stroke:var(--muted);stroke-width:1.3}
&lt;/style&gt;
&lt;defs&gt;
&lt;marker id=&quot;op-ah&quot; viewBox=&quot;0 0 10 10&quot; refX=&quot;8.5&quot; refY=&quot;5&quot; markerWidth=&quot;6&quot; markerHeight=&quot;6&quot; orient=&quot;auto-start-reverse&quot;&gt;&lt;path d=&quot;M0 0 L10 5 L0 10 z&quot; fill=&quot;var(--tok-number)&quot; /&gt;&lt;/marker&gt;
&lt;/defs&gt;
&lt;text class=&quot;rt&quot; x=&quot;10&quot; y=&quot;18&quot;&gt;row page, a 16 KB leaf&lt;/text&gt;
&lt;rect class=&quot;leaf&quot; x=&quot;4&quot; y=&quot;28&quot; width=&quot;752&quot; height=&quot;74&quot; rx=&quot;6&quot; /&gt;
&lt;rect class=&quot;box&quot; x=&quot;10&quot; y=&quot;38&quot; width=&quot;60&quot; height=&quot;54&quot; /&gt;
&lt;text class=&quot;ttl&quot; x=&quot;40&quot; y=&quot;60&quot; text-anchor=&quot;middle&quot;&gt;id&lt;/text&gt;
&lt;text class=&quot;mut&quot; x=&quot;40&quot; y=&quot;80&quot; text-anchor=&quot;middle&quot;&gt;8 B&lt;/text&gt;
&lt;rect class=&quot;box&quot; x=&quot;70&quot; y=&quot;38&quot; width=&quot;90&quot; height=&quot;54&quot; /&gt;
&lt;text class=&quot;ttl&quot; x=&quot;115&quot; y=&quot;60&quot; text-anchor=&quot;middle&quot;&gt;title&lt;/text&gt;
&lt;text class=&quot;mut&quot; x=&quot;115&quot; y=&quot;80&quot; text-anchor=&quot;middle&quot;&gt;varchar&lt;/text&gt;
&lt;rect class=&quot;box&quot; x=&quot;160&quot; y=&quot;38&quot; width=&quot;80&quot; height=&quot;54&quot; /&gt;
&lt;text class=&quot;ttl&quot; x=&quot;200&quot; y=&quot;60&quot; text-anchor=&quot;middle&quot;&gt;status&lt;/text&gt;
&lt;text class=&quot;mut&quot; x=&quot;200&quot; y=&quot;80&quot; text-anchor=&quot;middle&quot;&gt;varchar&lt;/text&gt;
&lt;rect class=&quot;box&quot; x=&quot;240&quot; y=&quot;38&quot; width=&quot;100&quot; height=&quot;54&quot; /&gt;
&lt;text class=&quot;ttl&quot; x=&quot;290&quot; y=&quot;60&quot; text-anchor=&quot;middle&quot;&gt;started_at&lt;/text&gt;
&lt;text class=&quot;mut&quot; x=&quot;290&quot; y=&quot;80&quot; text-anchor=&quot;middle&quot;&gt;datetime&lt;/text&gt;
&lt;rect class=&quot;box&quot; x=&quot;340&quot; y=&quot;38&quot; width=&quot;120&quot; height=&quot;54&quot; /&gt;
&lt;text class=&quot;ttl&quot; x=&quot;400&quot; y=&quot;60&quot; text-anchor=&quot;middle&quot;&gt;summary&lt;/text&gt;
&lt;text class=&quot;mut&quot; x=&quot;400&quot; y=&quot;80&quot; text-anchor=&quot;middle&quot;&gt;2 KB, inline&lt;/text&gt;
&lt;rect class=&quot;ptr&quot; x=&quot;460&quot; y=&quot;38&quot; width=&quot;130&quot; height=&quot;54&quot; /&gt;
&lt;text class=&quot;ott&quot; x=&quot;525&quot; y=&quot;60&quot; text-anchor=&quot;middle&quot;&gt;transcript&lt;/text&gt;
&lt;text class=&quot;ot&quot; x=&quot;525&quot; y=&quot;80&quot; text-anchor=&quot;middle&quot;&gt;20 B pointer&lt;/text&gt;
&lt;rect class=&quot;ptr&quot; x=&quot;590&quot; y=&quot;38&quot; width=&quot;160&quot; height=&quot;54&quot; /&gt;
&lt;text class=&quot;ott&quot; x=&quot;670&quot; y=&quot;60&quot; text-anchor=&quot;middle&quot;&gt;transcript_text&lt;/text&gt;
&lt;text class=&quot;ot&quot; x=&quot;670&quot; y=&quot;80&quot; text-anchor=&quot;middle&quot;&gt;20 B pointer&lt;/text&gt;
&lt;path class=&quot;brace&quot; d=&quot;M10 116 V124 H460 V116&quot; /&gt;
&lt;text class=&quot;mut&quot; x=&quot;235&quot; y=&quot;144&quot; text-anchor=&quot;middle&quot;&gt;on the row page, free once the row is read&lt;/text&gt;
&lt;line class=&quot;edge&quot; x1=&quot;525&quot; y1=&quot;92&quot; x2=&quot;525&quot; y2=&quot;138&quot; marker-end=&quot;url(#op-ah)&quot; /&gt;
&lt;line class=&quot;edge&quot; x1=&quot;670&quot; y1=&quot;92&quot; x2=&quot;670&quot; y2=&quot;138&quot; marker-end=&quot;url(#op-ah)&quot; /&gt;
&lt;rect class=&quot;ovf&quot; x=&quot;465&quot; y=&quot;140&quot; width=&quot;120&quot; height=&quot;30&quot; rx=&quot;4&quot; /&gt;
&lt;text class=&quot;ot&quot; x=&quot;525&quot; y=&quot;160&quot; text-anchor=&quot;middle&quot;&gt;overflow 16 KB&lt;/text&gt;
&lt;line class=&quot;edge&quot; x1=&quot;525&quot; y1=&quot;170&quot; x2=&quot;525&quot; y2=&quot;182&quot; marker-end=&quot;url(#op-ah)&quot; /&gt;
&lt;rect class=&quot;ovf&quot; x=&quot;465&quot; y=&quot;184&quot; width=&quot;120&quot; height=&quot;30&quot; rx=&quot;4&quot; /&gt;
&lt;text class=&quot;ot&quot; x=&quot;525&quot; y=&quot;204&quot; text-anchor=&quot;middle&quot;&gt;overflow 16 KB&lt;/text&gt;
&lt;line class=&quot;edge&quot; x1=&quot;525&quot; y1=&quot;214&quot; x2=&quot;525&quot; y2=&quot;226&quot; marker-end=&quot;url(#op-ah)&quot; /&gt;
&lt;rect class=&quot;ovf&quot; x=&quot;465&quot; y=&quot;228&quot; width=&quot;120&quot; height=&quot;30&quot; rx=&quot;4&quot; stroke-dasharray=&quot;4 3&quot; /&gt;
&lt;text class=&quot;ot&quot; x=&quot;525&quot; y=&quot;248&quot; text-anchor=&quot;middle&quot;&gt;… 16 more&lt;/text&gt;
&lt;text class=&quot;ott&quot; x=&quot;525&quot; y=&quot;282&quot; text-anchor=&quot;middle&quot;&gt;~290 KB ≈ 18 pages&lt;/text&gt;
&lt;rect class=&quot;ovf&quot; x=&quot;610&quot; y=&quot;140&quot; width=&quot;120&quot; height=&quot;30&quot; rx=&quot;4&quot; /&gt;
&lt;text class=&quot;ot&quot; x=&quot;670&quot; y=&quot;160&quot; text-anchor=&quot;middle&quot;&gt;overflow 16 KB&lt;/text&gt;
&lt;line class=&quot;edge&quot; x1=&quot;670&quot; y1=&quot;170&quot; x2=&quot;670&quot; y2=&quot;182&quot; marker-end=&quot;url(#op-ah)&quot; /&gt;
&lt;rect class=&quot;ovf&quot; x=&quot;610&quot; y=&quot;184&quot; width=&quot;120&quot; height=&quot;30&quot; rx=&quot;4&quot; /&gt;
&lt;text class=&quot;ot&quot; x=&quot;670&quot; y=&quot;204&quot; text-anchor=&quot;middle&quot;&gt;overflow 16 KB&lt;/text&gt;
&lt;line class=&quot;edge&quot; x1=&quot;670&quot; y1=&quot;214&quot; x2=&quot;670&quot; y2=&quot;226&quot; marker-end=&quot;url(#op-ah)&quot; /&gt;
&lt;rect class=&quot;ovf&quot; x=&quot;610&quot; y=&quot;228&quot; width=&quot;120&quot; height=&quot;30&quot; rx=&quot;4&quot; /&gt;
&lt;text class=&quot;ot&quot; x=&quot;670&quot; y=&quot;248&quot; text-anchor=&quot;middle&quot;&gt;overflow 16 KB&lt;/text&gt;
&lt;text class=&quot;ott&quot; x=&quot;670&quot; y=&quot;282&quot; text-anchor=&quot;middle&quot;&gt;~36 KB ≈ 3 pages&lt;/text&gt;
&lt;/svg&gt;
&lt;svg id=&quot;op-svg-m&quot; class=&quot;op&quot; viewBox=&quot;0 0 340 300&quot; role=&quot;img&quot; aria-labelledby=&quot;op-tm op-dm&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; style=&quot;width:100%;height:auto&quot;&gt;
&lt;title id=&quot;op-tm&quot;&gt;One call row and its overflow pages&lt;/title&gt;
&lt;desc id=&quot;op-dm&quot;&gt;A call row on its 16 KB leaf page holds id, title, status, started_at and a 2 KB summary inline. The transcript and transcript_text columns hold only a 20-byte pointer each, leading to chains of about 18 and 3 overflow pages.&lt;/desc&gt;
&lt;defs&gt;
&lt;marker id=&quot;op-ahm&quot; viewBox=&quot;0 0 10 10&quot; refX=&quot;8.5&quot; refY=&quot;5&quot; markerWidth=&quot;6&quot; markerHeight=&quot;6&quot; orient=&quot;auto-start-reverse&quot;&gt;&lt;path d=&quot;M0 0 L10 5 L0 10 z&quot; fill=&quot;var(--tok-number)&quot; /&gt;&lt;/marker&gt;
&lt;/defs&gt;
&lt;text class=&quot;rt&quot; x=&quot;4&quot; y=&quot;14&quot;&gt;row page, a 16 KB leaf&lt;/text&gt;
&lt;rect class=&quot;leaf&quot; x=&quot;2&quot; y=&quot;22&quot; width=&quot;336&quot; height=&quot;104&quot; rx=&quot;6&quot; /&gt;
&lt;rect class=&quot;box&quot; x=&quot;10&quot; y=&quot;30&quot; width=&quot;50&quot; height=&quot;44&quot; /&gt;
&lt;text class=&quot;ttl&quot; x=&quot;35&quot; y=&quot;48&quot; text-anchor=&quot;middle&quot;&gt;id&lt;/text&gt;
&lt;text class=&quot;mut&quot; x=&quot;35&quot; y=&quot;65&quot; text-anchor=&quot;middle&quot;&gt;8 B&lt;/text&gt;
&lt;rect class=&quot;box&quot; x=&quot;60&quot; y=&quot;30&quot; width=&quot;80&quot; height=&quot;44&quot; /&gt;
&lt;text class=&quot;ttl&quot; x=&quot;100&quot; y=&quot;48&quot; text-anchor=&quot;middle&quot;&gt;title&lt;/text&gt;
&lt;text class=&quot;mut&quot; x=&quot;100&quot; y=&quot;65&quot; text-anchor=&quot;middle&quot;&gt;varchar&lt;/text&gt;
&lt;rect class=&quot;box&quot; x=&quot;140&quot; y=&quot;30&quot; width=&quot;80&quot; height=&quot;44&quot; /&gt;
&lt;text class=&quot;ttl&quot; x=&quot;180&quot; y=&quot;48&quot; text-anchor=&quot;middle&quot;&gt;status&lt;/text&gt;
&lt;text class=&quot;mut&quot; x=&quot;180&quot; y=&quot;65&quot; text-anchor=&quot;middle&quot;&gt;varchar&lt;/text&gt;
&lt;rect class=&quot;box&quot; x=&quot;220&quot; y=&quot;30&quot; width=&quot;110&quot; height=&quot;44&quot; /&gt;
&lt;text class=&quot;ttl&quot; x=&quot;275&quot; y=&quot;48&quot; text-anchor=&quot;middle&quot;&gt;started_at&lt;/text&gt;
&lt;text class=&quot;mut&quot; x=&quot;275&quot; y=&quot;65&quot; text-anchor=&quot;middle&quot;&gt;datetime&lt;/text&gt;
&lt;rect class=&quot;box&quot; x=&quot;10&quot; y=&quot;74&quot; width=&quot;90&quot; height=&quot;44&quot; /&gt;
&lt;text class=&quot;ttl&quot; x=&quot;55&quot; y=&quot;92&quot; text-anchor=&quot;middle&quot;&gt;summary&lt;/text&gt;
&lt;text class=&quot;mut&quot; x=&quot;55&quot; y=&quot;109&quot; text-anchor=&quot;middle&quot;&gt;2 KB, inline&lt;/text&gt;
&lt;rect class=&quot;ptr&quot; x=&quot;100&quot; y=&quot;74&quot; width=&quot;100&quot; height=&quot;44&quot; /&gt;
&lt;text class=&quot;ott&quot; x=&quot;150&quot; y=&quot;92&quot; text-anchor=&quot;middle&quot;&gt;transcript&lt;/text&gt;
&lt;text class=&quot;ot&quot; x=&quot;150&quot; y=&quot;109&quot; text-anchor=&quot;middle&quot;&gt;20 B pointer&lt;/text&gt;
&lt;rect class=&quot;ptr&quot; x=&quot;200&quot; y=&quot;74&quot; width=&quot;130&quot; height=&quot;44&quot; /&gt;
&lt;text class=&quot;ott&quot; x=&quot;265&quot; y=&quot;92&quot; text-anchor=&quot;middle&quot;&gt;transcript_text&lt;/text&gt;
&lt;text class=&quot;ot&quot; x=&quot;265&quot; y=&quot;109&quot; text-anchor=&quot;middle&quot;&gt;20 B pointer&lt;/text&gt;
&lt;line class=&quot;edge&quot; x1=&quot;150&quot; y1=&quot;118&quot; x2=&quot;150&quot; y2=&quot;154&quot; marker-end=&quot;url(#op-ahm)&quot; /&gt;
&lt;line class=&quot;edge&quot; x1=&quot;265&quot; y1=&quot;118&quot; x2=&quot;265&quot; y2=&quot;154&quot; marker-end=&quot;url(#op-ahm)&quot; /&gt;
&lt;rect class=&quot;ovf&quot; x=&quot;100&quot; y=&quot;156&quot; width=&quot;100&quot; height=&quot;26&quot; rx=&quot;4&quot; /&gt;
&lt;text class=&quot;ot&quot; x=&quot;150&quot; y=&quot;173&quot; text-anchor=&quot;middle&quot;&gt;overflow 16 KB&lt;/text&gt;
&lt;line class=&quot;edge&quot; x1=&quot;150&quot; y1=&quot;182&quot; x2=&quot;150&quot; y2=&quot;192&quot; marker-end=&quot;url(#op-ahm)&quot; /&gt;
&lt;rect class=&quot;ovf&quot; x=&quot;100&quot; y=&quot;194&quot; width=&quot;100&quot; height=&quot;26&quot; rx=&quot;4&quot; /&gt;
&lt;text class=&quot;ot&quot; x=&quot;150&quot; y=&quot;211&quot; text-anchor=&quot;middle&quot;&gt;overflow 16 KB&lt;/text&gt;
&lt;line class=&quot;edge&quot; x1=&quot;150&quot; y1=&quot;220&quot; x2=&quot;150&quot; y2=&quot;230&quot; marker-end=&quot;url(#op-ahm)&quot; /&gt;
&lt;rect class=&quot;ovf&quot; x=&quot;100&quot; y=&quot;232&quot; width=&quot;100&quot; height=&quot;26&quot; rx=&quot;4&quot; stroke-dasharray=&quot;4 3&quot; /&gt;
&lt;text class=&quot;ot&quot; x=&quot;150&quot; y=&quot;249&quot; text-anchor=&quot;middle&quot;&gt;… 16 more&lt;/text&gt;
&lt;text class=&quot;ott&quot; x=&quot;150&quot; y=&quot;280&quot; text-anchor=&quot;middle&quot; style=&quot;font-size:12px&quot;&gt;~290 KB ≈ 18 pages&lt;/text&gt;
&lt;rect class=&quot;ovf&quot; x=&quot;215&quot; y=&quot;156&quot; width=&quot;100&quot; height=&quot;26&quot; rx=&quot;4&quot; /&gt;
&lt;text class=&quot;ot&quot; x=&quot;265&quot; y=&quot;173&quot; text-anchor=&quot;middle&quot;&gt;overflow 16 KB&lt;/text&gt;
&lt;line class=&quot;edge&quot; x1=&quot;265&quot; y1=&quot;182&quot; x2=&quot;265&quot; y2=&quot;192&quot; marker-end=&quot;url(#op-ahm)&quot; /&gt;
&lt;rect class=&quot;ovf&quot; x=&quot;215&quot; y=&quot;194&quot; width=&quot;100&quot; height=&quot;26&quot; rx=&quot;4&quot; /&gt;
&lt;text class=&quot;ot&quot; x=&quot;265&quot; y=&quot;211&quot; text-anchor=&quot;middle&quot;&gt;overflow 16 KB&lt;/text&gt;
&lt;line class=&quot;edge&quot; x1=&quot;265&quot; y1=&quot;220&quot; x2=&quot;265&quot; y2=&quot;230&quot; marker-end=&quot;url(#op-ahm)&quot; /&gt;
&lt;rect class=&quot;ovf&quot; x=&quot;215&quot; y=&quot;232&quot; width=&quot;100&quot; height=&quot;26&quot; rx=&quot;4&quot; /&gt;
&lt;text class=&quot;ot&quot; x=&quot;265&quot; y=&quot;249&quot; text-anchor=&quot;middle&quot;&gt;overflow 16 KB&lt;/text&gt;
&lt;text class=&quot;ott&quot; x=&quot;265&quot; y=&quot;280&quot; text-anchor=&quot;middle&quot; style=&quot;font-size:12px&quot;&gt;~36 KB ≈ 3 pages&lt;/text&gt;
&lt;/svg&gt;
&lt;figcaption style=&quot;color:var(--muted);font-size:.85em;text-align:center;margin-top:.7em&quot;&gt;Everything up to the summary sits in the row. Each transcript leaves a 20-byte pointer behind and lives in its own chain of overflow pages.&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Now let’s take a look at two queries that list calls and how MySQL executes them, 
especially how it handles the blobs.&lt;/p&gt;

&lt;h2 id=&quot;newest-first-a-sort-the-index-serves&quot;&gt;Newest first: a sort the index serves&lt;/h2&gt;

&lt;p&gt;The default order is newest first (&lt;code class=&quot;highlighter-rouge&quot;&gt;id DESC&lt;/code&gt;):&lt;/p&gt;

&lt;div class=&quot;language-sql highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;calls&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;WHERE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;account_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;AND&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;deleted_at&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;IS&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NULL&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;AND&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;status&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;completed&apos;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;ORDER&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;BY&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;DESC&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;LIMIT&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;InnoDB appends the primary key to every secondary index, so
&lt;code class=&quot;highlighter-rouge&quot;&gt;index_calls_on_account_id&lt;/code&gt; is physically &lt;code class=&quot;highlighter-rouge&quot;&gt;(account_id, id)&lt;/code&gt;. The entries come
out already ordered by id and MySQL walks them backwards without a filesort. But
&lt;code class=&quot;highlighter-rouge&quot;&gt;status&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;deleted_at&lt;/code&gt; aren’t in the index, so for every entry it has to fetch
the row from the clustered index to check them.&lt;/p&gt;

&lt;p&gt;Each call goes through the whole chain before the next one starts:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;the next entry from the index&lt;/li&gt;
  &lt;li&gt;its row from the clustered index&lt;/li&gt;
  &lt;li&gt;its overflow pages&lt;/li&gt;
  &lt;li&gt;the filter on &lt;code class=&quot;highlighter-rouge&quot;&gt;status&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;deleted_at&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;the limit: stop at 30, otherwise back to 1&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Seven in eight calls are completed, so the first page stops after 35 rows:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;-&amp;gt; Limit: 30 row(s)
     (actual time=0.236..3.57 rows=30)
  -&amp;gt; Filter: (status = &apos;completed&apos; and deleted_at is null)
       (actual time=0.235..3.56 rows=30)
    -&amp;gt; Index lookup on calls using index_calls_on_account_id
       (account_id=1) (reverse)
         (actual time=0.159..3.49 rows=35)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;SELECT *&lt;/code&gt; takes about 4 ms. The blobs are read, but only for 35 rows.&lt;/p&gt;

&lt;p&gt;This was the happy path, though. The execution time depends on the distribution of
the rows. In a copy of the table where only the two calls with the lowest ids are completed, the
same query walks all 15,000 calls to find them and takes 19 s instead of 4 ms.
That is what made this case confusing for me at first: the same query was fast
for some tenants and slow for others.&lt;/p&gt;

&lt;h2 id=&quot;by-start-time-a-sort-the-index-cant-serve&quot;&gt;By start time: a sort the index can’t serve&lt;/h2&gt;

&lt;p&gt;Now let’s sort the same list by &lt;code class=&quot;highlighter-rouge&quot;&gt;started_at&lt;/code&gt;. It isn’t in any index, so MySQL
has to visit every candidate, sort them, and only then take the first 30:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;index lookup on the account: 15,000 rows, each read with every selected
column, blobs (unfortunately) included&lt;/li&gt;
  &lt;li&gt;filter on &lt;code class=&quot;highlighter-rouge&quot;&gt;deleted_at&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;status&lt;/code&gt;: 13,125 rows&lt;/li&gt;
  &lt;li&gt;construct &lt;code class=&quot;highlighter-rouge&quot;&gt;(id, started_at)&lt;/code&gt; pairs and sort them by &lt;code class=&quot;highlighter-rouge&quot;&gt;started_at&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;limit: 30 pairs&lt;/li&gt;
  &lt;li&gt;read the 30 winners (all selected columns) again by id from the clustered index&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The plan for &lt;code class=&quot;highlighter-rouge&quot;&gt;SELECT *&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;-&amp;gt; Limit: 30 row(s)
     (actual time=17141..17178 rows=30)
  -&amp;gt; Sort row IDs: calls.started_at DESC, limit input to 30 row(s) per chunk
       (actual time=17141..17178 rows=30)
    -&amp;gt; Filter: (status = &apos;completed&apos; and deleted_at is null)
         (actual time=7.81..17129 rows=13125)
      -&amp;gt; Index lookup on calls using index_calls_on_account_id
         (account_id=1)
           (actual time=7.8..17111 rows=15000)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;MySQL is smart enough not to carry whole rows through the sort, only the
&lt;code class=&quot;highlighter-rouge&quot;&gt;(id, started_at)&lt;/code&gt; pairs. That doesn’t save the query, though. Almost all of the
17 seconds go to the index lookup, where InnoDB reads the blobs of all 15,000
candidates, just for the query to return 30 of them.&lt;/p&gt;

&lt;p&gt;This looked like a bug to me, since the sort only needs the WHERE and ORDER BY
columns. The 30 winners are read again afterwards anyway, so there’s no
reason to read every blob before it. It turns out I wasn’t the first to notice;
&lt;a href=&quot;https://bugs.mysql.com/bug.php?id=110998&quot;&gt;#110998&lt;/a&gt; traces it to a regression in
MySQL 8.0.20 and &lt;a href=&quot;https://bugs.mysql.com/bug.php?id=115555&quot;&gt;#115555&lt;/a&gt; is
verified on 8.0 and 8.4.&lt;/p&gt;

&lt;h2 id=&quot;which-columns-get-read&quot;&gt;Which columns get read&lt;/h2&gt;

&lt;p&gt;Above I said the sort has no reason to read every blob, that MySQL could be
smarter about which columns it reads. That set of columns is called the &lt;code class=&quot;highlighter-rouge&quot;&gt;read_set&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Before execution, the server builds the list of columns InnoDB reads for every
row it visits. The &lt;code class=&quot;highlighter-rouge&quot;&gt;read_set&lt;/code&gt; includes not just the SELECT list, but also
whatever is needed for filtering and sorting. For example, let’s take a look at
the following query:&lt;/p&gt;

&lt;div class=&quot;language-sql highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;started_at&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;calls&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;WHERE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;account_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;AND&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;deleted_at&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;IS&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NULL&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;AND&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;status&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;completed&apos;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;ORDER&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;BY&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;started_at&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;DESC&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;LIMIT&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It returns three columns but reads six, &lt;code class=&quot;highlighter-rouge&quot;&gt;id&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;title&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;started_at&lt;/code&gt; from the
SELECT, plus &lt;code class=&quot;highlighter-rouge&quot;&gt;account_id&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;deleted_at&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;status&lt;/code&gt; from the WHERE. All six sit on
the row page, and the query takes 31 ms. Make it &lt;code class=&quot;highlighter-rouge&quot;&gt;SELECT *&lt;/code&gt; and the &lt;code class=&quot;highlighter-rouge&quot;&gt;read_set&lt;/code&gt;
also picks up &lt;code class=&quot;highlighter-rouge&quot;&gt;transcript&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;transcript_text&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;summary&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;notes&lt;/code&gt;,
roughly 330 KB for every row InnoDB visits.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;read_set&lt;/code&gt; also explained something I noticed in production. The pagination
runs a &lt;code class=&quot;highlighter-rouge&quot;&gt;COUNT(*)&lt;/code&gt; with the same WHERE before the main query and
it was much faster than the list itself, even though both walk the same rows.
Here it takes 24 ms against the 17 s of the &lt;code class=&quot;highlighter-rouge&quot;&gt;SELECT *&lt;/code&gt;, because its &lt;code class=&quot;highlighter-rouge&quot;&gt;read_set&lt;/code&gt;
holds only the three WHERE columns, all on the row page.&lt;/p&gt;

&lt;h2 id=&quot;the-fix-at-the-index&quot;&gt;The fix at the index&lt;/h2&gt;

&lt;p&gt;The blobs hurt because MySQL reads them for rows it then throws away. If an index
answers the whole WHERE and hands the entries over already in the requested
order, there’s nothing to throw away.&lt;/p&gt;

&lt;p&gt;For the first query, that’s an index on the WHERE columns:&lt;/p&gt;

&lt;div class=&quot;language-sql highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;KEY&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;account_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;deleted_at&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To the index, &lt;code class=&quot;highlighter-rouge&quot;&gt;IS NULL&lt;/code&gt; is just another equality, so after three equalities
the entries come out already in &lt;code class=&quot;highlighter-rouge&quot;&gt;id&lt;/code&gt; order. On the copy where only two calls
are completed, the query now reads those two rows instead of 15,000 and takes
0.07 ms instead of 19 s.&lt;/p&gt;

&lt;p&gt;For the second query, the sort column goes at the end:&lt;/p&gt;

&lt;div class=&quot;language-sql highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;KEY&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;account_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;deleted_at&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;started_at&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;After the same three equalities, the entries come out in &lt;code class=&quot;highlighter-rouge&quot;&gt;started_at&lt;/code&gt; order.
There’s no sort and MySQL reads 30 rows. Even a &lt;code class=&quot;highlighter-rouge&quot;&gt;SELECT *&lt;/code&gt; then pays only for the
blobs of the rows it returns and runs in a mere 1.9 ms instead of 17 s.&lt;/p&gt;

&lt;p&gt;The problem is that an index serves one query shape. These two queries already
need two different indexes. Every other filter or sort on the list would need one
of its own. The queries here are also a simplification: a real list usually
carries more conditions (permissions for example) that no index can fully
answer, so the blobs get read for discarded rows again.&lt;/p&gt;

&lt;p&gt;And even when the query itself takes a couple of milliseconds, the rows it
returns still have to get to the application. With &lt;code class=&quot;highlighter-rouge&quot;&gt;SELECT *&lt;/code&gt;, the 30 calls are
about 10 MB that MySQL sends over the network and Rails then parses into
memory.&lt;/p&gt;

&lt;h2 id=&quot;deferring-the-join&quot;&gt;Deferring the join&lt;/h2&gt;

&lt;p&gt;When no index serves the query, we can still keep the blobs out of the part that
visits every row. We split the work in two: a narrow query finds the ids of the
page, and only then are the full rows read (for those same ids).&lt;/p&gt;

&lt;div class=&quot;language-sql highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;calls&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;calls&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;JOIN&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;calls&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;WHERE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;account_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;AND&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;deleted_at&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;IS&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NULL&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;AND&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;status&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;completed&apos;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;ORDER&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;BY&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;started_at&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;DESC&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;LIMIT&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;page&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;USING&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;ORDER&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;BY&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;calls&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;started_at&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;DESC&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is called a deferred join. The inner query visits the same 15,000
rows and sorts them, but with the narrow &lt;code class=&quot;highlighter-rouge&quot;&gt;read_set&lt;/code&gt;. The outer one reads
the blobs of only the 30 rows that made the page. The result is the
same as the &lt;code class=&quot;highlighter-rouge&quot;&gt;SELECT *&lt;/code&gt;, and it takes 31 ms instead of 17 s.&lt;/p&gt;

&lt;p&gt;The problem for me here was the fact that it wasn’t easy to implement on the
application layer. The inner query still has to carry every filter, permission
check and sort from the request.&lt;/p&gt;

&lt;h2 id=&quot;the-fix-at-the-query&quot;&gt;The fix at the query&lt;/h2&gt;

&lt;p&gt;But why complicate things? If the list doesn’t show the blobs, we can just… not
select them. With &lt;code class=&quot;highlighter-rouge&quot;&gt;.select&lt;/code&gt;, the heavy columns never enter the &lt;code class=&quot;highlighter-rouge&quot;&gt;read_set&lt;/code&gt;. All
we need is for the SELECT to follow the sparse fieldset:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# Every attribute that reads a heavy column names the columns it reads.&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# An attribute can read a column under a different name, or several of them.&lt;/span&gt;
&lt;span class=&quot;no&quot;&gt;HEAVY_COLUMNS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;ss&quot;&gt;transcript:      &lt;/span&gt;&lt;span class=&quot;sx&quot;&gt;%w[transcript]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;ss&quot;&gt;transcript_text: &lt;/span&gt;&lt;span class=&quot;sx&quot;&gt;%w[transcript_text]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;ss&quot;&gt;highlights:      &lt;/span&gt;&lt;span class=&quot;sx&quot;&gt;%w[transcript transcript_text]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;freeze&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;selected_columns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fields&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# No fields[calls] in the request, nothing changes.&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;column_names&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fields&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;nil?&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# A heavy column stays as long as any requested attribute reads it,&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# the rest drop out of the SELECT.&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;heavy&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;HEAVY_COLUMNS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;flatten&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;needed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;HEAVY_COLUMNS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;values_at&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fields&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;compact&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;flatten&lt;/span&gt;
  &lt;span class=&quot;no&quot;&gt;Call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;column_names&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;heavy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;needed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;fields&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;dig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:fields&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:calls&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;split&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;,&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:to_sym&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;no&quot;&gt;Call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;where&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;selected_columns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fields&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The plan doesn’t change and neither does the number of rows visited. Each visit
just gets as cheap as in the narrow query. Sorted by start time, that’s 17 s
against 31 ms.&lt;/p&gt;

&lt;p&gt;The catch is that every call it loads is now a partial model. If a callback, a
cache key or a serializer helper reads one of the dropped columns, Rails raises
&lt;code class=&quot;highlighter-rouge&quot;&gt;ActiveModel::MissingAttributeError&lt;/code&gt;. On top of that,
&lt;code class=&quot;highlighter-rouge&quot;&gt;HEAVY_COLUMNS&lt;/code&gt; has to be kept in sync with the serializer by hand.&lt;/p&gt;

&lt;p&gt;And &lt;code class=&quot;highlighter-rouge&quot;&gt;.select&lt;/code&gt; only fixes the query it’s on. Calls get loaded in other places
too: a preload through &lt;code class=&quot;highlighter-rouge&quot;&gt;includes(:calls)&lt;/code&gt; on another model, a &lt;code class=&quot;highlighter-rouge&quot;&gt;find&lt;/code&gt;, a
background job, or an update, which loads the whole record before it writes a
single column. Each of those still runs &lt;code class=&quot;highlighter-rouge&quot;&gt;SELECT *&lt;/code&gt; and still reads the blobs,
unless someone remembers to add a &lt;code class=&quot;highlighter-rouge&quot;&gt;select&lt;/code&gt; there as well.&lt;/p&gt;

&lt;h2 id=&quot;splitting-the-column-out&quot;&gt;Splitting the column out&lt;/h2&gt;

&lt;p&gt;The durable fix is to move the heavy columns out of the table, either into
something like S3 or into their own table, one row for each call:&lt;/p&gt;

&lt;div class=&quot;language-sql highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;CREATE&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;TABLE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;calls&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;         &lt;span class=&quot;nb&quot;&gt;BIGINT&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AUTO_INCREMENT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;PRIMARY&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;KEY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;account_id&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;BIGINT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NOT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;      &lt;span class=&quot;nb&quot;&gt;VARCHAR&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;255&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NOT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;status&lt;/span&gt;     &lt;span class=&quot;nb&quot;&gt;VARCHAR&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NOT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;started_at&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;DATETIME&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NOT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;deleted_at&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;DATETIME&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;KEY&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index_calls_on_account_id&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;account_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;CREATE&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;TABLE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;call_contents&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;call_id&lt;/span&gt;         &lt;span class=&quot;nb&quot;&gt;BIGINT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;PRIMARY&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;KEY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;summary&lt;/span&gt;         &lt;span class=&quot;nb&quot;&gt;MEDIUMTEXT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;notes&lt;/span&gt;           &lt;span class=&quot;nb&quot;&gt;MEDIUMTEXT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;transcript&lt;/span&gt;      &lt;span class=&quot;n&quot;&gt;JSON&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;transcript_text&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;MEDIUMTEXT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;FOREIGN&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;KEY&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;call_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;REFERENCES&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;calls&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is the option I like best. Nobody filters or sorts by a transcript, so a
list never needs the join. On the seed, the same &lt;code class=&quot;highlighter-rouge&quot;&gt;SELECT *&lt;/code&gt; sorted by start time
goes from 17 s to 11 ms. That’s even faster than the narrow query on the
original table (31 ms), because the 2 KB summary moved off the row page too.&lt;/p&gt;

&lt;p&gt;Once the blobs aren’t in the calls table, nothing that loads calls can read them
by accident, whether it’s the list, a preload or a background job. There’s no
column list to maintain, no partial model and no index to match to the query.&lt;/p&gt;

&lt;p&gt;The cost is a data migration on a big table, and a join or a second query
wherever the content is needed.&lt;/p&gt;

&lt;p&gt;If a migration is what holds you back, Rails can get you most of the way there without
it. &lt;code class=&quot;highlighter-rouge&quot;&gt;ignored_columns&lt;/code&gt; keeps the heavy columns out of every query &lt;code class=&quot;highlighter-rouge&quot;&gt;Call&lt;/code&gt; makes
and a second model on the same table reads them when they’re needed:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Call&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;ignored_columns&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;sx&quot;&gt;%w[summary notes transcript transcript_text]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;has_one&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;class_name: &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;CallContent&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;foreign_key: :id&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CallContent&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;table_name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;calls&quot;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;A list, a preload or an update no longer selects the blobs. Code that needs
them asks for &lt;code class=&quot;highlighter-rouge&quot;&gt;call.content&lt;/code&gt; explicitly. There’s still a catch, though.
With the default &lt;code class=&quot;highlighter-rouge&quot;&gt;binlog_row_image=FULL&lt;/code&gt;, every &lt;code class=&quot;highlighter-rouge&quot;&gt;UPDATE&lt;/code&gt; on a call
writes the whole row to the binlog, before and after (blobs included). A status
change or a bumped &lt;code class=&quot;highlighter-rouge&quot;&gt;updated_at&lt;/code&gt; is about 660 KB of binlog every time and every
replica has to replay it. A backfill of a new column multiplies that by every row
in the table.&lt;/p&gt;

&lt;p&gt;Here are all the numbers side by side:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;query&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;rows visited&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;time&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;newest first, &lt;code class=&quot;highlighter-rouge&quot;&gt;SELECT *&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;35&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;4 ms&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;newest first, two completed calls, &lt;code class=&quot;highlighter-rouge&quot;&gt;SELECT *&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;15,000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;19 s&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;newest first, two completed calls, &lt;code class=&quot;highlighter-rouge&quot;&gt;SELECT *&lt;/code&gt; with the WHERE index&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;2&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;0.07 ms&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;by start time, &lt;code class=&quot;highlighter-rouge&quot;&gt;SELECT *&lt;/code&gt;&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;15,000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;17 s&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;by start time, narrow&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;15,000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;31 ms&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;by start time, &lt;code class=&quot;highlighter-rouge&quot;&gt;SELECT *&lt;/code&gt; as a deferred join&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;15,000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;31 ms&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;by start time, &lt;code class=&quot;highlighter-rouge&quot;&gt;SELECT *&lt;/code&gt; with the new index&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;30&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;1.9 ms&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;by start time, &lt;code class=&quot;highlighter-rouge&quot;&gt;SELECT *&lt;/code&gt; on the split table&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;15,000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;11 ms&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h2 id=&quot;takeaway&quot;&gt;Takeaway&lt;/h2&gt;

&lt;p&gt;Although it wasn’t the main point of this post, I think another really important
lesson here is the difference between rows read and rows returned. The list returned 30 rows and
read up to 15,000, each with every selected column, heavy ones included. A
sparse fieldset doesn’t necessarily change that because, on its own, it only trims
the response.&lt;/p&gt;

&lt;p&gt;So when a list endpoint is slow and its plan looks fine, run a &lt;code class=&quot;highlighter-rouge&quot;&gt;COUNT(*)&lt;/code&gt; with
the same WHERE. If the count is fast and the &lt;code class=&quot;highlighter-rouge&quot;&gt;SELECT *&lt;/code&gt; isn’t, the time goes to
columns nobody asked for, usually a &lt;code class=&quot;highlighter-rouge&quot;&gt;TEXT&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;BLOB&lt;/code&gt; or &lt;code class=&quot;highlighter-rouge&quot;&gt;JSON&lt;/code&gt;. Selecting fewer
columns or adding the right index fixes that one query, but moving the heavy
columns into their own table should fix it for every query on the table.&lt;/p&gt;</content><author><name></name></author></entry><entry><title type="html">MySQL can’t merge sorted index ranges (and the UNION ALL fix)</title><link href="https://filipsucic.com/posts/mysql-cant-merge-sorted-index-ranges/" rel="alternate" type="text/html" title="MySQL can&apos;t merge sorted index ranges (and the UNION ALL fix)" /><published>2026-07-23T00:00:00+00:00</published><updated>2026-07-23T00:00:00+00:00</updated><id>repo://posts.collection/_posts/2026-07-23-mysql-cant-merge-sorted-index-ranges.md</id><content type="html" xml:base="https://filipsucic.com/posts/mysql-cant-merge-sorted-index-ranges/">&lt;p&gt;A filter on an indexed column with an &lt;code class=&quot;highlighter-rouge&quot;&gt;ORDER BY&lt;/code&gt; and a small &lt;code class=&quot;highlighter-rouge&quot;&gt;LIMIT&lt;/code&gt; is about as
common as queries get. With a single value in the filter it’s fast, since MySQL walks
the index and stops as soon as it has enough rows. When you add a second value to the
filter, the same query can get orders of magnitude slower, scanning the whole
table to return 200 rows.&lt;/p&gt;

&lt;p&gt;That’s what happened to me recently. The &lt;code class=&quot;highlighter-rouge&quot;&gt;EXPLAIN ANALYZE&lt;/code&gt; output is what finally
made it click. I want to walk through it one plan at a time, the way I got there,
instead of just dropping the final query on you up front.&lt;/p&gt;

&lt;h2 id=&quot;the-setup&quot;&gt;The setup&lt;/h2&gt;

&lt;p&gt;I’ll demonstrate the problem and the fix using an &lt;code class=&quot;highlighter-rouge&quot;&gt;activities&lt;/code&gt; table, an append-only
log. Each row records which tenant it belongs to (&lt;code class=&quot;highlighter-rouge&quot;&gt;account_id&lt;/code&gt;) and which user produced
it (&lt;code class=&quot;highlighter-rouge&quot;&gt;creator_id&lt;/code&gt;), and one index, &lt;code class=&quot;highlighter-rouge&quot;&gt;idx_account_creator&lt;/code&gt;, covers both. Every plan
below comes from MySQL 8.4.&lt;/p&gt;

&lt;p&gt;The seed puts two users near the start of the log, then buries them under two
million newer rows from a third user, so the two we later query sit far from
the most recent &lt;code class=&quot;highlighter-rouge&quot;&gt;id&lt;/code&gt; values.&lt;/p&gt;

&lt;details&gt;
  &lt;summary&gt;Full schema and seed&lt;/summary&gt;

  &lt;div class=&quot;language-sql highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;CREATE&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;TABLE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;activities&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;         &lt;span class=&quot;nb&quot;&gt;BIGINT&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;AUTO_INCREMENT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;PRIMARY&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;KEY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;account_id&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;BIGINT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NOT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;creator_id&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;BIGINT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NOT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;payload&lt;/span&gt;    &lt;span class=&quot;nb&quot;&gt;VARCHAR&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;64&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NOT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;NULL&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;DEFAULT&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;c1&quot;&gt;-- stand-in for the row&apos;s contents&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;KEY&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;idx_account_creator&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;account_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;creator_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ENGINE&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;InnoDB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;DELIMITER&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;$$&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;CREATE&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;PROCEDURE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seed_activities&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;BEGIN&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;DECLARE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;INT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;DEFAULT&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;DECLARE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;base_max_id&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;BIGINT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;START&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TRANSACTION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;WHILE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;125000&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;DO&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;INSERT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;INTO&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;activities&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;account_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;creator_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;VALUES&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;SET&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;END&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WHILE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;COMMIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;MAX&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;INTO&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;base_max_id&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;activities&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;SET&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;WHILE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;DO&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;INSERT&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;INTO&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;activities&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;account_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;creator_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;activities&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;WHERE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;base_max_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;SET&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;END&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;WHILE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;END&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;$$&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;DELIMITER&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;CALL&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;seed_activities&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;

&lt;/details&gt;

&lt;h2 id=&quot;the-query&quot;&gt;The query&lt;/h2&gt;

&lt;p&gt;The query we care about lists a tenant’s most recent activity, narrowed to a few
users:&lt;/p&gt;

&lt;div class=&quot;language-sql highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;activities&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;WHERE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;account_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;AND&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;creator_id&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;IN&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;ORDER&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;BY&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;DESC&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;LIMIT&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;200&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Naturally, we’ll focus on the &lt;code class=&quot;highlighter-rouge&quot;&gt;idx_account_creator&lt;/code&gt; index. Because InnoDB
implicitly appends the primary key to every secondary index, this index is physically
&lt;code class=&quot;highlighter-rouge&quot;&gt;(account_id, creator_id, id)&lt;/code&gt;. That means, within each &lt;code class=&quot;highlighter-rouge&quot;&gt;(account_id, creator_id)&lt;/code&gt; group,
rows are already ordered by &lt;code class=&quot;highlighter-rouge&quot;&gt;id&lt;/code&gt;. Because of that, we could expect MySQL to walk it backward
and stop after 200 rows. We’ll first show it does exactly that for a single user.
Then, we will try two users and see what happens (hint: it’s not great).&lt;/p&gt;

&lt;h2 id=&quot;what-mysql-actually-does&quot;&gt;What MySQL actually does&lt;/h2&gt;

&lt;h3 id=&quot;single-value&quot;&gt;Single value&lt;/h3&gt;

&lt;p&gt;Let’s first narrow the query to a single user:&lt;/p&gt;

&lt;div class=&quot;language-sql highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;activities&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;WHERE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;account_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;AND&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;creator_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;ORDER&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;BY&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;DESC&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;LIMIT&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;200&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;For &lt;code class=&quot;highlighter-rouge&quot;&gt;creator_id = 1&lt;/code&gt; the index holds one contiguous run of rows for
&lt;code class=&quot;highlighter-rouge&quot;&gt;(account_id, creator_id) = (1, 1)&lt;/code&gt;, already ordered by &lt;code class=&quot;highlighter-rouge&quot;&gt;id&lt;/code&gt;. Because
of that, MySQL starts at its high end, reads 200 rows and returns them.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;-&amp;gt; Limit: 200 row(s)
     (actual time=0.305..0.321 rows=200)
  -&amp;gt; Index lookup on activities using idx_account_creator
     (account_id=1, creator_id=1) (reverse)
       (rows=128788) (actual time=0.305..0.315 rows=200)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can see the index does both jobs at once. It finds the matching rows
and hands them back already ordered, so there’s no sort step. This is a
well-known optimization, &lt;a href=&quot;https://dev.mysql.com/doc/refman/8.4/en/order-by-optimization.html&quot;&gt;using an index for ORDER BY&lt;/a&gt;,
and exactly the plan we were hoping for.&lt;/p&gt;

&lt;h3 id=&quot;multiple-values&quot;&gt;Multiple values&lt;/h3&gt;

&lt;p&gt;With two values the execution plan changes. The index gives us two runs, one per
user, each ordered by &lt;code class=&quot;highlighter-rouge&quot;&gt;id&lt;/code&gt; on its own. The thing is, their rows are mixed across
the &lt;code class=&quot;highlighter-rouge&quot;&gt;id&lt;/code&gt; range. Reading just one run won’t give us the newest rows overall.
To list the newest 200 across both, we have to combine the two runs. I was kind of
hoping that MySQL would do that on its own.&lt;/p&gt;

&lt;p&gt;But, before turning to what MySQL does, let’s first consider what an ideal execution would look
like. We have two sorted sequences and we want the 200 largest &lt;code class=&quot;highlighter-rouge&quot;&gt;id&lt;/code&gt;s across both.&lt;/p&gt;

&lt;p&gt;The classic answer is a two-pointer merge, the same step merge sort uses to combine two sorted halves.
We keep a cursor at the high end of each run, repeatedly take the larger of the two values and stop after 200 of them.
The cost is at most 201 reads and 200 comparisons, regardless of how large the runs
are, the one extra read being the head of the other run, which loses the last comparison.&lt;/p&gt;

&lt;div class=&quot;plan-carousel&quot; data-legend=&quot;step&quot; data-labels=&quot;start|row 1|row 2|row 3|row 4|row 5&quot;&gt;
&lt;figure class=&quot;carousel-slide&quot;&gt;&lt;svg viewBox=&quot;0 0 640 222&quot; role=&quot;img&quot; aria-label=&quot;Step 1: Each run is already sorted by id, high end first. A cursor starts at the top of each one: 98 and 95. To keep the example small, the limit is 5 instead of 200.&quot; style=&quot;font:12px system-ui,sans-serif;white-space:pre&quot;&gt;&lt;text x=&quot;18&quot; y=&quot;40&quot; style=&quot;fill:var(--muted)&quot;&gt;(account_id, creator_id) = (1, 1)&lt;/text&gt;&lt;rect x=&quot;248&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--inline-bg);stroke:var(--muted);stroke-width:1&quot; /&gt;&lt;text x=&quot;267&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--text)&quot;&gt;98&lt;/text&gt;&lt;rect x=&quot;292&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;311&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;91&lt;/text&gt;&lt;rect x=&quot;336&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;355&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;77&lt;/text&gt;&lt;rect x=&quot;380&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;399&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;64&lt;/text&gt;&lt;rect x=&quot;424&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;443&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;52&lt;/text&gt;&lt;rect x=&quot;468&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;487&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;40&lt;/text&gt;&lt;rect x=&quot;512&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;531&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;31&lt;/text&gt;&lt;rect x=&quot;556&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;575&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;17&lt;/text&gt;&lt;path d=&quot;M267 54 l-6 9 h12 z&quot; style=&quot;fill:var(--link)&quot; /&gt;&lt;text x=&quot;18&quot; y=&quot;102&quot; style=&quot;fill:var(--muted)&quot;&gt;(account_id, creator_id) = (1, 2)&lt;/text&gt;&lt;rect x=&quot;248&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--inline-bg);stroke:var(--muted);stroke-width:1&quot; /&gt;&lt;text x=&quot;267&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--text)&quot;&gt;95&lt;/text&gt;&lt;rect x=&quot;292&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;311&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;83&lt;/text&gt;&lt;rect x=&quot;336&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;355&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;80&lt;/text&gt;&lt;rect x=&quot;380&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;399&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;59&lt;/text&gt;&lt;rect x=&quot;424&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;443&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;45&lt;/text&gt;&lt;rect x=&quot;468&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;487&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;22&lt;/text&gt;&lt;rect x=&quot;512&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;531&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;12&lt;/text&gt;&lt;path d=&quot;M267 116 l-6 9 h12 z&quot; style=&quot;fill:var(--link)&quot; /&gt;&lt;text x=&quot;18&quot; y=&quot;170&quot; style=&quot;fill:var(--muted)&quot;&gt;result (ORDER BY id DESC)&lt;/text&gt;&lt;rect x=&quot;248&quot; y=&quot;150&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:none;stroke:var(--rule);stroke-dasharray:3 3&quot; /&gt;&lt;rect x=&quot;292&quot; y=&quot;150&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:none;stroke:var(--rule);stroke-dasharray:3 3&quot; /&gt;&lt;rect x=&quot;336&quot; y=&quot;150&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:none;stroke:var(--rule);stroke-dasharray:3 3&quot; /&gt;&lt;rect x=&quot;380&quot; y=&quot;150&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:none;stroke:var(--rule);stroke-dasharray:3 3&quot; /&gt;&lt;rect x=&quot;424&quot; y=&quot;150&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:none;stroke:var(--rule);stroke-dasharray:3 3&quot; /&gt;&lt;text x=&quot;18&quot; y=&quot;212&quot; style=&quot;fill:var(--text)&quot;&gt;entries read: 2     rows returned: 0 of 5&lt;/text&gt;&lt;/svg&gt;&lt;figcaption&gt;Each run is already sorted by id, high end first. A cursor starts at the top of each one: 98 and 95. To keep the example small, the limit is 5 instead of 200.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;figure class=&quot;carousel-slide&quot;&gt;&lt;svg viewBox=&quot;0 0 640 222&quot; role=&quot;img&quot; aria-label=&quot;Step 2: 98 is larger than 95, so 98 goes to the result. The cursor on that run moves down to 91.&quot; style=&quot;font:12px system-ui,sans-serif;white-space:pre&quot;&gt;&lt;text x=&quot;18&quot; y=&quot;40&quot; style=&quot;fill:var(--muted)&quot;&gt;(account_id, creator_id) = (1, 1)&lt;/text&gt;&lt;rect x=&quot;248&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--link);stroke:var(--link-hover);stroke-width:2&quot; /&gt;&lt;text x=&quot;267&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--bg)&quot;&gt;98&lt;/text&gt;&lt;rect x=&quot;292&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--inline-bg);stroke:var(--muted);stroke-width:1&quot; /&gt;&lt;text x=&quot;311&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--text)&quot;&gt;91&lt;/text&gt;&lt;rect x=&quot;336&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;355&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;77&lt;/text&gt;&lt;rect x=&quot;380&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;399&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;64&lt;/text&gt;&lt;rect x=&quot;424&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;443&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;52&lt;/text&gt;&lt;rect x=&quot;468&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;487&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;40&lt;/text&gt;&lt;rect x=&quot;512&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;531&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;31&lt;/text&gt;&lt;rect x=&quot;556&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;575&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;17&lt;/text&gt;&lt;path d=&quot;M311 54 l-6 9 h12 z&quot; style=&quot;fill:var(--link)&quot; /&gt;&lt;text x=&quot;18&quot; y=&quot;102&quot; style=&quot;fill:var(--muted)&quot;&gt;(account_id, creator_id) = (1, 2)&lt;/text&gt;&lt;rect x=&quot;248&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--inline-bg);stroke:var(--muted);stroke-width:1&quot; /&gt;&lt;text x=&quot;267&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--text)&quot;&gt;95&lt;/text&gt;&lt;rect x=&quot;292&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;311&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;83&lt;/text&gt;&lt;rect x=&quot;336&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;355&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;80&lt;/text&gt;&lt;rect x=&quot;380&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;399&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;59&lt;/text&gt;&lt;rect x=&quot;424&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;443&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;45&lt;/text&gt;&lt;rect x=&quot;468&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;487&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;22&lt;/text&gt;&lt;rect x=&quot;512&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;531&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;12&lt;/text&gt;&lt;path d=&quot;M267 116 l-6 9 h12 z&quot; style=&quot;fill:var(--link)&quot; /&gt;&lt;text x=&quot;18&quot; y=&quot;170&quot; style=&quot;fill:var(--muted)&quot;&gt;result (ORDER BY id DESC)&lt;/text&gt;&lt;rect x=&quot;248&quot; y=&quot;150&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--link);stroke:var(--link-hover);stroke-width:2&quot; /&gt;&lt;text x=&quot;267&quot; y=&quot;170&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--bg)&quot;&gt;98&lt;/text&gt;&lt;rect x=&quot;292&quot; y=&quot;150&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:none;stroke:var(--rule);stroke-dasharray:3 3&quot; /&gt;&lt;rect x=&quot;336&quot; y=&quot;150&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:none;stroke:var(--rule);stroke-dasharray:3 3&quot; /&gt;&lt;rect x=&quot;380&quot; y=&quot;150&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:none;stroke:var(--rule);stroke-dasharray:3 3&quot; /&gt;&lt;rect x=&quot;424&quot; y=&quot;150&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:none;stroke:var(--rule);stroke-dasharray:3 3&quot; /&gt;&lt;text x=&quot;18&quot; y=&quot;212&quot; style=&quot;fill:var(--text)&quot;&gt;entries read: 3     rows returned: 1 of 5&lt;/text&gt;&lt;/svg&gt;&lt;figcaption&gt;98 is larger than 95, so 98 goes to the result. The cursor on that run moves down to 91.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;figure class=&quot;carousel-slide&quot;&gt;&lt;svg viewBox=&quot;0 0 640 222&quot; role=&quot;img&quot; aria-label=&quot;Step 3: 95 is larger than 91, so 95 goes to the result. The cursor on that run moves down to 83.&quot; style=&quot;font:12px system-ui,sans-serif;white-space:pre&quot;&gt;&lt;text x=&quot;18&quot; y=&quot;40&quot; style=&quot;fill:var(--muted)&quot;&gt;(account_id, creator_id) = (1, 1)&lt;/text&gt;&lt;rect x=&quot;248&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--link);stroke:var(--link);stroke-width:1&quot; /&gt;&lt;text x=&quot;267&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--bg)&quot;&gt;98&lt;/text&gt;&lt;rect x=&quot;292&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--inline-bg);stroke:var(--muted);stroke-width:1&quot; /&gt;&lt;text x=&quot;311&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--text)&quot;&gt;91&lt;/text&gt;&lt;rect x=&quot;336&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;355&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;77&lt;/text&gt;&lt;rect x=&quot;380&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;399&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;64&lt;/text&gt;&lt;rect x=&quot;424&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;443&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;52&lt;/text&gt;&lt;rect x=&quot;468&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;487&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;40&lt;/text&gt;&lt;rect x=&quot;512&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;531&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;31&lt;/text&gt;&lt;rect x=&quot;556&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;575&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;17&lt;/text&gt;&lt;path d=&quot;M311 54 l-6 9 h12 z&quot; style=&quot;fill:var(--link)&quot; /&gt;&lt;text x=&quot;18&quot; y=&quot;102&quot; style=&quot;fill:var(--muted)&quot;&gt;(account_id, creator_id) = (1, 2)&lt;/text&gt;&lt;rect x=&quot;248&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--link);stroke:var(--link-hover);stroke-width:2&quot; /&gt;&lt;text x=&quot;267&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--bg)&quot;&gt;95&lt;/text&gt;&lt;rect x=&quot;292&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--inline-bg);stroke:var(--muted);stroke-width:1&quot; /&gt;&lt;text x=&quot;311&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--text)&quot;&gt;83&lt;/text&gt;&lt;rect x=&quot;336&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;355&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;80&lt;/text&gt;&lt;rect x=&quot;380&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;399&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;59&lt;/text&gt;&lt;rect x=&quot;424&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;443&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;45&lt;/text&gt;&lt;rect x=&quot;468&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;487&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;22&lt;/text&gt;&lt;rect x=&quot;512&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;531&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;12&lt;/text&gt;&lt;path d=&quot;M311 116 l-6 9 h12 z&quot; style=&quot;fill:var(--link)&quot; /&gt;&lt;text x=&quot;18&quot; y=&quot;170&quot; style=&quot;fill:var(--muted)&quot;&gt;result (ORDER BY id DESC)&lt;/text&gt;&lt;rect x=&quot;248&quot; y=&quot;150&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--link);stroke:var(--link);stroke-width:1&quot; /&gt;&lt;text x=&quot;267&quot; y=&quot;170&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--bg)&quot;&gt;98&lt;/text&gt;&lt;rect x=&quot;292&quot; y=&quot;150&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--link);stroke:var(--link-hover);stroke-width:2&quot; /&gt;&lt;text x=&quot;311&quot; y=&quot;170&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--bg)&quot;&gt;95&lt;/text&gt;&lt;rect x=&quot;336&quot; y=&quot;150&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:none;stroke:var(--rule);stroke-dasharray:3 3&quot; /&gt;&lt;rect x=&quot;380&quot; y=&quot;150&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:none;stroke:var(--rule);stroke-dasharray:3 3&quot; /&gt;&lt;rect x=&quot;424&quot; y=&quot;150&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:none;stroke:var(--rule);stroke-dasharray:3 3&quot; /&gt;&lt;text x=&quot;18&quot; y=&quot;212&quot; style=&quot;fill:var(--text)&quot;&gt;entries read: 4     rows returned: 2 of 5&lt;/text&gt;&lt;/svg&gt;&lt;figcaption&gt;95 is larger than 91, so 95 goes to the result. The cursor on that run moves down to 83.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;figure class=&quot;carousel-slide&quot;&gt;&lt;svg viewBox=&quot;0 0 640 222&quot; role=&quot;img&quot; aria-label=&quot;Step 4: 91 is larger than 83, so 91 goes to the result. The cursor on that run moves down to 77.&quot; style=&quot;font:12px system-ui,sans-serif;white-space:pre&quot;&gt;&lt;text x=&quot;18&quot; y=&quot;40&quot; style=&quot;fill:var(--muted)&quot;&gt;(account_id, creator_id) = (1, 1)&lt;/text&gt;&lt;rect x=&quot;248&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--link);stroke:var(--link);stroke-width:1&quot; /&gt;&lt;text x=&quot;267&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--bg)&quot;&gt;98&lt;/text&gt;&lt;rect x=&quot;292&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--link);stroke:var(--link-hover);stroke-width:2&quot; /&gt;&lt;text x=&quot;311&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--bg)&quot;&gt;91&lt;/text&gt;&lt;rect x=&quot;336&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--inline-bg);stroke:var(--muted);stroke-width:1&quot; /&gt;&lt;text x=&quot;355&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--text)&quot;&gt;77&lt;/text&gt;&lt;rect x=&quot;380&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;399&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;64&lt;/text&gt;&lt;rect x=&quot;424&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;443&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;52&lt;/text&gt;&lt;rect x=&quot;468&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;487&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;40&lt;/text&gt;&lt;rect x=&quot;512&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;531&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;31&lt;/text&gt;&lt;rect x=&quot;556&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;575&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;17&lt;/text&gt;&lt;path d=&quot;M355 54 l-6 9 h12 z&quot; style=&quot;fill:var(--link)&quot; /&gt;&lt;text x=&quot;18&quot; y=&quot;102&quot; style=&quot;fill:var(--muted)&quot;&gt;(account_id, creator_id) = (1, 2)&lt;/text&gt;&lt;rect x=&quot;248&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--link);stroke:var(--link);stroke-width:1&quot; /&gt;&lt;text x=&quot;267&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--bg)&quot;&gt;95&lt;/text&gt;&lt;rect x=&quot;292&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--inline-bg);stroke:var(--muted);stroke-width:1&quot; /&gt;&lt;text x=&quot;311&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--text)&quot;&gt;83&lt;/text&gt;&lt;rect x=&quot;336&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;355&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;80&lt;/text&gt;&lt;rect x=&quot;380&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;399&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;59&lt;/text&gt;&lt;rect x=&quot;424&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;443&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;45&lt;/text&gt;&lt;rect x=&quot;468&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;487&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;22&lt;/text&gt;&lt;rect x=&quot;512&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;531&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;12&lt;/text&gt;&lt;path d=&quot;M311 116 l-6 9 h12 z&quot; style=&quot;fill:var(--link)&quot; /&gt;&lt;text x=&quot;18&quot; y=&quot;170&quot; style=&quot;fill:var(--muted)&quot;&gt;result (ORDER BY id DESC)&lt;/text&gt;&lt;rect x=&quot;248&quot; y=&quot;150&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--link);stroke:var(--link);stroke-width:1&quot; /&gt;&lt;text x=&quot;267&quot; y=&quot;170&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--bg)&quot;&gt;98&lt;/text&gt;&lt;rect x=&quot;292&quot; y=&quot;150&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--link);stroke:var(--link);stroke-width:1&quot; /&gt;&lt;text x=&quot;311&quot; y=&quot;170&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--bg)&quot;&gt;95&lt;/text&gt;&lt;rect x=&quot;336&quot; y=&quot;150&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--link);stroke:var(--link-hover);stroke-width:2&quot; /&gt;&lt;text x=&quot;355&quot; y=&quot;170&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--bg)&quot;&gt;91&lt;/text&gt;&lt;rect x=&quot;380&quot; y=&quot;150&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:none;stroke:var(--rule);stroke-dasharray:3 3&quot; /&gt;&lt;rect x=&quot;424&quot; y=&quot;150&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:none;stroke:var(--rule);stroke-dasharray:3 3&quot; /&gt;&lt;text x=&quot;18&quot; y=&quot;212&quot; style=&quot;fill:var(--text)&quot;&gt;entries read: 5     rows returned: 3 of 5&lt;/text&gt;&lt;/svg&gt;&lt;figcaption&gt;91 is larger than 83, so 91 goes to the result. The cursor on that run moves down to 77.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;figure class=&quot;carousel-slide&quot;&gt;&lt;svg viewBox=&quot;0 0 640 222&quot; role=&quot;img&quot; aria-label=&quot;Step 5: 83 is larger than 77, so 83 goes to the result. The cursor on that run moves down to 80.&quot; style=&quot;font:12px system-ui,sans-serif;white-space:pre&quot;&gt;&lt;text x=&quot;18&quot; y=&quot;40&quot; style=&quot;fill:var(--muted)&quot;&gt;(account_id, creator_id) = (1, 1)&lt;/text&gt;&lt;rect x=&quot;248&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--link);stroke:var(--link);stroke-width:1&quot; /&gt;&lt;text x=&quot;267&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--bg)&quot;&gt;98&lt;/text&gt;&lt;rect x=&quot;292&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--link);stroke:var(--link);stroke-width:1&quot; /&gt;&lt;text x=&quot;311&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--bg)&quot;&gt;91&lt;/text&gt;&lt;rect x=&quot;336&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--inline-bg);stroke:var(--muted);stroke-width:1&quot; /&gt;&lt;text x=&quot;355&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--text)&quot;&gt;77&lt;/text&gt;&lt;rect x=&quot;380&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;399&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;64&lt;/text&gt;&lt;rect x=&quot;424&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;443&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;52&lt;/text&gt;&lt;rect x=&quot;468&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;487&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;40&lt;/text&gt;&lt;rect x=&quot;512&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;531&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;31&lt;/text&gt;&lt;rect x=&quot;556&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;575&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;17&lt;/text&gt;&lt;path d=&quot;M355 54 l-6 9 h12 z&quot; style=&quot;fill:var(--link)&quot; /&gt;&lt;text x=&quot;18&quot; y=&quot;102&quot; style=&quot;fill:var(--muted)&quot;&gt;(account_id, creator_id) = (1, 2)&lt;/text&gt;&lt;rect x=&quot;248&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--link);stroke:var(--link);stroke-width:1&quot; /&gt;&lt;text x=&quot;267&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--bg)&quot;&gt;95&lt;/text&gt;&lt;rect x=&quot;292&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--link);stroke:var(--link-hover);stroke-width:2&quot; /&gt;&lt;text x=&quot;311&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--bg)&quot;&gt;83&lt;/text&gt;&lt;rect x=&quot;336&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--inline-bg);stroke:var(--muted);stroke-width:1&quot; /&gt;&lt;text x=&quot;355&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--text)&quot;&gt;80&lt;/text&gt;&lt;rect x=&quot;380&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;399&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;59&lt;/text&gt;&lt;rect x=&quot;424&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;443&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;45&lt;/text&gt;&lt;rect x=&quot;468&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;487&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;22&lt;/text&gt;&lt;rect x=&quot;512&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;531&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;12&lt;/text&gt;&lt;path d=&quot;M355 116 l-6 9 h12 z&quot; style=&quot;fill:var(--link)&quot; /&gt;&lt;text x=&quot;18&quot; y=&quot;170&quot; style=&quot;fill:var(--muted)&quot;&gt;result (ORDER BY id DESC)&lt;/text&gt;&lt;rect x=&quot;248&quot; y=&quot;150&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--link);stroke:var(--link);stroke-width:1&quot; /&gt;&lt;text x=&quot;267&quot; y=&quot;170&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--bg)&quot;&gt;98&lt;/text&gt;&lt;rect x=&quot;292&quot; y=&quot;150&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--link);stroke:var(--link);stroke-width:1&quot; /&gt;&lt;text x=&quot;311&quot; y=&quot;170&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--bg)&quot;&gt;95&lt;/text&gt;&lt;rect x=&quot;336&quot; y=&quot;150&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--link);stroke:var(--link);stroke-width:1&quot; /&gt;&lt;text x=&quot;355&quot; y=&quot;170&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--bg)&quot;&gt;91&lt;/text&gt;&lt;rect x=&quot;380&quot; y=&quot;150&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--link);stroke:var(--link-hover);stroke-width:2&quot; /&gt;&lt;text x=&quot;399&quot; y=&quot;170&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--bg)&quot;&gt;83&lt;/text&gt;&lt;rect x=&quot;424&quot; y=&quot;150&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:none;stroke:var(--rule);stroke-dasharray:3 3&quot; /&gt;&lt;text x=&quot;18&quot; y=&quot;212&quot; style=&quot;fill:var(--text)&quot;&gt;entries read: 6     rows returned: 4 of 5&lt;/text&gt;&lt;/svg&gt;&lt;figcaption&gt;83 is larger than 77, so 83 goes to the result. The cursor on that run moves down to 80.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;figure class=&quot;carousel-slide&quot;&gt;&lt;svg viewBox=&quot;0 0 640 222&quot; role=&quot;img&quot; aria-label=&quot;Step 6: 80 is larger than 77, so 80 goes to the result. That&apos;s 5 rows, so the merge stops. Everything below the cursors is never read. In total: 6 entries read for 5 rows.&quot; style=&quot;font:12px system-ui,sans-serif;white-space:pre&quot;&gt;&lt;text x=&quot;18&quot; y=&quot;40&quot; style=&quot;fill:var(--muted)&quot;&gt;(account_id, creator_id) = (1, 1)&lt;/text&gt;&lt;rect x=&quot;248&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--link);stroke:var(--link);stroke-width:1&quot; /&gt;&lt;text x=&quot;267&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--bg)&quot;&gt;98&lt;/text&gt;&lt;rect x=&quot;292&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--link);stroke:var(--link);stroke-width:1&quot; /&gt;&lt;text x=&quot;311&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--bg)&quot;&gt;91&lt;/text&gt;&lt;rect x=&quot;336&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--inline-bg);stroke:var(--muted);stroke-width:1&quot; /&gt;&lt;text x=&quot;355&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--text)&quot;&gt;77&lt;/text&gt;&lt;rect x=&quot;380&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;399&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;64&lt;/text&gt;&lt;rect x=&quot;424&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;443&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;52&lt;/text&gt;&lt;rect x=&quot;468&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;487&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;40&lt;/text&gt;&lt;rect x=&quot;512&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;531&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;31&lt;/text&gt;&lt;rect x=&quot;556&quot; y=&quot;20&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;575&quot; y=&quot;40&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;17&lt;/text&gt;&lt;path d=&quot;M355 54 l-6 9 h12 z&quot; style=&quot;fill:var(--link)&quot; /&gt;&lt;text x=&quot;18&quot; y=&quot;102&quot; style=&quot;fill:var(--muted)&quot;&gt;(account_id, creator_id) = (1, 2)&lt;/text&gt;&lt;rect x=&quot;248&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--link);stroke:var(--link);stroke-width:1&quot; /&gt;&lt;text x=&quot;267&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--bg)&quot;&gt;95&lt;/text&gt;&lt;rect x=&quot;292&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--link);stroke:var(--link);stroke-width:1&quot; /&gt;&lt;text x=&quot;311&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--bg)&quot;&gt;83&lt;/text&gt;&lt;rect x=&quot;336&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--link);stroke:var(--link-hover);stroke-width:2&quot; /&gt;&lt;text x=&quot;355&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--bg)&quot;&gt;80&lt;/text&gt;&lt;rect x=&quot;380&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;399&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;59&lt;/text&gt;&lt;rect x=&quot;424&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;443&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;45&lt;/text&gt;&lt;rect x=&quot;468&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;487&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;22&lt;/text&gt;&lt;rect x=&quot;512&quot; y=&quot;82&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--code-bg);stroke:var(--rule);stroke-width:1&quot; /&gt;&lt;text x=&quot;531&quot; y=&quot;102&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--muted)&quot;&gt;12&lt;/text&gt;&lt;path d=&quot;M399 116 l-6 9 h12 z&quot; style=&quot;fill:var(--link)&quot; /&gt;&lt;text x=&quot;18&quot; y=&quot;170&quot; style=&quot;fill:var(--muted)&quot;&gt;result (ORDER BY id DESC)&lt;/text&gt;&lt;rect x=&quot;248&quot; y=&quot;150&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--link);stroke:var(--link);stroke-width:1&quot; /&gt;&lt;text x=&quot;267&quot; y=&quot;170&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--bg)&quot;&gt;98&lt;/text&gt;&lt;rect x=&quot;292&quot; y=&quot;150&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--link);stroke:var(--link);stroke-width:1&quot; /&gt;&lt;text x=&quot;311&quot; y=&quot;170&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--bg)&quot;&gt;95&lt;/text&gt;&lt;rect x=&quot;336&quot; y=&quot;150&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--link);stroke:var(--link);stroke-width:1&quot; /&gt;&lt;text x=&quot;355&quot; y=&quot;170&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--bg)&quot;&gt;91&lt;/text&gt;&lt;rect x=&quot;380&quot; y=&quot;150&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--link);stroke:var(--link);stroke-width:1&quot; /&gt;&lt;text x=&quot;399&quot; y=&quot;170&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--bg)&quot;&gt;83&lt;/text&gt;&lt;rect x=&quot;424&quot; y=&quot;150&quot; width=&quot;38&quot; height=&quot;30&quot; rx=&quot;4&quot; style=&quot;fill:var(--link);stroke:var(--link-hover);stroke-width:2&quot; /&gt;&lt;text x=&quot;443&quot; y=&quot;170&quot; text-anchor=&quot;middle&quot; style=&quot;fill:var(--bg)&quot;&gt;80&lt;/text&gt;&lt;text x=&quot;18&quot; y=&quot;212&quot; style=&quot;fill:var(--text)&quot;&gt;entries read: 6     rows returned: 5 of 5&lt;/text&gt;&lt;/svg&gt;&lt;figcaption&gt;80 is larger than 77, so 80 goes to the result. That&apos;s 5 rows, so the merge stops. Everything below the cursors is never read. In total: 6 entries read for 5 rows.&lt;/figcaption&gt;&lt;/figure&gt;
&lt;/div&gt;

&lt;p&gt;The issue is that MySQL has no operator for this kind of merge, so it executes a plan
that reads far more than 200 rows. Faced with the IN list and &lt;code class=&quot;highlighter-rouge&quot;&gt;ORDER BY id&lt;/code&gt;, it
walks the primary key backward and filters as it goes:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;-&amp;gt; Limit: 200 row(s)
     (actual time=375..375 rows=200)
  -&amp;gt; Filter: (account_id = 1 and creator_id in (1, 2))
       (actual time=375..375 rows=200)
    -&amp;gt; Index scan on activities using PRIMARY (reverse)
         (rows=3075) (actual time=0.05..313 rows=2000000)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;MySQL scanned two million rows (almost the whole table!) to return two hundred. 
It expected to read about 3,075. That estimate assumed the two users’
rows were spread evenly, but I buried them on purpose to make the point
a little more dramatic. In practice, the distribution can be anything. 
The further the users’ rows sit from the top of the &lt;code class=&quot;highlighter-rouge&quot;&gt;id&lt;/code&gt; range, the more rows MySQL reads.&lt;/p&gt;

&lt;h2 id=&quot;why-not-use-the-index&quot;&gt;Why not use the index?&lt;/h2&gt;

&lt;p&gt;We’ve shown the &lt;code class=&quot;highlighter-rouge&quot;&gt;idx_account_creator&lt;/code&gt; index works well for a single-value query.
So let’s force it and see what happens when it’s used with two values:&lt;/p&gt;

&lt;div class=&quot;language-sql highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;activities&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;FORCE&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;INDEX&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;idx_account_creator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;WHERE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;account_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;AND&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;creator_id&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;IN&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;ORDER&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;BY&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;DESC&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;LIMIT&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;200&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;-&amp;gt; Limit: 200 row(s)
     (actual time=103..103 rows=200)
  -&amp;gt; Sort: activities.id DESC, limit input to 200 row(s) per chunk
       (rows=137786) (actual time=103..103 rows=200)
    -&amp;gt; Index range scan on activities using idx_account_creator
       over (account_id = 1 AND creator_id = 1)
        OR (account_id = 1 AND creator_id = 2)
         (rows=137786) (actual time=0.34..89 rows=125000)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Better, 125,000 rows instead of two million, but still nowhere near the 201 the merge would read. The
index jumps straight to the two users’ rows, so the filtering part is solved first. The
problem is ordering. Since MySQL can’t merge them, it reads all 125,000 into a
sort and applies the limit only after sorting.&lt;/p&gt;

&lt;p&gt;Turns out both options are suboptimal. Left alone, MySQL scans the whole table.
When we force the index, it sorts every matching row. Neither ever pushes the limit down to
where the rows are read, because neither can merge the two runs… but I believe we can do better!
Reading just 201 rows is the theoretical optimum. We can’t quite hit it (with MySQL), but we can
get close. We read 400 rows, sort them, and take the top 200. That’s much better
than sorting all 125,000 matching rows. But for that, we will have to transform the query.&lt;/p&gt;

&lt;h2 id=&quot;the-fix&quot;&gt;The fix&lt;/h2&gt;

&lt;p&gt;If the engine won’t merge the runs for us, we can at least cap the work
ourselves. This trick is well known in the MySQL performance world, Peter Zaitsev
describes it &lt;a href=&quot;https://www.percona.com/blog/possible-optimization-for-sort_merge-and-union-order-by-limit/&quot;&gt;here&lt;/a&gt;.
Every row in the global top 200 is also in the top 200 of its own
run, because a row can’t be among the 200 newest overall without being among the
200 newest for its user. In short, the union of the per-user top 200s already contains
the whole answer, and that union is at most 400 rows. This is how we’ll shape the
rewrite. We query each value on its own, each with its own limit and then combine the
results.&lt;/p&gt;

&lt;div class=&quot;language-sql highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;activities&lt;/span&gt;
     &lt;span class=&quot;k&quot;&gt;WHERE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;account_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;AND&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;creator_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
     &lt;span class=&quot;k&quot;&gt;ORDER&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;BY&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;DESC&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;LIMIT&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;UNION&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;ALL&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;FROM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;activities&lt;/span&gt;
     &lt;span class=&quot;k&quot;&gt;WHERE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;account_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;AND&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;creator_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
     &lt;span class=&quot;k&quot;&gt;ORDER&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;BY&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;DESC&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;LIMIT&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;AS&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;activities&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;ORDER&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;BY&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;DESC&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;LIMIT&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;200&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Each branch now filters on a single user, so each branch behaves like the single-user query
we’ve already described: MySQL walks &lt;code class=&quot;highlighter-rouge&quot;&gt;(account_id, creator_id, id)&lt;/code&gt; backward for that user and
stops after 200 rows, with no sort inside the branch. The outer query then orders
at most &lt;code class=&quot;highlighter-rouge&quot;&gt;branches × 200&lt;/code&gt; rows, which is, in this case, negligible.&lt;/p&gt;

&lt;p&gt;That per-branch &lt;code class=&quot;highlighter-rouge&quot;&gt;LIMIT&lt;/code&gt; is crucial. Without it, each branch returns every row for its
user. MySQL reads all 125,000 into a sort again, exactly the plan we were trying to
escape. So, the limit has to sit inside each branch, not only on the outer query.&lt;/p&gt;

&lt;p&gt;The plan confirms it:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;-&amp;gt; Limit: 200 row(s)
     (actual time=6.42..6.49 rows=200)
  -&amp;gt; Sort: activities.id DESC, limit input to 200 row(s) per chunk
       (actual time=6.42..6.45 rows=200)
    -&amp;gt; Table scan on activities
         (actual time=6.12..6.25 rows=400)
      -&amp;gt; Union all materialize
           (actual time=6.12..6.12 rows=400)
        -&amp;gt; Limit: 200 row(s)
             (actual time=2.55..2.67 rows=200)
          -&amp;gt; Index lookup on activities using idx_account_creator
             (account_id=1, creator_id=1) (reverse)
               (rows=128788) (actual time=2.55..2.64 rows=200)
        -&amp;gt; Limit: 200 row(s)
             (actual time=3.03..3.14 rows=200)
          -&amp;gt; Index lookup on activities using idx_account_creator
             (account_id=1, creator_id=2) (reverse)
               (rows=8998) (actual time=3.03..3.14 rows=200)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To sum it all up, we’ve got three plans, same result, three very 
different amounts of work:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;approach&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;rows read&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;time&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;default (PK scan)&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;2,000,000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;~375 ms&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;forced index + sort&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;125,000&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;~103 ms&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;UNION ALL per branch&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;400&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;~6.4 ms&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;The rewrite only pays off under two conditions:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;The branches have to cut rows.&lt;/strong&gt; If every user has fewer rows than the limit,
each branch returns all of them. Both plans then read the same rows, so there’s
nothing to gain.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Each branch has to be cheap.&lt;/strong&gt; Every branch is a separate query, so whatever
else the &lt;code class=&quot;highlighter-rouge&quot;&gt;WHERE&lt;/code&gt; clause carries runs once per value. In my simple example, it’s
only an index lookup, but if the query were to go through an expensive scope, like a
permission subquery or a few joins, the rewrite would repeat that work for every value.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;pagination&quot;&gt;Pagination&lt;/h2&gt;

&lt;p&gt;Offset pagination adds a subtlety. If the request is the third page of 200, we
need rows 401 to 600 in the global order. In the worst case all 400 rows before
them come from a single branch, so the per-branch limit has to be
&lt;code class=&quot;highlighter-rouge&quot;&gt;page_number × page_size&lt;/code&gt;, not just &lt;code class=&quot;highlighter-rouge&quot;&gt;page_size&lt;/code&gt;. The rewrite still holds, but its
cost now grows linearly with page depth.&lt;/p&gt;

&lt;p&gt;Cursor pagination resolves this problem. When the client passes the last &lt;code class=&quot;highlighter-rouge&quot;&gt;id&lt;/code&gt; it’s
seen (&lt;code class=&quot;highlighter-rouge&quot;&gt;filter[before_id]=X&lt;/code&gt;), each branch keeps a constant limit of &lt;code class=&quot;highlighter-rouge&quot;&gt;page_size&lt;/code&gt;,
because the cursor decides where to resume. Each branch just gets an extra
&lt;code class=&quot;highlighter-rouge&quot;&gt;AND id &amp;lt; X&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;the-postgres-equivalent&quot;&gt;The Postgres equivalent&lt;/h2&gt;

&lt;p&gt;If we hand Postgres 14 the same UNION ALL rewrite, its planner reaches for an operator
MySQL doesn’t have, &lt;em&gt;Merge Append&lt;/em&gt;:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Limit  (actual time=0.78..1.09 rows=200)
  -&amp;gt; Merge Append  (actual time=0.77..1.05 rows=200)
       Sort Key: id DESC
       -&amp;gt; Limit  (actual rows=192)
            -&amp;gt; Index Scan Backward using idx_account_creator (actual rows=192)
                 Index Cond: ((account_id = 1) AND (creator_id = 1))
       -&amp;gt; Limit  (actual rows=9)
            -&amp;gt; Index Scan Backward using idx_account_creator (actual rows=9)
                 Index Cond: ((account_id = 1) AND (creator_id = 2))
Execution Time: 1.228 ms
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Merge Append is a true streaming merge. We can see that in the counts: 192 rows from
one branch, 9 from the other, 201 in total, not 400. That’s the theoretical optimum
we described earlier.&lt;/p&gt;

&lt;h2 id=&quot;takeaway&quot;&gt;Takeaway&lt;/h2&gt;

&lt;p&gt;A single index can do both jobs this query needs, finding the matching rows and
returning them in order. But it only does both at once if the query is structured
to let it.&lt;/p&gt;

&lt;p&gt;So when a query looks like it should be fast from the indexes alone and isn’t,
check whether the &lt;code class=&quot;highlighter-rouge&quot;&gt;ORDER BY&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;LIMIT&lt;/code&gt; and multi-value filter together are forcing
the engine to read and sort far more than the limit. If they are, rewriting it
as a UNION ALL of per-value branches, each with its own limit, can significantly cut 
the amount of work.&lt;/p&gt;</content><author><name></name></author></entry></feed>