<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Beyond Nguyen</title>
    <description>The latest articles on DEV Community by Beyond Nguyen (@tada_dev).</description>
    <link>https://dev.to/tada_dev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4258328%2Ff6ba8092-7fb5-434d-8774-1221002d1561.JPG</url>
      <title>DEV Community: Beyond Nguyen</title>
      <link>https://dev.to/tada_dev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tada_dev"/>
    <language>en</language>
    <item>
      <title>Stop rewriting IN as EXISTS for speed. The real bomb is NOT IN</title>
      <dc:creator>Beyond Nguyen</dc:creator>
      <pubDate>Sun, 22 Nov 2026 06:13:53 +0000</pubDate>
      <link>https://dev.to/tada_dev/stop-rewriting-in-as-exists-for-speed-the-real-bomb-is-not-in-50h4</link>
      <guid>https://dev.to/tada_dev/stop-rewriting-in-as-exists-for-speed-the-real-bomb-is-not-in-50h4</guid>
      <description>&lt;p&gt;A reviewer left a familiar comment on my migration: "switch &lt;code&gt;IN&lt;/code&gt; to &lt;code&gt;EXISTS&lt;/code&gt;, it's faster."&lt;br&gt;
So I measured it. &lt;code&gt;IN&lt;/code&gt;, &lt;code&gt;EXISTS&lt;/code&gt;, &lt;code&gt;USING&lt;/code&gt;: Postgres runs all three with the exact same plan. The differences are noise.&lt;/p&gt;

&lt;p&gt;The scary part was somewhere nobody commented on: &lt;code&gt;NOT IN&lt;/code&gt;.&lt;br&gt;
One &lt;code&gt;NULL&lt;/code&gt; row, and the DELETE removes… zero rows. No error, no warning.&lt;br&gt;
And once the table grows a little, it jumps from 29 ms to over 2 minutes.&lt;/p&gt;
&lt;h2&gt;
  
  
  One job, three ways to write it, and an argument
&lt;/h2&gt;

&lt;p&gt;Take a problem every codebase has: an &lt;code&gt;orders&lt;/code&gt; table and an &lt;code&gt;order_items&lt;/code&gt; table. When an order is cancelled, delete its items.&lt;/p&gt;

&lt;p&gt;The first version, the one I wrote on autopilot, uses &lt;code&gt;IN&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;order_items&lt;/span&gt;
&lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;order_id&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'cancelled'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second, &lt;code&gt;EXISTS&lt;/code&gt;, is what the reviewer suggested:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;order_items&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;
&lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="k"&gt;exists&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt; &lt;span class="k"&gt;and&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'cancelled'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The third, &lt;code&gt;USING&lt;/code&gt;, is Postgres's own syntax for a DELETE with a join:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;order_items&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;
&lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt; &lt;span class="k"&gt;and&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'cancelled'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three queries, one job. Instead of arguing about which is faster, I made them race.&lt;br&gt;
I ran Postgres 18.7 in a devcontainer and loaded 200,000 orders and 950,000 items.&lt;br&gt;
One order in ten is cancelled, so every query has to delete exactly 100,000 items.&lt;br&gt;
The data is generated for the test, not production numbers.&lt;br&gt;
Each query ran under &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; inside &lt;code&gt;BEGIN … ROLLBACK&lt;/code&gt; so the data stayed intact, three times, taking the median.&lt;/p&gt;
&lt;h2&gt;
  
  
  Is EXISTS really faster than IN?
&lt;/h2&gt;

&lt;p&gt;Before measuring, I was half convinced. The explanation I got sounded very reasonable:&lt;br&gt;
&lt;code&gt;IN&lt;/code&gt; runs the subquery first, builds a big list, then checks every row against it.&lt;br&gt;
&lt;code&gt;EXISTS&lt;/code&gt; is smarter: it checks row by row and stops at the first match.&lt;br&gt;
It sounded so reasonable that I nearly changed the code without measuring anything.&lt;/p&gt;

&lt;p&gt;But that's how &lt;em&gt;I read&lt;/em&gt; the SQL. How Postgres &lt;em&gt;runs&lt;/em&gt; it is a question for &lt;code&gt;EXPLAIN&lt;/code&gt;. Here is the plan for the &lt;code&gt;IN&lt;/code&gt; version:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Delete on order_items (actual time=120.559..120.560 rows=0.00 loops=1)
  Buffers: shared hit=106218
  -&amp;gt;  Hash Join (actual time=7.471..101.665 rows=100000.00 loops=1)
        Hash Cond: (order_items.order_id = orders.id)
        -&amp;gt;  Seq Scan on order_items (actual time=0.004..52.265 rows=950000.00 loops=1)
        -&amp;gt;  Hash (actual time=7.461..7.462 rows=20000.00 loops=1)
              -&amp;gt;  Seq Scan on orders (actual time=0.002..6.243 rows=20000.00 loops=1)
                    Filter: (status = 'cancelled'::text)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The plans for &lt;code&gt;EXISTS&lt;/code&gt; and &lt;code&gt;USING&lt;/code&gt; match it line for line, apart from the aliases.&lt;br&gt;
Same &lt;code&gt;Hash Join&lt;/code&gt;, same &lt;code&gt;shared hit=106218&lt;/code&gt;, so all three touch exactly the same number of pages.&lt;br&gt;
There is no "build a list" step and no "stop at the first match" step.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Query&lt;/th&gt;
&lt;th&gt;Plan&lt;/th&gt;
&lt;th&gt;Time (median)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;IN&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Hash Join&lt;/td&gt;
&lt;td&gt;120.6 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;EXISTS&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Hash Join&lt;/td&gt;
&lt;td&gt;139.0 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;USING&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Hash Join&lt;/td&gt;
&lt;td&gt;116.2 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;By this table, &lt;code&gt;EXISTS&lt;/code&gt; is actually the slowest. Don't crown &lt;code&gt;IN&lt;/code&gt; yet: I dropped the index on &lt;code&gt;order_items(order_id)&lt;/code&gt; and ran it again.&lt;br&gt;
This time &lt;code&gt;EXISTS&lt;/code&gt; won: 119.1 ms, &lt;code&gt;USING&lt;/code&gt; 120.5 ms, &lt;code&gt;IN&lt;/code&gt; 124.9 ms.&lt;br&gt;
The ranking shuffles between runs, because when the plan is the same, the difference is noise.&lt;/p&gt;

&lt;p&gt;The reason is the planner. When it sees &lt;code&gt;IN (subquery)&lt;/code&gt; or &lt;code&gt;EXISTS (subquery)&lt;/code&gt; in a &lt;code&gt;WHERE&lt;/code&gt; clause, Postgres pulls the subquery up into a semi-join, then picks a join strategy like it would for any other join.&lt;br&gt;
Here &lt;code&gt;orders.id&lt;/code&gt; is the primary key, so each item matches at most one order, and the planner reduces the semi-join to a plain join. That is exactly what &lt;code&gt;USING&lt;/code&gt; spells out by hand.&lt;br&gt;
Both steps are right there in the Postgres source: &lt;a href="https://github.com/postgres/postgres/blob/master/src/backend/optimizer/prep/prepjointree.c" rel="noopener noreferrer"&gt;&lt;code&gt;pull_up_sublinks&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://github.com/postgres/postgres/blob/master/src/backend/optimizer/plan/analyzejoins.c" rel="noopener noreferrer"&gt;&lt;code&gt;reduce_unique_semijoins&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So for a positive condition, "delete the rows that &lt;em&gt;have&lt;/em&gt; a match", choosing between &lt;code&gt;IN&lt;/code&gt;, &lt;code&gt;EXISTS&lt;/code&gt; and &lt;code&gt;USING&lt;/code&gt; is about readability.&lt;br&gt;
Not speed. Three different outfits, one skeleton.&lt;/p&gt;
&lt;h2&gt;
  
  
  NOT IN: one NULL and the DELETE deletes… nothing
&lt;/h2&gt;

&lt;p&gt;Now flip the problem: delete the orders that &lt;em&gt;no longer&lt;/em&gt; have any items. The test data has 10,000 of them.&lt;br&gt;
On autopilot, the counterpart of &lt;code&gt;IN&lt;/code&gt; is &lt;code&gt;NOT IN&lt;/code&gt;, and the counterpart of &lt;code&gt;EXISTS&lt;/code&gt; is &lt;code&gt;NOT EXISTS&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- NOT IN&lt;/span&gt;
&lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;
&lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="n"&gt;order_id&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;order_items&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- NOT EXISTS&lt;/span&gt;
&lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;
&lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="k"&gt;exists&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;order_items&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;order_items.order_id&lt;/code&gt; column is nullable. With no &lt;code&gt;NULL&lt;/code&gt; rows, both queries delete exactly 10,000 orders.&lt;br&gt;
I added &lt;strong&gt;one&lt;/strong&gt; item with &lt;code&gt;order_id = NULL&lt;/code&gt; and ran them again:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Query&lt;/th&gt;
&lt;th&gt;Orders deleted&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;NOT IN&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;NOT EXISTS&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;10,000&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;No error, no warning. It just quietly deletes nothing.&lt;/p&gt;

&lt;p&gt;The cause is SQL's three-valued logic. &lt;code&gt;id not in (1, 2, NULL)&lt;/code&gt; means &lt;code&gt;id &amp;lt;&amp;gt; 1 AND id &amp;lt;&amp;gt; 2 AND id &amp;lt;&amp;gt; NULL&lt;/code&gt;.&lt;br&gt;
Comparing anything to &lt;code&gt;NULL&lt;/code&gt; gives &lt;code&gt;NULL&lt;/code&gt;, not &lt;code&gt;TRUE&lt;/code&gt;. The whole expression can never be &lt;code&gt;TRUE&lt;/code&gt;, so no row qualifies.&lt;br&gt;
The Postgres docs spell this out in &lt;a href="https://www.postgresql.org/docs/current/functions-subquery.html" rel="noopener noreferrer"&gt;Subquery Expressions&lt;/a&gt;, and the Postgres wiki lists &lt;code&gt;NOT IN&lt;/code&gt; under &lt;a href="https://wiki.postgresql.org/wiki/Don%27t_Do_This#Don.27t_use_NOT_IN" rel="noopener noreferrer"&gt;Don't Do This&lt;/a&gt;.&lt;br&gt;
&lt;code&gt;NOT EXISTS&lt;/code&gt; is immune, because it only asks "is there a matching row?", and &lt;code&gt;NULL = 5&lt;/code&gt; is not a match.&lt;/p&gt;




&lt;pre data-lang="mermaid"&gt;&lt;code&gt;flowchart LR
  A["id = 5"] --&amp;gt; B{"5 NOT IN (1, 2, NULL)"}
  B --&amp;gt; C["5 &amp;lt;&amp;gt; 1 → TRUE"]
  B --&amp;gt; D["5 &amp;lt;&amp;gt; 2 → TRUE"]
  B --&amp;gt; E["5 &amp;lt;&amp;gt; NULL → NULL"]
  C &amp;amp; D &amp;amp; E --&amp;gt; F["TRUE AND TRUE AND NULL = NULL"]
  F --&amp;gt; G["WHERE NULL → row is skipped"]&lt;/code&gt;&lt;/pre&gt;


&lt;h2&gt;
  
  
  5,000 more orders, from 29 ms to over 2 minutes
&lt;/h2&gt;

&lt;p&gt;Wrong results are bad enough. But even with no &lt;code&gt;NULL&lt;/code&gt; anywhere, &lt;code&gt;NOT IN&lt;/code&gt; has a second trap: speed.&lt;/p&gt;

&lt;p&gt;To preserve the &lt;code&gt;NULL&lt;/code&gt; semantics above, Postgres here does not turn &lt;code&gt;NOT IN&lt;/code&gt; into an anti-join the way it does with &lt;code&gt;NOT EXISTS&lt;/code&gt;.&lt;br&gt;
That leaves two options. If the planner estimates that the whole subquery result fits in &lt;code&gt;work_mem&lt;/code&gt;, it builds a hash table (&lt;code&gt;hashed SubPlan&lt;/code&gt;) and runs fast.&lt;br&gt;
If not, it rescans the list for &lt;strong&gt;every row&lt;/strong&gt; of the outer table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Delete on orders
  -&amp;gt;  Seq Scan on orders
        Filter: (NOT (ANY (id = (SubPlan 1).col1)))
        SubPlan 1
          -&amp;gt;  Materialize
                -&amp;gt;  Seq Scan on order_items
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second option means billions of comparisons. To find where it kicks in, I kept the default 4MB &lt;code&gt;work_mem&lt;/code&gt; and grew the data step by step:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;orders&lt;/th&gt;
&lt;th&gt;order_items&lt;/th&gt;
&lt;th&gt;
&lt;code&gt;NOT IN&lt;/code&gt; plan&lt;/th&gt;
&lt;th&gt;&lt;code&gt;NOT IN&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;NOT EXISTS&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;25,000&lt;/td&gt;
&lt;td&gt;118,750&lt;/td&gt;
&lt;td&gt;hashed SubPlan&lt;/td&gt;
&lt;td&gt;13 ms&lt;/td&gt;
&lt;td&gt;14 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;50,000&lt;/td&gt;
&lt;td&gt;237,500&lt;/td&gt;
&lt;td&gt;hashed SubPlan&lt;/td&gt;
&lt;td&gt;28 ms&lt;/td&gt;
&lt;td&gt;28 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;55,000&lt;/td&gt;
&lt;td&gt;261,250&lt;/td&gt;
&lt;td&gt;hashed SubPlan&lt;/td&gt;
&lt;td&gt;29 ms&lt;/td&gt;
&lt;td&gt;31 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;60,000&lt;/td&gt;
&lt;td&gt;285,000&lt;/td&gt;
&lt;td&gt;SubPlan (not hashed)&lt;/td&gt;
&lt;td&gt;&amp;gt; 120 s&lt;/td&gt;
&lt;td&gt;34 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;70,000&lt;/td&gt;
&lt;td&gt;332,500&lt;/td&gt;
&lt;td&gt;SubPlan (not hashed)&lt;/td&gt;
&lt;td&gt;&amp;gt; 120 s&lt;/td&gt;
&lt;td&gt;45 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This isn't a gentle slope. It's a cliff.&lt;br&gt;
Below the threshold, &lt;code&gt;NOT IN&lt;/code&gt; is as fast as &lt;code&gt;NOT EXISTS&lt;/code&gt;. Add 9% more data and it runs past 2 minutes before I kill it, while &lt;code&gt;NOT EXISTS&lt;/code&gt; still takes 34 ms.&lt;/p&gt;

&lt;p&gt;This is the scariest part. On a dev machine with small tables, the &lt;code&gt;NOT IN&lt;/code&gt; query flies and nobody in review notices anything.&lt;br&gt;
In production the table keeps growing, one day it crosses the threshold, and the plan silently switches.&lt;br&gt;
Nobody changed the code. Only the data grew.&lt;/p&gt;

&lt;p&gt;Raising &lt;code&gt;work_mem&lt;/code&gt; to 64MB does bring &lt;code&gt;NOT IN&lt;/code&gt; back to 138 ms at 950,000 items.&lt;br&gt;
But that only moves the cliff further away, it doesn't flatten it, and it does nothing about &lt;code&gt;NULL&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  So which one should you write?
&lt;/h2&gt;

&lt;p&gt;My migration kept &lt;code&gt;IN&lt;/code&gt;. With identical plans there's no speed reason to change it, and &lt;code&gt;IN&lt;/code&gt; is the shortest version: you read it once and you get it.&lt;/p&gt;

&lt;p&gt;The rules I now follow:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Positive conditions&lt;/strong&gt; (delete or update rows that &lt;em&gt;have&lt;/em&gt; a match): pick for readability. One column, &lt;code&gt;IN&lt;/code&gt; is shortest. Multi-column conditions, &lt;code&gt;EXISTS&lt;/code&gt;. Need columns from the other table, for example in &lt;code&gt;RETURNING&lt;/code&gt;, use &lt;code&gt;USING&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Negative conditions&lt;/strong&gt; (rows with &lt;em&gt;no&lt;/em&gt; match): always &lt;code&gt;NOT EXISTS&lt;/code&gt;. I haven't seen a case where &lt;code&gt;NOT IN (subquery)&lt;/code&gt; is worth keeping.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are trade-offs. &lt;code&gt;USING&lt;/code&gt; is Postgres-only syntax; MySQL has multi-table DELETE too, but written differently, so switching databases means rewriting it.&lt;br&gt;
&lt;code&gt;EXISTS&lt;/code&gt; is longer and forces you to use aliases.&lt;/p&gt;

&lt;p&gt;To be fair about the limits of this test: one Postgres version, one data shape, a container on a laptop.&lt;br&gt;
Don't compare the absolute milliseconds to your machine. What holds up is the shape of the plans and the ratios between queries.&lt;br&gt;
MySQL, SQL Server and Oracle are out of scope here. Their planners have their own personalities.&lt;/p&gt;
&lt;h2&gt;
  
  
  Before rewriting a query "for speed", ask EXPLAIN
&lt;/h2&gt;

&lt;p&gt;Next time someone says "switch to &lt;code&gt;EXISTS&lt;/code&gt;, it's faster", don't argue and don't rush to change it.&lt;br&gt;
Run &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; on both versions with enough data, and put the two plans side by side. If the plans match, the debate is over: keep whichever reads better.&lt;/p&gt;

&lt;p&gt;Wrap it in a transaction. &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; really executes the statement, and for a DELETE that means really deleting:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;explain&lt;/span&gt; &lt;span class="k"&gt;analyze&lt;/span&gt; &lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;order_items&lt;/span&gt; &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;order_id&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'cancelled'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;rollback&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One more thing worth doing today. It takes about a minute:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;grep -rniE &lt;span class="s2"&gt;"not in \([[:space:]]*select"&lt;/span&gt; src/ migrations/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every hit is a place that can fall into one of the two traps above: a single &lt;code&gt;NULL&lt;/code&gt;, or the day the table outgrows &lt;code&gt;work_mem&lt;/code&gt;. Rewrite it as &lt;code&gt;NOT EXISTS&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The test script, so you can run it yourself: &lt;a href="https://gist.github.com/thientuquangtrung/358803972e091ff910a79e2d9667b1c3" rel="noopener noreferrer"&gt;IN / EXISTS / NOT IN lab gist&lt;/a&gt;.&lt;br&gt;
Run it on your machine. Your planner may have a personality of its own.&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>sql</category>
      <category>performance</category>
      <category>backend</category>
    </item>
  </channel>
</rss>
