<?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: Abdeldjalil Chougui</title>
    <description>The latest articles on DEV Community by Abdeldjalil Chougui (@abdeldjalilchougui).</description>
    <link>https://dev.to/abdeldjalilchougui</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%2F1033886%2Fce8b325b-79e4-4dfc-945c-702c115acdb1.png</url>
      <title>DEV Community: Abdeldjalil Chougui</title>
      <link>https://dev.to/abdeldjalilchougui</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abdeldjalilchougui"/>
    <language>en</language>
    <item>
      <title>Database Normalization in 3 Minutes (1NF 3NF)</title>
      <dc:creator>Abdeldjalil Chougui</dc:creator>
      <pubDate>Tue, 01 Dec 2026 22:57:30 +0000</pubDate>
      <link>https://dev.to/abdeldjalilchougui/database-normalization-in-3-minutes-1nf-3nf-36b3</link>
      <guid>https://dev.to/abdeldjalilchougui/database-normalization-in-3-minutes-1nf-3nf-36b3</guid>
      <description>&lt;p&gt;You start a project with one table, because it is quick. Every order row holds the customer's name, phone and city, plus the list of products in one text column. A month later, a customer changes her phone number and you update it in three of her seven orders. Now the database disagrees with itself.&lt;/p&gt;

&lt;p&gt;Normalization is the set of rules that prevents this. It sounds academic, but the core idea is simple: store each fact in exactly one place. This article takes one messy table through the first three normal forms, which is as far as most applications need to go.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problems normalization solves
&lt;/h2&gt;

&lt;p&gt;Duplicated data causes three kinds of trouble, called anomalies:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Update anomaly.&lt;/strong&gt; A fact stored in many rows must be changed in all of them. Miss one and the data is inconsistent.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Insert anomaly.&lt;/strong&gt; You cannot record one fact without another. If customers only exist inside order rows, you cannot add a customer who has not ordered yet.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Delete anomaly.&lt;/strong&gt; Deleting one fact destroys another. Delete a customer's only order and you lose the customer.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each normal form removes a specific source of these problems.&lt;/p&gt;

&lt;h2&gt;
  
  
  The starting table
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;orders
order_id | order_date | customer_name | customer_phone | customer_city | city_postal | products
---------+------------+---------------+----------------+---------------+-------------+--------------------------------
1        | 2026-11-02 | Amina         | 0550 11 22 33  | Oran          | 31000       | Keyboard x1 (4500), Mouse x2 (1200)
2        | 2026-11-03 | Karim         | 0661 44 55 66  | Setif         | 19000       | Monitor x1 (32000)
3        | 2026-11-05 | Amina         | 0550 11 22 33  | Oran          | 31000       | Mouse x1 (1200)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  First normal form (1NF): one value per cell
&lt;/h2&gt;

&lt;p&gt;A table is in 1NF when every column holds a single, atomic value and there are no repeating groups. Our &lt;code&gt;products&lt;/code&gt; column breaks that rule: it holds a list, with quantities and prices packed into text. You cannot easily ask "how many mice did we sell?" or put an index on a product.&lt;/p&gt;

&lt;p&gt;The fix is to give each product in an order its own row:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;order_items
order_id | product  | quantity | unit_price | order_date | customer_name | customer_phone | customer_city | city_postal
---------+----------+----------+------------+------------+---------------+----------------+---------------+------------
1        | Keyboard | 1        | 4500       | 2026-11-02 | Amina         | 0550 11 22 33  | Oran          | 31000
1        | Mouse    | 2        | 1200       | 2026-11-02 | Amina         | 0550 11 22 33  | Oran          | 31000
2        | Monitor  | 1        | 32000      | 2026-11-03 | Karim         | 0661 44 55 66  | Setif         | 19000
3        | Mouse    | 1        | 1200       | 2026-11-05 | Amina         | 0550 11 22 33  | Oran          | 31000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every cell now holds one value, and the key that identifies a row is &lt;code&gt;(order_id, product)&lt;/code&gt;. But the duplication got worse: Amina's phone now appears three times.&lt;/p&gt;

&lt;h2&gt;
  
  
  Second normal form (2NF): depend on the whole key
&lt;/h2&gt;

&lt;p&gt;A table is in 2NF when it is in 1NF and every non-key column depends on the whole primary key, not just part of it. This only matters when the key has several columns.&lt;/p&gt;

&lt;p&gt;Look at the columns:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;quantity&lt;/code&gt; depends on the order and the product together. Fine.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;order_date&lt;/code&gt;, &lt;code&gt;customer_name&lt;/code&gt;, &lt;code&gt;customer_phone&lt;/code&gt;, &lt;code&gt;customer_city&lt;/code&gt;, &lt;code&gt;city_postal&lt;/code&gt; depend only on &lt;code&gt;order_id&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;unit_price&lt;/code&gt; here depends only on &lt;code&gt;product&lt;/code&gt; (assuming one catalog price; more on that below).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These partial dependencies are why data repeats. Split the table so each fact lives with the key it depends on:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;orders(order_id, order_date, customer_name, customer_phone, customer_city, city_postal)
products(product, unit_price)
order_items(order_id, product, quantity)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Order details are stored once per order, product prices once per product.&lt;/p&gt;

&lt;h2&gt;
  
  
  Third normal form (3NF): no facts about non-key columns
&lt;/h2&gt;

&lt;p&gt;A table is in 3NF when it is in 2NF and no non-key column depends on another non-key column. In other words, every column describes the key, the whole key, and nothing but the key.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;orders&lt;/code&gt; table still has two such chains:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;customer_phone&lt;/code&gt; and &lt;code&gt;customer_city&lt;/code&gt; describe the customer, not the order. They depend on &lt;code&gt;order_id&lt;/code&gt; only through the customer.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;city_postal&lt;/code&gt; describes the city. It depends on &lt;code&gt;customer_city&lt;/code&gt;, not on the order.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These transitive dependencies are what caused the phone number problem. Move each fact to the table it describes:&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;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;cities&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt;     &lt;span class="kt"&gt;BIGINT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;name&lt;/span&gt;   &lt;span class="kt"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;postal&lt;/span&gt; &lt;span class="kt"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt;      &lt;span class="kt"&gt;BIGINT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;name&lt;/span&gt;    &lt;span class="kt"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;phone&lt;/span&gt;   &lt;span class="kt"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;city_id&lt;/span&gt; &lt;span class="kt"&gt;BIGINT&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="nf"&gt;cities&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;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt;          &lt;span class="kt"&gt;BIGINT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="kt"&gt;BIGINT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="nf"&gt;customers&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;span class="n"&gt;order_date&lt;/span&gt;  &lt;span class="kt"&gt;DATE&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;id&lt;/span&gt;    &lt;span class="kt"&gt;BIGINT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;name&lt;/span&gt;  &lt;span class="kt"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="kt"&gt;INTEGER&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;order_items&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;order_id&lt;/span&gt;   &lt;span class="kt"&gt;BIGINT&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="nf"&gt;orders&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;span class="n"&gt;product_id&lt;/span&gt; &lt;span class="kt"&gt;BIGINT&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="nf"&gt;products&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;span class="n"&gt;quantity&lt;/span&gt;   &lt;span class="kt"&gt;INTEGER&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;CHECK&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;quantity&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;unit_price&lt;/span&gt; &lt;span class="kt"&gt;INTEGER&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;product_id&lt;/span&gt;&lt;span class="p"&gt;)&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;Now Amina's phone is stored once. Changing it is one &lt;code&gt;UPDATE&lt;/code&gt;. You can add a customer before her first order, and deleting an order does not delete the customer.&lt;/p&gt;

&lt;h2&gt;
  
  
  A detail that matters: historical values
&lt;/h2&gt;

&lt;p&gt;Notice that &lt;code&gt;order_items&lt;/code&gt; keeps a &lt;code&gt;unit_price&lt;/code&gt; even though &lt;code&gt;products&lt;/code&gt; has a &lt;code&gt;price&lt;/code&gt;. That is not a normalization mistake. The catalog price is "the price today"; the order item price is "the price this customer paid". They are different facts. If you only kept the catalog price, every past invoice would change when you raise a price. The same applies to a shipping address copied onto an order. Ask what fact a column records, not just whether the text looks the same.&lt;/p&gt;

&lt;h2&gt;
  
  
  Beyond 3NF
&lt;/h2&gt;

&lt;p&gt;There are stricter forms. Boyce-Codd normal form (BCNF) tightens 3NF for some tables with overlapping candidate keys, and 4NF and 5NF deal with multi-valued and join dependencies. In everyday application design, reaching 3NF removes most of the practical problems.&lt;/p&gt;

&lt;h2&gt;
  
  
  When to denormalize
&lt;/h2&gt;

&lt;p&gt;Normalization optimizes for correctness and easy writes. Sometimes reads matter more:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Reporting and analytics.&lt;/strong&gt; Data warehouses often use star schemas with wide, denormalized dimension tables, because they are read far more than written.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hot read paths.&lt;/strong&gt; Storing an &lt;code&gt;order_total&lt;/code&gt; on the order, or a &lt;code&gt;comment_count&lt;/code&gt; on a post, saves a join or an aggregate on every page view.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Caches and search indexes.&lt;/strong&gt; Copies built from the normalized data.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The rule: normalize first, then denormalize deliberately, with a clear plan to keep the copy in sync (a trigger, a transaction that updates both, or a background job).&lt;/p&gt;

&lt;h2&gt;
  
  
  Common mistakes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Packing lists into one column (comma-separated tags, JSON arrays of IDs that should be rows) and then needing to query them.&lt;/li&gt;
&lt;li&gt;Over-normalizing simple values, such as a separate table for a status that is better as a &lt;code&gt;CHECK&lt;/code&gt; constraint or enum.&lt;/li&gt;
&lt;li&gt;Treating historical values as duplicates and "fixing" them away.&lt;/li&gt;
&lt;li&gt;Denormalizing early "for performance" without measuring a real slow query.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  In short
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Normalization means storing each fact in one place to avoid update, insert and delete anomalies.&lt;/li&gt;
&lt;li&gt;1NF: one value per cell, no lists. 2NF: no column depends on only part of a composite key. 3NF: no column depends on another non-key column.&lt;/li&gt;
&lt;li&gt;Keep historical values such as the price paid; they are different facts.&lt;/li&gt;
&lt;li&gt;Normalize by default, and denormalize on purpose where reads need it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Watch the animated version: &lt;a href="https://youtu.be/V8z8rYK8LrY" rel="noopener noreferrer"&gt;https://youtu.be/V8z8rYK8LrY&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://abdeldjalilchougui.biz/blog/database-normalization-in-3-minutes" rel="noopener noreferrer"&gt;abdeldjalilchougui.biz&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>database</category>
      <category>backend</category>
      <category>sql</category>
    </item>
    <item>
      <title>B-Tree vs LSM-Tree Cheat Sheet</title>
      <dc:creator>Abdeldjalil Chougui</dc:creator>
      <pubDate>Tue, 01 Dec 2026 22:55:29 +0000</pubDate>
      <link>https://dev.to/abdeldjalilchougui/b-tree-vs-lsm-tree-cheat-sheet-466k</link>
      <guid>https://dev.to/abdeldjalilchougui/b-tree-vs-lsm-tree-cheat-sheet-466k</guid>
      <description>&lt;p&gt;Most storage engines are built on one of two structures. B-trees update sorted pages in place. LSM-trees (log-structured merge trees) buffer writes in memory, flush them as immutable sorted files and merge those files in the background. This cheat sheet compares them on the points that matter when you choose a database or tune one.&lt;/p&gt;

&lt;h2&gt;
  
  
  B-tree in short
&lt;/h2&gt;

&lt;p&gt;Keys are kept sorted in fixed-size pages, organised as a shallow, balanced tree. A lookup walks from the root to a leaf, usually three or four pages. A write finds the right leaf, changes it in memory, logs the change in the write-ahead log, and the page is written back to its place later. Full pages split in two.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Used by:&lt;/strong&gt; PostgreSQL, MySQL InnoDB, SQLite, SQL Server, Oracle, and most relational databases.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Strengths:&lt;/strong&gt; one place per key, predictable read latency, excellent range scans over sorted, linked leaves, mature tooling.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Weak points:&lt;/strong&gt; random writes across the disk, a whole page rewritten for a small change, free space left in pages after splits, and maintenance such as vacuum or index rebuilds.&lt;/p&gt;

&lt;h2&gt;
  
  
  LSM-tree in short
&lt;/h2&gt;

&lt;p&gt;A write goes to a write-ahead log and an in-memory sorted table (the memtable). When the memtable is full, it is flushed to disk as an SSTable, a sorted file that is never modified. Background compaction merges SSTables, keeps the newest version of each key and drops deleted ones. Deletes are written as tombstones.&lt;/p&gt;

&lt;p&gt;A read checks the memtable, then SSTables from newest to oldest. Bloom filters let it skip files that cannot contain the key.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Used by:&lt;/strong&gt; RocksDB, LevelDB, Cassandra, ScyllaDB, HBase, and the storage layer of several distributed SQL databases. MySQL can use it through MyRocks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Strengths:&lt;/strong&gt; sequential writes, high write throughput, dense files that compress well.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Weak points:&lt;/strong&gt; reads may check several places, range scans merge several files, compaction competes with live traffic for disk and CPU, and deleted data lingers as tombstones until compaction.&lt;/p&gt;

&lt;h2&gt;
  
  
  Side by side
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;B-tree&lt;/th&gt;
&lt;th&gt;LSM-tree&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Write path&lt;/td&gt;
&lt;td&gt;update page in place&lt;/td&gt;
&lt;td&gt;memtable, then flush sorted file&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Disk write pattern&lt;/td&gt;
&lt;td&gt;random&lt;/td&gt;
&lt;td&gt;sequential&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Write throughput&lt;/td&gt;
&lt;td&gt;good&lt;/td&gt;
&lt;td&gt;usually higher&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Point read&lt;/td&gt;
&lt;td&gt;a few pages, predictable&lt;/td&gt;
&lt;td&gt;memtable + files, Bloom filters help&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Range scan&lt;/td&gt;
&lt;td&gt;very good&lt;/td&gt;
&lt;td&gt;good, merges files&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Update / delete&lt;/td&gt;
&lt;td&gt;change the entry&lt;/td&gt;
&lt;td&gt;write new version / tombstone&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Space overhead&lt;/td&gt;
&lt;td&gt;free space in pages&lt;/td&gt;
&lt;td&gt;old versions until compaction&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Background work&lt;/td&gt;
&lt;td&gt;vacuum, page cleanup&lt;/td&gt;
&lt;td&gt;compaction&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Latency spikes from&lt;/td&gt;
&lt;td&gt;checkpoints, page splits&lt;/td&gt;
&lt;td&gt;compaction, write stalls&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  The amplification view
&lt;/h2&gt;

&lt;p&gt;Engineers describe the trade-off with three ratios:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Write amplification&lt;/strong&gt; (disk bytes written per byte you write). B-trees pay with full-page writes; LSM-trees pay by rewriting data during compaction.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Read amplification&lt;/strong&gt; (work per read). Low and steady in B-trees; higher in LSM-trees, reduced by Bloom filters and caching.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space amplification&lt;/strong&gt; (disk used versus live data). B-trees waste partly empty pages; LSM-trees keep obsolete versions until compaction, but compress well.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;No design wins all three. LSM compaction strategies (leveled, size-tiered and others) shift the balance between them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common mistakes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;"LSM is faster."&lt;/strong&gt; It is usually faster for writes. For read-heavy work with many range scans, a B-tree is often the better fit.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ignoring tombstones.&lt;/strong&gt; In an LSM engine, deleting millions of keys does not free space right away, and scans slow down while they skip the markers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Judging by a short benchmark.&lt;/strong&gt; Compaction debt and B-tree bloat build up over hours or days. Test with realistic data volumes and durations.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  When to pick which
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Pick a B-tree engine&lt;/strong&gt; for general transactional workloads, read-heavy applications, frequent range scans and &lt;code&gt;ORDER BY&lt;/code&gt; on indexed columns, and when predictable latency matters more than peak write throughput. That covers most business applications.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pick an LSM engine&lt;/strong&gt; for write-heavy ingestion: logs, events, metrics, time series, message storage, and large key-value datasets where recent data is read most. Also when you want good compression.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Watch compaction&lt;/strong&gt; if you run LSM: give it disk bandwidth, monitor pending compaction, and be careful with mass deletes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Watch bloat&lt;/strong&gt; if you run a B-tree engine: keep vacuum healthy and check index size on hot tables.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Remember the layer above matters too.&lt;/strong&gt; Many databases put a full SQL layer on top of an LSM engine, so the choice of storage engine does not decide the query language.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://abdeldjalilchougui.biz/blog/b-tree-vs-lsm-tree-cheat-sheet" rel="noopener noreferrer"&gt;abdeldjalilchougui.biz&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>database</category>
      <category>backend</category>
      <category>cheatsheets</category>
      <category>systemdesign</category>
    </item>
    <item>
      <title>Isolation Levels Cheat Sheet: What Each One Prevents</title>
      <dc:creator>Abdeldjalil Chougui</dc:creator>
      <pubDate>Tue, 01 Dec 2026 06:44:17 +0000</pubDate>
      <link>https://dev.to/abdeldjalilchougui/isolation-levels-cheat-sheet-what-each-one-prevents-3aph</link>
      <guid>https://dev.to/abdeldjalilchougui/isolation-levels-cheat-sheet-what-each-one-prevents-3aph</guid>
      <description>&lt;p&gt;Every SQL database lets you choose how isolated concurrent transactions are from each other. The four standard levels trade safety for concurrency. This cheat sheet lists what each level prevents, what it still allows, how PostgreSQL and MySQL actually behave, and when to use which.&lt;/p&gt;

&lt;h2&gt;
  
  
  The anomalies in one line each
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Dirty read:&lt;/strong&gt; you see another transaction's uncommitted change, which may be rolled back.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Non-repeatable read:&lt;/strong&gt; you read the same row twice in one transaction and get different values.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Phantom read:&lt;/strong&gt; you run the same filtered query twice and new rows appear or disappear.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lost update:&lt;/strong&gt; two transactions read the same value and write back, and one write erases the other.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Write skew:&lt;/strong&gt; two transactions check the same rule, each changes a different row, and together they break the rule.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The four levels
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Read Uncommitted.&lt;/strong&gt; The weakest level in the standard. Dirty reads are allowed. In practice it is rarely useful, and PostgreSQL does not even implement it: asking for it gives you Read Committed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Read Committed.&lt;/strong&gt; You only ever see committed data, but each statement can see a newer committed state than the previous one. The default in PostgreSQL, SQL Server and Oracle. Good enough for many simple request handlers if you write updates carefully.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Repeatable Read.&lt;/strong&gt; Rows you have read do not change under you for the whole transaction. The default in MySQL InnoDB. In PostgreSQL it is snapshot isolation: the whole transaction sees one snapshot, phantoms are prevented too, and a concurrent update to a row you also update causes a serialization error instead of a lost update.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Serializable.&lt;/strong&gt; The result is equivalent to running the transactions one at a time. Every anomaly above is prevented. The price is more blocking (lock-based engines) or more aborted transactions that must be retried (PostgreSQL's Serializable Snapshot Isolation).&lt;/p&gt;

&lt;h2&gt;
  
  
  What the standard requires
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Level&lt;/th&gt;
&lt;th&gt;Dirty read&lt;/th&gt;
&lt;th&gt;Non-repeatable read&lt;/th&gt;
&lt;th&gt;Phantom read&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Read Uncommitted&lt;/td&gt;
&lt;td&gt;allowed&lt;/td&gt;
&lt;td&gt;allowed&lt;/td&gt;
&lt;td&gt;allowed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Read Committed&lt;/td&gt;
&lt;td&gt;prevented&lt;/td&gt;
&lt;td&gt;allowed&lt;/td&gt;
&lt;td&gt;allowed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Repeatable Read&lt;/td&gt;
&lt;td&gt;prevented&lt;/td&gt;
&lt;td&gt;prevented&lt;/td&gt;
&lt;td&gt;allowed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Serializable&lt;/td&gt;
&lt;td&gt;prevented&lt;/td&gt;
&lt;td&gt;prevented&lt;/td&gt;
&lt;td&gt;prevented&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The standard sets a minimum. Real databases often prevent more.&lt;/p&gt;

&lt;h2&gt;
  
  
  What you actually get (PostgreSQL and MySQL InnoDB)
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Level&lt;/th&gt;
&lt;th&gt;Lost update&lt;/th&gt;
&lt;th&gt;Write skew&lt;/th&gt;
&lt;th&gt;Notes&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Read Committed&lt;/td&gt;
&lt;td&gt;possible&lt;/td&gt;
&lt;td&gt;possible&lt;/td&gt;
&lt;td&gt;new snapshot per statement&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Repeatable Read (PostgreSQL)&lt;/td&gt;
&lt;td&gt;prevented, by error&lt;/td&gt;
&lt;td&gt;possible&lt;/td&gt;
&lt;td&gt;one snapshot per transaction&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Repeatable Read (InnoDB)&lt;/td&gt;
&lt;td&gt;possible with plain reads&lt;/td&gt;
&lt;td&gt;possible&lt;/td&gt;
&lt;td&gt;locking reads see latest data&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Serializable&lt;/td&gt;
&lt;td&gt;prevented&lt;/td&gt;
&lt;td&gt;prevented&lt;/td&gt;
&lt;td&gt;retries or more locking&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;At InnoDB's Repeatable Read, a plain &lt;code&gt;SELECT&lt;/code&gt; reads the snapshot, but &lt;code&gt;UPDATE&lt;/code&gt; and &lt;code&gt;SELECT ... FOR UPDATE&lt;/code&gt; read the latest committed row and take locks, including gap locks against phantoms. That mix surprises people: a read-then-write in application code can still lose an update unless the read is a locking read.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting the level
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- PostgreSQL: one transaction&lt;/span&gt;
&lt;span class="k"&gt;BEGIN&lt;/span&gt; &lt;span class="no"&gt;ISOLATION&lt;/span&gt; &lt;span class="no"&gt;LEVEL&lt;/span&gt; &lt;span class="no"&gt;SERIALIZABLE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="k"&gt;COMMIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- MySQL: the next transaction in this session&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="k"&gt;TRANSACTION&lt;/span&gt; &lt;span class="no"&gt;ISOLATION&lt;/span&gt; &lt;span class="no"&gt;LEVEL&lt;/span&gt; &lt;span class="no"&gt;READ&lt;/span&gt; &lt;span class="no"&gt;COMMITTED&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="no"&gt;START&lt;/span&gt; &lt;span class="k"&gt;TRANSACTION&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="k"&gt;COMMIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Most frameworks and ORMs keep the database default unless you configure the level on the connection or the transaction.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tools that work at any level
&lt;/h2&gt;

&lt;p&gt;You do not always need a stricter level:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Atomic updates:&lt;/strong&gt; &lt;code&gt;UPDATE stock SET qty = qty - 1 WHERE id = 7 AND qty &amp;gt; 0&lt;/code&gt; cannot lose an update.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Row locks:&lt;/strong&gt; &lt;code&gt;SELECT ... FOR UPDATE&lt;/code&gt; before a read-modify-write.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Constraints:&lt;/strong&gt; a &lt;code&gt;UNIQUE&lt;/code&gt; index or a &lt;code&gt;CHECK&lt;/code&gt; constraint stops invalid states at any level.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Optimistic locking:&lt;/strong&gt; a &lt;code&gt;version&lt;/code&gt; column checked in the &lt;code&gt;UPDATE&lt;/code&gt;'s &lt;code&gt;WHERE&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  When to pick which
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Read Uncommitted:&lt;/strong&gt; practically never. If you want approximate numbers fast, use a replica or a summary table instead.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Read Committed:&lt;/strong&gt; the default for most web request handlers. Combine it with atomic updates, &lt;code&gt;FOR UPDATE&lt;/code&gt; or constraints for the few places where races matter.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Repeatable Read:&lt;/strong&gt; reports, exports and multi-query reads that must see one consistent picture. In PostgreSQL, also a cheap way to turn lost updates into retryable errors.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Serializable:&lt;/strong&gt; complex check-then-write rules (bookings, limits, on-call schedules) where reasoning about every race by hand is risky. Always pair it with a retry loop, and keep those transactions short.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://abdeldjalilchougui.biz/blog/isolation-levels-cheat-sheet-what-each-one-prevents" rel="noopener noreferrer"&gt;abdeldjalilchougui.biz&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>database</category>
      <category>backend</category>
      <category>cheatsheets</category>
      <category>sql</category>
    </item>
    <item>
      <title>Every SQL JOIN in One Picture</title>
      <dc:creator>Abdeldjalil Chougui</dc:creator>
      <pubDate>Tue, 01 Dec 2026 06:42:17 +0000</pubDate>
      <link>https://dev.to/abdeldjalilchougui/every-sql-join-in-one-picture-3a43</link>
      <guid>https://dev.to/abdeldjalilchougui/every-sql-join-in-one-picture-3a43</guid>
      <description>&lt;p&gt;JOINs are easier to remember as a set than one at a time. They all answer the same question, "which rows from table A go with which rows from table B?", and differ only in what they do with rows that find no partner. This cheat sheet puts them side by side on one small example.&lt;/p&gt;

&lt;h2&gt;
  
  
  The example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Amina&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Karim&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Sara&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;101&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;customer&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;102&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;customer&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;103&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;customer&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;104&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;customer&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sara has no orders. Order 104 points to a customer that does not exist. Every join below uses &lt;code&gt;ON o.customer_id = c.id&lt;/code&gt;, with &lt;code&gt;customers&lt;/code&gt; on the left and &lt;code&gt;orders&lt;/code&gt; on the right.&lt;/p&gt;

&lt;p&gt;Picture two overlapping circles: the left circle is &lt;code&gt;customers&lt;/code&gt;, the right one is &lt;code&gt;orders&lt;/code&gt;, and the overlap is the matched pairs. Each join is a different shaded area. (The circle picture is a memory aid; remember that a match can produce several rows, as Amina's two orders show.)&lt;/p&gt;

&lt;h2&gt;
  
  
  The joins at a glance
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;INNER JOIN: the overlap.&lt;/strong&gt; Only matched pairs. Returns Amina-101, Amina-102, Karim-103. Sara and order 104 are dropped.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;LEFT JOIN: the whole left circle.&lt;/strong&gt; Every customer, plus matching orders. Returns the three matches and Sara with &lt;code&gt;NULL&lt;/code&gt; order columns.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;RIGHT JOIN: the whole right circle.&lt;/strong&gt; Every order, plus matching customers. Returns the three matches and order 104 with a &lt;code&gt;NULL&lt;/code&gt; customer. Usually written as a &lt;code&gt;LEFT JOIN&lt;/code&gt; with the tables swapped.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;FULL OUTER JOIN: both circles.&lt;/strong&gt; Matches, plus Sara with &lt;code&gt;NULL&lt;/code&gt; orders, plus order 104 with a &lt;code&gt;NULL&lt;/code&gt; customer. Not supported natively in MySQL.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;LEFT anti join: left circle minus the overlap.&lt;/strong&gt; Customers with no orders: Sara. Written as &lt;code&gt;LEFT JOIN ... WHERE o.id IS NULL&lt;/code&gt; or, more clearly, &lt;code&gt;NOT EXISTS&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CROSS JOIN: no circles, every pair.&lt;/strong&gt; Three customers times four orders gives twelve rows. No &lt;code&gt;ON&lt;/code&gt; condition.&lt;/p&gt;

&lt;h2&gt;
  
  
  Side by side
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Join&lt;/th&gt;
&lt;th&gt;Keeps unmatched left&lt;/th&gt;
&lt;th&gt;Keeps unmatched right&lt;/th&gt;
&lt;th&gt;Rows here&lt;/th&gt;
&lt;th&gt;Typical question&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;INNER&lt;/td&gt;
&lt;td&gt;no&lt;/td&gt;
&lt;td&gt;no&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;orders with their customer&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;LEFT&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;no&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;all customers, orders if any&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;RIGHT&lt;/td&gt;
&lt;td&gt;no&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;all orders, customer if any&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;FULL OUTER&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;yes&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;reconcile two lists&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;LEFT anti&lt;/td&gt;
&lt;td&gt;only unmatched&lt;/td&gt;
&lt;td&gt;no&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;customers who never ordered&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CROSS&lt;/td&gt;
&lt;td&gt;n/a&lt;/td&gt;
&lt;td&gt;n/a&lt;/td&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;td&gt;every combination&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  The syntax in one block
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="k"&gt;INNER&lt;/span&gt; &lt;span class="k"&gt;JOIN&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;ON&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;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&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="k"&gt;SELECT&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="k"&gt;LEFT&lt;/span&gt; &lt;span class="k"&gt;JOIN&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;ON&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;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&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="k"&gt;SELECT&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="k"&gt;RIGHT&lt;/span&gt; &lt;span class="k"&gt;JOIN&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;ON&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;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&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="k"&gt;SELECT&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="k"&gt;FULL&lt;/span&gt; &lt;span class="k"&gt;JOIN&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;ON&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;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&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="k"&gt;SELECT&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="n"&gt;c&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;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;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&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;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;sizes&lt;/span&gt; &lt;span class="k"&gt;CROSS&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;colors&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;JOIN&lt;/code&gt; alone means &lt;code&gt;INNER JOIN&lt;/code&gt;, and &lt;code&gt;LEFT JOIN&lt;/code&gt; is short for &lt;code&gt;LEFT OUTER JOIN&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Traps that apply to all of them
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A condition on the optional side belongs in &lt;code&gt;ON&lt;/code&gt;.&lt;/strong&gt; Putting &lt;code&gt;WHERE o.total &amp;gt; 1000&lt;/code&gt; after a &lt;code&gt;LEFT JOIN&lt;/code&gt; removes the &lt;code&gt;NULL&lt;/code&gt; rows and turns it into an inner join.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;One-to-many multiplies rows.&lt;/strong&gt; Amina appears twice. Joining two one-to-many tables at once multiplies again and breaks &lt;code&gt;SUM&lt;/code&gt; and &lt;code&gt;COUNT&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;COUNT(*)&lt;/code&gt; counts the &lt;code&gt;NULL&lt;/code&gt; row.&lt;/strong&gt; Use &lt;code&gt;COUNT(o.id)&lt;/code&gt; to count zero orders for Sara.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;NULL&lt;/code&gt; keys never match.&lt;/strong&gt; Rows with a &lt;code&gt;NULL&lt;/code&gt; in the join column are silently dropped from inner joins.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Index the foreign key.&lt;/strong&gt; An index on &lt;code&gt;orders.customer_id&lt;/code&gt; lets the database find matches without scanning the whole table.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  When to pick which
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;INNER JOIN&lt;/strong&gt; when both sides must exist: an order list showing customer names, where an order without a customer is an error you do not want to display.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;LEFT JOIN&lt;/strong&gt; for "every X, with its Y if it has one": all customers with their last order, all products with their stock.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;RIGHT JOIN&lt;/strong&gt; rarely; rewrite it as a &lt;code&gt;LEFT JOIN&lt;/code&gt; so the main table comes first.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;FULL OUTER JOIN&lt;/strong&gt; for comparisons: two imports, two systems, expected versus actual, where you need what is missing on both sides.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Anti join (&lt;code&gt;NOT EXISTS&lt;/code&gt;)&lt;/strong&gt; for "X without Y": customers who never ordered, users who never logged in, invoices with no payment.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CROSS JOIN&lt;/strong&gt; to generate combinations on purpose, such as every size with every colour, or every day with every store for a report grid.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://abdeldjalilchougui.biz/blog/every-sql-join-in-one-picture" rel="noopener noreferrer"&gt;abdeldjalilchougui.biz&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>database</category>
      <category>backend</category>
      <category>cheatsheets</category>
      <category>sql</category>
    </item>
    <item>
      <title>SQL vs NoSQL Cheat Sheet</title>
      <dc:creator>Abdeldjalil Chougui</dc:creator>
      <pubDate>Tue, 01 Dec 2026 06:40:16 +0000</pubDate>
      <link>https://dev.to/abdeldjalilchougui/sql-vs-nosql-cheat-sheet-39gl</link>
      <guid>https://dev.to/abdeldjalilchougui/sql-vs-nosql-cheat-sheet-39gl</guid>
      <description>&lt;p&gt;"NoSQL" is not one kind of database, so comparing "SQL vs NoSQL" as two options hides most of the useful detail. This cheat sheet puts the relational model next to the three NoSQL families you are most likely to meet as a backend developer: document stores, key-value stores and wide-column stores. Graph databases are a fourth family, worth a look when your queries mostly follow relationships many hops deep.&lt;/p&gt;

&lt;h2&gt;
  
  
  The four options
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Relational (SQL).&lt;/strong&gt; PostgreSQL, MySQL, SQLite, SQL Server. Tables with typed columns, a schema enforced on write, joins between tables, and ACID transactions across any rows. The query language is expressive enough to answer questions nobody planned for.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Document store.&lt;/strong&gt; MongoDB, Couchbase, Firestore. Each record is a JSON-like document that can nest objects and arrays. Data that is read together is stored together, so a typical page load is a single lookup. The schema is flexible, which helps early and needs discipline later.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key-value store.&lt;/strong&gt; Redis, etcd, Memcached, and DynamoDB in its simplest form. You put a value under a key and get it back by that key. Very fast and simple, but you cannot ask "which values match this condition" unless you build that yourself.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Wide-column store.&lt;/strong&gt; Cassandra, ScyllaDB, HBase. Rows are grouped by a partition key and spread across many nodes. Writes are cheap and scale out by adding machines. You design tables around the queries you will run, often duplicating data across several tables.&lt;/p&gt;

&lt;h2&gt;
  
  
  Side by side
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Relational&lt;/th&gt;
&lt;th&gt;Document&lt;/th&gt;
&lt;th&gt;Key-value&lt;/th&gt;
&lt;th&gt;Wide-column&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Data model&lt;/td&gt;
&lt;td&gt;tables, rows&lt;/td&gt;
&lt;td&gt;JSON documents&lt;/td&gt;
&lt;td&gt;key to value&lt;/td&gt;
&lt;td&gt;partitions of rows&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Schema&lt;/td&gt;
&lt;td&gt;enforced on write&lt;/td&gt;
&lt;td&gt;flexible&lt;/td&gt;
&lt;td&gt;none&lt;/td&gt;
&lt;td&gt;defined per table, query-first&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Querying&lt;/td&gt;
&lt;td&gt;full SQL, joins&lt;/td&gt;
&lt;td&gt;rich filters, no real joins&lt;/td&gt;
&lt;td&gt;by key only&lt;/td&gt;
&lt;td&gt;by partition key, limited&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Transactions&lt;/td&gt;
&lt;td&gt;ACID, any rows&lt;/td&gt;
&lt;td&gt;multi-document, with limits&lt;/td&gt;
&lt;td&gt;per key, some atomic ops&lt;/td&gt;
&lt;td&gt;per partition, lightweight&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Scaling&lt;/td&gt;
&lt;td&gt;vertical, replicas, sharding is work&lt;/td&gt;
&lt;td&gt;sharding built in&lt;/td&gt;
&lt;td&gt;sharding built in&lt;/td&gt;
&lt;td&gt;built for many nodes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Typical use&lt;/td&gt;
&lt;td&gt;business data, money&lt;/td&gt;
&lt;td&gt;catalogs, content&lt;/td&gt;
&lt;td&gt;cache, sessions&lt;/td&gt;
&lt;td&gt;events, time series&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A few cells need nuance. MongoDB supports multi-document ACID transactions, and Redis offers atomic commands, &lt;code&gt;MULTI&lt;/code&gt;/&lt;code&gt;EXEC&lt;/code&gt; and Lua scripts. Cassandra lets you tune consistency per query, from a single replica to a quorum. And PostgreSQL's &lt;code&gt;JSONB&lt;/code&gt; type lets a relational database store and index document-style data.&lt;/p&gt;

&lt;h2&gt;
  
  
  What each one makes easy and hard
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Relational.&lt;/strong&gt; Easy: ad-hoc questions, reports, keeping data correct with constraints, transactions that span many tables. Hard: very high write volume across many machines, and data whose shape changes constantly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Document.&lt;/strong&gt; Easy: loading a whole object in one read, evolving fields without migrations, storing records with varied attributes. Hard: many-to-many relationships, queries that cut across documents, keeping duplicated data in sync.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key-value.&lt;/strong&gt; Easy: caching, sessions, rate limiting counters, feature flags, anything accessed by an exact key. Hard: anything else. Many key-value stores (Redis in particular) keep data in memory, so size and persistence settings matter.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Wide-column.&lt;/strong&gt; Easy: heavy, continuous writes, data spread across regions, queries that always start from a known partition key ("all readings from sensor 17 today"). Hard: ad-hoc queries, joins, and changing access patterns after launch.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common mistakes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Picking NoSQL for scale you do not have yet, and losing joins and constraints you need now.&lt;/li&gt;
&lt;li&gt;Modelling relational data as documents, then rebuilding joins in application code.&lt;/li&gt;
&lt;li&gt;Using a key-value cache as the only copy of important data without understanding its persistence settings.&lt;/li&gt;
&lt;li&gt;Designing Cassandra tables like SQL tables. Partition keys and query patterns come first.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  When to pick which
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Pick relational&lt;/strong&gt; as the default for business applications: users, orders, payments, inventory, anything where correctness and flexible querying matter. Add &lt;code&gt;JSONB&lt;/code&gt; columns for the semi-structured parts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pick a document store&lt;/strong&gt; when each record is naturally a self-contained object (product catalog, CMS content, user profiles with varied fields) and most reads fetch one object at a time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pick a key-value store&lt;/strong&gt; next to your main database for caching, sessions, queues and counters. It is rarely the only database in a system.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pick a wide-column store&lt;/strong&gt; when you have very high write throughput, data that must spread across many nodes or regions, and queries you can define in advance.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mix them&lt;/strong&gt; when needed. A common shape: PostgreSQL as the source of truth, Redis as the cache, and a specialised store only for the workload that truly needs it.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://abdeldjalilchougui.biz/blog/sql-vs-nosql-cheat-sheet" rel="noopener noreferrer"&gt;abdeldjalilchougui.biz&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>database</category>
      <category>backend</category>
      <category>cheatsheets</category>
      <category>sql</category>
    </item>
    <item>
      <title>MVCC: How Databases Let Readers and Writers Coexist</title>
      <dc:creator>Abdeldjalil Chougui</dc:creator>
      <pubDate>Tue, 01 Dec 2026 02:48:04 +0000</pubDate>
      <link>https://dev.to/abdeldjalilchougui/mvcc-how-databases-let-readers-and-writers-coexist-15k9</link>
      <guid>https://dev.to/abdeldjalilchougui/mvcc-how-databases-let-readers-and-writers-coexist-15k9</guid>
      <description>&lt;p&gt;Imagine a report that reads every order from the last year. It takes thirty seconds. Meanwhile, customers keep placing and updating orders. With simple locking, you get a bad choice: either the report blocks all writes for thirty seconds, or writers block the report, or the report sees a mix of old and new data that never existed at any single moment.&lt;/p&gt;

&lt;p&gt;Multi-version concurrency control (MVCC) avoids that choice. PostgreSQL, MySQL's InnoDB, Oracle, SQL Server (with snapshot isolation enabled) and many newer databases use it. This article explains how it works, using PostgreSQL's design as the main example, and what it costs.&lt;/p&gt;

&lt;h2&gt;
  
  
  The core idea
&lt;/h2&gt;

&lt;p&gt;Instead of overwriting a row in place, the database keeps several versions of it. A writer creates a new version. A reader looks at the version that was current at the moment its snapshot was taken, and ignores newer ones.&lt;/p&gt;

&lt;p&gt;The result is the property MVCC is known for: &lt;strong&gt;readers do not block writers, and writers do not block readers.&lt;/strong&gt; Two writers changing the same row still have to wait for each other, but most workloads are dominated by reads.&lt;/p&gt;

&lt;h2&gt;
  
  
  Snapshots
&lt;/h2&gt;

&lt;p&gt;Every transaction has an ID, assigned in increasing order. When a transaction (or, at Read Committed, each statement) begins, the database takes a snapshot. Roughly, a snapshot records:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;which transactions had already committed,&lt;/li&gt;
&lt;li&gt;which were still running,&lt;/li&gt;
&lt;li&gt;and that anything that starts later is invisible.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A row version is visible to a snapshot only if the transaction that created it had committed when the snapshot was taken, and the transaction that deleted or replaced it (if any) had not.&lt;/p&gt;

&lt;h2&gt;
  
  
  Row versions in PostgreSQL
&lt;/h2&gt;

&lt;p&gt;In PostgreSQL, each row version (a tuple) carries two hidden fields:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;xmin&lt;/code&gt;: the ID of the transaction that created this version,&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;xmax&lt;/code&gt;: the ID of the transaction that deleted or replaced it, or empty if it is still current.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can see them:&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;SELECT&lt;/span&gt; &lt;span class="n"&gt;xmin&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;xmax&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="n"&gt;balance&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;accounts&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An &lt;code&gt;UPDATE&lt;/code&gt; does not change the existing tuple's data. It sets &lt;code&gt;xmax&lt;/code&gt; on the old version and inserts a new version with &lt;code&gt;xmin&lt;/code&gt; set to the updating transaction. A &lt;code&gt;DELETE&lt;/code&gt; only sets &lt;code&gt;xmax&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  A worked example
&lt;/h2&gt;

&lt;p&gt;Account 1 has a balance of 100, created long ago by transaction 50.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Transaction 200 starts a long report at Repeatable Read. Its snapshot says: everything up to 199 is committed, nothing newer is visible.&lt;/li&gt;
&lt;li&gt;Transaction 201 runs &lt;code&gt;UPDATE accounts SET balance = 80 WHERE id = 1&lt;/code&gt; and commits. Now there are two versions:

&lt;ul&gt;
&lt;li&gt;version A: balance 100, &lt;code&gt;xmin&lt;/code&gt; 50, &lt;code&gt;xmax&lt;/code&gt; 201&lt;/li&gt;
&lt;li&gt;version B: balance 80, &lt;code&gt;xmin&lt;/code&gt; 201, &lt;code&gt;xmax&lt;/code&gt; empty&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Transaction 200 reads account 1. Version A was created by 50 (visible) and deleted by 201, which is not in its snapshot, so the deletion does not count: it sees 100. Version B was created by 201: invisible.&lt;/li&gt;
&lt;li&gt;A new transaction 202 reads account 1. 201 has committed, so version A is deleted and version B is visible: it sees 80.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Transaction 200 never waited, and transaction 201 never waited. Each saw a consistent picture.&lt;/p&gt;

&lt;h2&gt;
  
  
  What happens when two writers collide
&lt;/h2&gt;

&lt;p&gt;MVCC does not remove write conflicts. If transaction T1 updates a row and T2 tries to update the same row before T1 commits, T2 waits on T1's row lock. What happens next depends on the isolation level:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;At &lt;strong&gt;Read Committed&lt;/strong&gt;, after T1 commits, PostgreSQL re-checks the row's newest version against T2's &lt;code&gt;WHERE&lt;/code&gt; condition and applies T2's update to it.&lt;/li&gt;
&lt;li&gt;At &lt;strong&gt;Repeatable Read&lt;/strong&gt; and &lt;strong&gt;Serializable&lt;/strong&gt;, if T1 committed, T2 fails with a serialization error ("could not serialize access due to concurrent update"), and the application should retry.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Other designs
&lt;/h2&gt;

&lt;p&gt;Not every engine stores versions the same way.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;PostgreSQL&lt;/strong&gt; keeps old and new versions side by side in the table itself.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;InnoDB&lt;/strong&gt; and &lt;strong&gt;Oracle&lt;/strong&gt; update the row in place and keep the previous versions in an undo log. A reader that needs an older version follows pointers into the undo log and reconstructs it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The visible behaviour is similar. The cost shows up in different places: table bloat in PostgreSQL, undo log growth and longer reconstruction in InnoDB when old snapshots stay open.&lt;/p&gt;

&lt;h2&gt;
  
  
  The cost: garbage
&lt;/h2&gt;

&lt;p&gt;Old versions must eventually go away. A version can be removed when no running transaction could still need to see it.&lt;/p&gt;

&lt;p&gt;In PostgreSQL, that cleanup is &lt;code&gt;VACUUM&lt;/code&gt;, normally run by the autovacuum background process. It marks dead tuples' space as reusable. It also handles transaction ID wraparound: transaction IDs are 32-bit counters, and vacuum "freezes" old rows so their IDs can be safely reused.&lt;/p&gt;

&lt;p&gt;In InnoDB, a purge thread removes undo records and delete-marked rows that no snapshot needs anymore.&lt;/p&gt;

&lt;p&gt;This leads to the most common MVCC problem in production: &lt;strong&gt;a long-running transaction blocks cleanup for the whole database.&lt;/strong&gt; A session left open with &lt;code&gt;BEGIN&lt;/code&gt; and never committed ("idle in transaction"), a slow analytics query or a forgotten replication slot keeps an old snapshot alive. Dead versions accumulate, tables and indexes grow, and queries slow down.&lt;/p&gt;

&lt;h2&gt;
  
  
  Practical advice
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Keep transactions short, and close them. Monitor sessions that stay "idle in transaction"; PostgreSQL has &lt;code&gt;idle_in_transaction_session_timeout&lt;/code&gt; for this.&lt;/li&gt;
&lt;li&gt;Let autovacuum run, and tune it for large, frequently updated tables instead of disabling it.&lt;/li&gt;
&lt;li&gt;Watch table and index bloat on hot tables.&lt;/li&gt;
&lt;li&gt;Prefer updates that do not touch indexed columns where you can. In PostgreSQL this allows HOT (heap-only tuple) updates, which avoid adding new index entries.&lt;/li&gt;
&lt;li&gt;Remember that MVCC is not full isolation. Snapshots prevent dirty and non-repeatable reads, but anomalies like write skew still need Serializable, locks or constraints.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  In short
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;MVCC keeps several versions of each row; each transaction reads from a snapshot.&lt;/li&gt;
&lt;li&gt;Readers never block writers, and writers never block readers. Writers on the same row still conflict.&lt;/li&gt;
&lt;li&gt;PostgreSQL stores versions in the table with &lt;code&gt;xmin&lt;/code&gt; and &lt;code&gt;xmax&lt;/code&gt;; InnoDB and Oracle rebuild old versions from undo logs.&lt;/li&gt;
&lt;li&gt;Old versions must be cleaned up by vacuum or purge, and long transactions stop that cleanup.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://abdeldjalilchougui.biz/blog/mvcc-how-databases-let-readers-and-writers-coexist" rel="noopener noreferrer"&gt;abdeldjalilchougui.biz&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>database</category>
      <category>backend</category>
      <category>mvcc</category>
    </item>
    <item>
      <title>Write-Ahead Logging: How Databases Survive Crashes</title>
      <dc:creator>Abdeldjalil Chougui</dc:creator>
      <pubDate>Tue, 01 Dec 2026 02:46:04 +0000</pubDate>
      <link>https://dev.to/abdeldjalilchougui/write-ahead-logging-how-databases-survive-crashes-5f6p</link>
      <guid>https://dev.to/abdeldjalilchougui/write-ahead-logging-how-databases-survive-crashes-5f6p</guid>
      <description>&lt;p&gt;A database promises that once it says "committed", your data is safe, even if the power goes out a millisecond later. At the same time, it keeps most of its data in memory and writes pages to disk lazily, because writing every changed page immediately would be far too slow. Those two facts seem to contradict each other. The write-ahead log (WAL) is how databases make them fit together.&lt;/p&gt;

&lt;p&gt;This article explains the problem, how the WAL works step by step, what happens during crash recovery, and the settings that affect it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem
&lt;/h2&gt;

&lt;p&gt;Table and index data live in pages, typically 8 KB or 16 KB. When you update a row, the database changes the page in its in-memory buffer pool. The page is now "dirty": newer in memory than on disk.&lt;/p&gt;

&lt;p&gt;Writing that page to disk on every commit has three problems:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;It is slow.&lt;/strong&gt; A transaction may touch pages scattered across many files. Forcing each one to disk means many random writes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It is wasteful.&lt;/strong&gt; Changing a 50-byte value rewrites a whole page.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It is not atomic.&lt;/strong&gt; A transaction that touches ten pages could crash after writing four of them. And a single page write can itself be interrupted halfway (a torn page).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If the database just kept everything in memory instead, a crash would lose every committed change since the last write.&lt;/p&gt;

&lt;h2&gt;
  
  
  The idea: log first, data later
&lt;/h2&gt;

&lt;p&gt;The rule behind the WAL is simple: &lt;strong&gt;before a change to a data page reaches disk, a record describing that change must already be safely on disk in the log.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The log is an append-only file (in practice a series of segment files). Each record describes a change, such as "in page 812 of table &lt;code&gt;accounts&lt;/code&gt;, set this row to these bytes", and gets a position in the log called a log sequence number (LSN).&lt;/p&gt;

&lt;p&gt;Appending to the end of one file is a sequential write, which is much faster than scattered page writes. Several transactions committing at the same time can share one flush (group commit).&lt;/p&gt;

&lt;h2&gt;
  
  
  A commit, step by step
&lt;/h2&gt;

&lt;p&gt;Take &lt;code&gt;UPDATE accounts SET balance = 900 WHERE id = 1; COMMIT;&lt;/code&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The database finds the page holding account 1 and loads it into the buffer pool if needed.&lt;/li&gt;
&lt;li&gt;It creates a WAL record describing the change and appends it to the in-memory WAL buffer.&lt;/li&gt;
&lt;li&gt;It modifies the page in memory and marks it dirty, stamped with the LSN of the record.&lt;/li&gt;
&lt;li&gt;On &lt;code&gt;COMMIT&lt;/code&gt;, it appends a commit record and flushes the WAL up to that point to disk, using &lt;code&gt;fsync&lt;/code&gt; or an equivalent call to make sure the data is on stable storage, not just in an operating system cache.&lt;/li&gt;
&lt;li&gt;Only then does the client get "COMMIT" back.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The data page itself is still only in memory. It will be written later, by a background writer or at a checkpoint. Before that page is written, the database checks that the WAL is flushed at least up to the page's LSN. That is the "write-ahead" rule in action.&lt;/p&gt;

&lt;h2&gt;
  
  
  Crash recovery
&lt;/h2&gt;

&lt;p&gt;The power fails. On restart, memory is gone. Disk contains:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;data pages, some up to date, some old, written at various times;&lt;/li&gt;
&lt;li&gt;the WAL, containing every committed change, in order.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Recovery replays the log. It starts from the last checkpoint and, for each record, checks whether the page on disk already includes that change (by comparing the page's LSN with the record's LSN). If not, it reapplies it. This is the redo phase.&lt;/p&gt;

&lt;p&gt;What about transactions that were running but never committed? The handling depends on the engine:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In &lt;strong&gt;PostgreSQL&lt;/strong&gt;, every row version records which transaction created it. After recovery, a transaction with no commit record is treated as aborted, so its row versions are simply invisible and are cleaned up later. No undo phase is needed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;InnoDB&lt;/strong&gt; keeps a separate undo log. After redo, it rolls back the changes of uncommitted transactions using that undo information.&lt;/li&gt;
&lt;li&gt;The classic ARIES recovery algorithm, which many databases follow in some form, has analysis, redo and undo phases.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Either way, the result is that committed transactions survive and uncommitted ones leave no trace.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checkpoints
&lt;/h2&gt;

&lt;p&gt;Without help, the log would grow forever and recovery would replay it from the beginning. Checkpoints prevent that. During a checkpoint, the database writes all dirty pages to disk and records the checkpoint's position in the log. After that, older log segments are no longer needed for crash recovery and can be recycled or archived.&lt;/p&gt;

&lt;p&gt;Checkpoints are a trade-off. Frequent checkpoints mean faster recovery but more page writes. Rare checkpoints mean less write work but a longer replay after a crash. PostgreSQL also writes a full copy of a page into the WAL the first time it changes after a checkpoint (full-page writes), which protects against torn pages.&lt;/p&gt;

&lt;h2&gt;
  
  
  The WAL does more than recovery
&lt;/h2&gt;

&lt;p&gt;Because the log contains every change in order, it is useful beyond crashes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Replication.&lt;/strong&gt; Replicas receive the WAL stream and replay it to stay in sync. PostgreSQL streaming replication works this way.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Point-in-time recovery.&lt;/strong&gt; Keep a base backup plus archived WAL segments, and you can restore the database as it was at any moment, for example just before someone ran &lt;code&gt;DELETE&lt;/code&gt; without a &lt;code&gt;WHERE&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Change data capture.&lt;/strong&gt; Tools can decode the log into a stream of row changes to feed search indexes, caches or analytics. MySQL's binary log is a separate log used for replication and similar purposes, alongside InnoDB's own redo log.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Settings worth knowing
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Synchronous commit.&lt;/strong&gt; PostgreSQL's &lt;code&gt;synchronous_commit = off&lt;/code&gt; returns before the WAL flush. A crash can lose the last few moments of commits, but the database stays consistent. InnoDB's &lt;code&gt;innodb_flush_log_at_trx_commit&lt;/code&gt; controls a similar trade-off.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;fsync&lt;/code&gt; must stay on.&lt;/strong&gt; Turning off flushing entirely (PostgreSQL's &lt;code&gt;fsync = off&lt;/code&gt;) can corrupt the database after a crash, not just lose recent data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Storage that lies.&lt;/strong&gt; Disks or controllers with volatile write caches that acknowledge writes before they are durable undermine everything above.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Disk space.&lt;/strong&gt; A replica that falls behind or a broken WAL archive can make WAL files pile up and fill the disk.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  In short
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Databases change pages in memory and write them lazily; the WAL makes that safe.&lt;/li&gt;
&lt;li&gt;Rule: the log record reaches disk before the data page does. A commit is durable once its log records are flushed.&lt;/li&gt;
&lt;li&gt;After a crash, replaying the log from the last checkpoint restores committed changes; uncommitted ones are discarded.&lt;/li&gt;
&lt;li&gt;The same log powers replication, point-in-time recovery and change data capture.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://abdeldjalilchougui.biz/blog/write-ahead-logging-how-databases-survive-crashes" rel="noopener noreferrer"&gt;abdeldjalilchougui.biz&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>database</category>
      <category>backend</category>
      <category>postgres</category>
    </item>
    <item>
      <title>How a Query Planner Chooses the Fastest Path</title>
      <dc:creator>Abdeldjalil Chougui</dc:creator>
      <pubDate>Tue, 01 Dec 2026 02:44:03 +0000</pubDate>
      <link>https://dev.to/abdeldjalilchougui/how-a-query-planner-chooses-the-fastest-path-1lp5</link>
      <guid>https://dev.to/abdeldjalilchougui/how-a-query-planner-chooses-the-fastest-path-1lp5</guid>
      <description>&lt;p&gt;SQL is declarative. You write what you want: "the names of customers in Oran who ordered this month". You do not write how to get it: which table to read first, whether to use an index, how to combine the rows. That decision belongs to the query planner, also called the query optimizer. The same query can run in milliseconds or minutes depending on the plan it picks.&lt;/p&gt;

&lt;p&gt;This article explains what the planner does, how it estimates costs, why it sometimes chooses badly, and how to see its choices with &lt;code&gt;EXPLAIN&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  From SQL text to a plan
&lt;/h2&gt;

&lt;p&gt;When you send a query, the database goes through a few stages:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Parse.&lt;/strong&gt; Check the syntax and build a tree that represents the query.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Analyze and rewrite.&lt;/strong&gt; Resolve table and column names, expand views, apply rewrite rules.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Plan.&lt;/strong&gt; Generate possible ways to execute the query, estimate the cost of each, and keep the cheapest.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Execute.&lt;/strong&gt; Run the chosen plan.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The plan is a tree of operators. Leaves read data (scans), inner nodes combine and transform it (joins, sorts, aggregates), and rows flow up to the root.&lt;/p&gt;

&lt;h2&gt;
  
  
  The choices the planner makes
&lt;/h2&gt;

&lt;p&gt;For each table, an &lt;strong&gt;access path&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Sequential scan:&lt;/strong&gt; read every page of the table. Cheap per row, but reads everything.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Index scan:&lt;/strong&gt; walk an index to find matching rows, then fetch each one from the table. Good when few rows match.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Index-only scan:&lt;/strong&gt; answer from the index alone when it holds all the needed columns.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bitmap scan (PostgreSQL):&lt;/strong&gt; collect matching row locations from one or more indexes, sort them by page, then read those pages in order. A middle ground for medium selectivity.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For each pair of inputs, a &lt;strong&gt;join algorithm&lt;/strong&gt;: nested loop, hash join or merge join.&lt;/p&gt;

&lt;p&gt;And a &lt;strong&gt;join order&lt;/strong&gt;. With three tables there are a handful of orders; with ten, there are millions. Join order often matters more than anything else, because joining the two most selective inputs first keeps the intermediate results small.&lt;/p&gt;

&lt;p&gt;Planners search this space with dynamic programming for a moderate number of tables, and switch to heuristics or randomized search when there are too many (PostgreSQL, for example, uses a genetic algorithm above a configurable number of tables).&lt;/p&gt;

&lt;h2&gt;
  
  
  How cost is estimated
&lt;/h2&gt;

&lt;p&gt;The planner cannot run every plan to see which is fastest. It estimates.&lt;/p&gt;

&lt;p&gt;Each operator gets a cost based on how many pages it reads and how many rows it processes. Configured constants weigh the parts: in PostgreSQL, &lt;code&gt;seq_page_cost&lt;/code&gt;, &lt;code&gt;random_page_cost&lt;/code&gt;, &lt;code&gt;cpu_tuple_cost&lt;/code&gt; and others. Random page reads are by default treated as more expensive than sequential ones, which is why an index scan that touches many scattered pages can cost more than reading the whole table in order.&lt;/p&gt;

&lt;p&gt;The key input is &lt;strong&gt;cardinality&lt;/strong&gt;: how many rows each step will produce. For that, the planner relies on statistics, gathered by &lt;code&gt;ANALYZE&lt;/code&gt; (run automatically by autovacuum in PostgreSQL, and by InnoDB's persistent statistics in MySQL):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the number of rows and pages in each table,&lt;/li&gt;
&lt;li&gt;the number of distinct values in each column,&lt;/li&gt;
&lt;li&gt;the most common values and their frequencies,&lt;/li&gt;
&lt;li&gt;a histogram of the value distribution,&lt;/li&gt;
&lt;li&gt;the fraction of &lt;code&gt;NULL&lt;/code&gt;s.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;From these, it estimates the &lt;strong&gt;selectivity&lt;/strong&gt; of each condition. If &lt;code&gt;city&lt;/code&gt; has 50 distinct values spread evenly, &lt;code&gt;WHERE city = 'Oran'&lt;/code&gt; is estimated to match 1 row in 50. Combined with the row count, that gives an estimate of rows flowing out of the scan, which feeds the cost of everything above it.&lt;/p&gt;

&lt;h2&gt;
  
  
  A worked example
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&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;total&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;
&lt;span class="k"&gt;JOIN&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;ON&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;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;city&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Oran'&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;created_at&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="s1"&gt;'2026-11-01'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Suppose &lt;code&gt;customers&lt;/code&gt; has 100,000 rows, &lt;code&gt;orders&lt;/code&gt; has 5 million, and there are indexes on &lt;code&gt;customers(city)&lt;/code&gt; and &lt;code&gt;orders(customer_id)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The planner estimates that &lt;code&gt;city = 'Oran'&lt;/code&gt; keeps about 2,000 customers. A good plan:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Index scan on &lt;code&gt;customers&lt;/code&gt; using the &lt;code&gt;city&lt;/code&gt; index: about 2,000 rows.&lt;/li&gt;
&lt;li&gt;Nested loop: for each of those customers, use the &lt;code&gt;orders(customer_id)&lt;/code&gt; index to fetch their orders.&lt;/li&gt;
&lt;li&gt;Filter those orders by &lt;code&gt;created_at&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The alternative, a hash join that reads all 5 million orders, would be much more expensive here. But if the condition were on a city holding half the customers, the hash join would win, and the planner would switch. Same SQL, different plan, because the data is different.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reading EXPLAIN
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="k"&gt;ANALYZE&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&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;total&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;
&lt;span class="k"&gt;JOIN&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;ON&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;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;city&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Oran'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PostgreSQL prints a tree. Each node shows the operator (&lt;code&gt;Index Scan&lt;/code&gt;, &lt;code&gt;Hash Join&lt;/code&gt;, &lt;code&gt;Seq Scan&lt;/code&gt;...), its estimated cost, the estimated &lt;code&gt;rows&lt;/code&gt;, and, with &lt;code&gt;ANALYZE&lt;/code&gt;, the actual rows and time. MySQL has &lt;code&gt;EXPLAIN&lt;/code&gt; and &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; too, with a different format.&lt;/p&gt;

&lt;p&gt;The single most useful habit: &lt;strong&gt;compare estimated rows with actual rows&lt;/strong&gt;. When the planner expected 10 rows and got 200,000, every decision above that node was made on wrong information. That is where slow plans usually come from.&lt;/p&gt;

&lt;p&gt;Note that &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; really runs the query. Wrap data-changing statements in a transaction and roll back.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why the planner gets it wrong
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Stale statistics.&lt;/strong&gt; A table that grew quickly since the last &lt;code&gt;ANALYZE&lt;/code&gt;. Run it after bulk loads.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Correlated columns.&lt;/strong&gt; The planner usually assumes conditions are independent. &lt;code&gt;city = 'Oran' AND postal_code = '31000'&lt;/code&gt; is estimated as two separate filters, which underestimates the result badly. PostgreSQL's &lt;code&gt;CREATE STATISTICS&lt;/code&gt; can record such dependencies.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Skewed data.&lt;/strong&gt; One customer with a million orders while most have five.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Opaque expressions.&lt;/strong&gt; Functions on columns, or values only known at run time, give the planner little to estimate from.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Generic plans for prepared statements&lt;/strong&gt;, where a plan chosen for one parameter value is reused for very different ones.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What you can do
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Keep statistics fresh, and raise the statistics target on columns with skewed distributions.&lt;/li&gt;
&lt;li&gt;Add the index the plan needs, then check that it is used.&lt;/li&gt;
&lt;li&gt;Write conditions the planner can use: no functions on indexed columns unless you have an expression index, and matching types.&lt;/li&gt;
&lt;li&gt;Rewrite the query when needed, for example by aggregating before joining.&lt;/li&gt;
&lt;li&gt;Treat hints and planner settings (where your database supports them) as a last resort: they freeze a decision that may become wrong as data changes.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  In short
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The planner generates candidate plans (access paths, join algorithms, join orders) and picks the one with the lowest estimated cost.&lt;/li&gt;
&lt;li&gt;Cost estimates depend on statistics about your data; cardinality estimates matter most.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; shows the plan; compare estimated and actual rows to find the problem.&lt;/li&gt;
&lt;li&gt;Most bad plans come from wrong estimates, and most fixes are fresher statistics, a better index or a clearer query.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://abdeldjalilchougui.biz/blog/how-a-query-planner-chooses-the-fastest-path" rel="noopener noreferrer"&gt;abdeldjalilchougui.biz&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>database</category>
      <category>backend</category>
      <category>sql</category>
    </item>
    <item>
      <title>How SQL JOINs Work (Inner, Left, Right, Full)</title>
      <dc:creator>Abdeldjalil Chougui</dc:creator>
      <pubDate>Mon, 30 Nov 2026 11:58:54 +0000</pubDate>
      <link>https://dev.to/abdeldjalilchougui/how-sql-joins-work-inner-left-right-full-4lhk</link>
      <guid>https://dev.to/abdeldjalilchougui/how-sql-joins-work-inner-left-right-full-4lhk</guid>
      <description>&lt;p&gt;Relational databases store each fact once. Customers live in one table, orders in another, and the link between them is a key. That keeps data clean, but it means almost every useful question needs data from more than one table. A JOIN is how SQL puts that data back together.&lt;/p&gt;

&lt;p&gt;This article shows what each type of JOIN returns, using two tiny tables, then covers the traps that produce wrong results without any error.&lt;/p&gt;

&lt;h2&gt;
  
  
  The example tables
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="n"&gt;customers&lt;/span&gt;                 &lt;span class="n"&gt;orders&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;name&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;customer_id&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt;
&lt;span class="c1"&gt;---+--------              ----+-------------+------&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt;  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Amina&lt;/span&gt;                &lt;span class="mi"&gt;101&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;           &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;2500&lt;/span&gt;
&lt;span class="mi"&gt;2&lt;/span&gt;  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Karim&lt;/span&gt;                &lt;span class="mi"&gt;102&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;           &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;1200&lt;/span&gt;
&lt;span class="mi"&gt;3&lt;/span&gt;  &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nc"&gt;Sara&lt;/span&gt;                 &lt;span class="mi"&gt;103&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;           &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;800&lt;/span&gt;
                          &lt;span class="mi"&gt;104&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;           &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice two things. Sara (id 3) has no orders. Order 104 points to customer 9, who does not exist (perhaps a deleted account, or data imported without a foreign key).&lt;/p&gt;

&lt;h2&gt;
  
  
  How a JOIN thinks
&lt;/h2&gt;

&lt;p&gt;Conceptually, a JOIN pairs every row of the left table with every row of the right table, then keeps the pairs that satisfy the &lt;code&gt;ON&lt;/code&gt; condition. The type of join then decides what to do with rows that found no partner. Databases do not literally build every pair; they use smarter algorithms. But this mental model always gives the right answer.&lt;/p&gt;

&lt;h2&gt;
  
  
  INNER JOIN: only the matches
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&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="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&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;total&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;
&lt;span class="k"&gt;INNER&lt;/span&gt; &lt;span class="k"&gt;JOIN&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;ON&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;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;name&lt;/th&gt;
&lt;th&gt;order_id&lt;/th&gt;
&lt;th&gt;total&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Amina&lt;/td&gt;
&lt;td&gt;101&lt;/td&gt;
&lt;td&gt;2500&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Amina&lt;/td&gt;
&lt;td&gt;102&lt;/td&gt;
&lt;td&gt;1200&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Karim&lt;/td&gt;
&lt;td&gt;103&lt;/td&gt;
&lt;td&gt;800&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Only pairs that match. Sara disappears because she has no orders. Order 104 disappears because it has no customer. Amina appears twice because she has two orders: a join produces one row per matching pair, not one row per customer. &lt;code&gt;JOIN&lt;/code&gt; on its own means &lt;code&gt;INNER JOIN&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  LEFT JOIN: everything from the left
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&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="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&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;total&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;
&lt;span class="k"&gt;LEFT&lt;/span&gt; &lt;span class="k"&gt;JOIN&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;ON&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;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;name&lt;/th&gt;
&lt;th&gt;order_id&lt;/th&gt;
&lt;th&gt;total&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Amina&lt;/td&gt;
&lt;td&gt;101&lt;/td&gt;
&lt;td&gt;2500&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Amina&lt;/td&gt;
&lt;td&gt;102&lt;/td&gt;
&lt;td&gt;1200&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Karim&lt;/td&gt;
&lt;td&gt;103&lt;/td&gt;
&lt;td&gt;800&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sara&lt;/td&gt;
&lt;td&gt;NULL&lt;/td&gt;
&lt;td&gt;NULL&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Every customer is kept. Where there is no match, the columns from the right table are filled with &lt;code&gt;NULL&lt;/code&gt;. This is the join you want for "all customers, with their orders if they have any".&lt;/p&gt;

&lt;p&gt;It also gives a classic pattern, the anti-join: find rows with no match.&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;SELECT&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;
&lt;span class="k"&gt;LEFT&lt;/span&gt; &lt;span class="k"&gt;JOIN&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;ON&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;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&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="k"&gt;IS&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;-- Sara: customers who never ordered&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;NOT EXISTS&lt;/code&gt; expresses the same thing and is often clearer.&lt;/p&gt;

&lt;h2&gt;
  
  
  RIGHT JOIN: everything from the right
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;RIGHT JOIN&lt;/code&gt; is the mirror image: keep every row of the right table.&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;SELECT&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&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="k"&gt;AS&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;customers&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;
&lt;span class="k"&gt;RIGHT&lt;/span&gt; &lt;span class="k"&gt;JOIN&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;ON&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;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;name&lt;/th&gt;
&lt;th&gt;order_id&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Amina&lt;/td&gt;
&lt;td&gt;101&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Amina&lt;/td&gt;
&lt;td&gt;102&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Karim&lt;/td&gt;
&lt;td&gt;103&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;NULL&lt;/td&gt;
&lt;td&gt;104&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Order 104 is kept with a &lt;code&gt;NULL&lt;/code&gt; customer. Any &lt;code&gt;RIGHT JOIN&lt;/code&gt; can be written as a &lt;code&gt;LEFT JOIN&lt;/code&gt; by swapping the table order, and most teams do exactly that, because queries read more easily when the "main" table comes first.&lt;/p&gt;

&lt;h2&gt;
  
  
  FULL OUTER JOIN: everything from both
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&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="k"&gt;AS&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;customers&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;
&lt;span class="k"&gt;FULL&lt;/span&gt; &lt;span class="k"&gt;OUTER&lt;/span&gt; &lt;span class="k"&gt;JOIN&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;ON&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;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;name&lt;/th&gt;
&lt;th&gt;order_id&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Amina&lt;/td&gt;
&lt;td&gt;101&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Amina&lt;/td&gt;
&lt;td&gt;102&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Karim&lt;/td&gt;
&lt;td&gt;103&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sara&lt;/td&gt;
&lt;td&gt;NULL&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;NULL&lt;/td&gt;
&lt;td&gt;104&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Matches, plus unmatched rows from both sides. It is handy for reconciliation: comparing two lists and seeing what is missing on each side. PostgreSQL, SQL Server and Oracle support it; MySQL does not, and you emulate it with a &lt;code&gt;LEFT JOIN&lt;/code&gt; combined with a &lt;code&gt;UNION&lt;/code&gt; of the unmatched right rows.&lt;/p&gt;

&lt;h2&gt;
  
  
  CROSS JOIN and self joins
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;CROSS JOIN&lt;/code&gt; has no condition: every row paired with every row. Three customers and four orders give twelve rows. It is useful for generating combinations (every size with every colour), and dangerous by accident.&lt;/p&gt;

&lt;p&gt;A self join joins a table to itself with two aliases, for example employees and their managers stored in the same table:&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;SELECT&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;employee&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;manager&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;
&lt;span class="k"&gt;LEFT&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;m&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;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;manager_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How the database actually runs a join
&lt;/h2&gt;

&lt;p&gt;The planner picks a physical algorithm based on table sizes and indexes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Nested loop:&lt;/strong&gt; for each row on one side, look up matches on the other, ideally through an index. Great when one side is small.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hash join:&lt;/strong&gt; build a hash table on the smaller side's join key, then scan the other side and probe it. Good for large unsorted inputs with an equality condition.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Merge join:&lt;/strong&gt; sort both sides on the key (or read them in order from an index) and walk them together.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You write the logical join; the database picks the physical one. &lt;code&gt;EXPLAIN&lt;/code&gt; shows which one it chose. An index on the foreign key column (&lt;code&gt;orders.customer_id&lt;/code&gt; here) often makes the difference between a fast nested loop and scanning a large table.&lt;/p&gt;

&lt;h2&gt;
  
  
  The traps
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Filtering the right table in &lt;code&gt;WHERE&lt;/code&gt; turns a LEFT JOIN into an INNER JOIN.&lt;/strong&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;-- Wrong: Sara disappears, because o.total is NULL for her&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&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;total&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;
&lt;span class="k"&gt;LEFT&lt;/span&gt; &lt;span class="k"&gt;JOIN&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;ON&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;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&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;total&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- Right: the condition belongs to the join&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&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;total&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;
&lt;span class="k"&gt;LEFT&lt;/span&gt; &lt;span class="k"&gt;JOIN&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;ON&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;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;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;total&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Row multiplication.&lt;/strong&gt; Joining customers to orders and to addresses at the same time multiplies rows: two orders times three addresses gives six rows per customer, and any &lt;code&gt;SUM&lt;/code&gt; is now wrong. Aggregate in a subquery first, then join.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NULL never equals NULL.&lt;/strong&gt; A join condition on columns that can be &lt;code&gt;NULL&lt;/code&gt; silently drops those rows.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Counting with outer joins.&lt;/strong&gt; &lt;code&gt;COUNT(*)&lt;/code&gt; counts Sara's row as 1. Use &lt;code&gt;COUNT(o.id)&lt;/code&gt; to count her orders as 0.&lt;/p&gt;

&lt;h2&gt;
  
  
  In short
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;INNER: only matching pairs. LEFT: all left rows, &lt;code&gt;NULL&lt;/code&gt; where no match. RIGHT: the mirror. FULL: everything from both sides. CROSS: every combination.&lt;/li&gt;
&lt;li&gt;A join returns one row per matching pair, so one-to-many joins repeat the "one" side.&lt;/li&gt;
&lt;li&gt;Put conditions on the optional table in &lt;code&gt;ON&lt;/code&gt;, not &lt;code&gt;WHERE&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Index the join keys and check the plan with &lt;code&gt;EXPLAIN&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://abdeldjalilchougui.biz/blog/how-sql-joins-work" rel="noopener noreferrer"&gt;abdeldjalilchougui.biz&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>database</category>
      <category>backend</category>
      <category>sql</category>
      <category>sqljoins</category>
    </item>
    <item>
      <title>Isolation Levels: Dirty Reads, Phantom Reads and More</title>
      <dc:creator>Abdeldjalil Chougui</dc:creator>
      <pubDate>Mon, 30 Nov 2026 11:56:53 +0000</pubDate>
      <link>https://dev.to/abdeldjalilchougui/isolation-levels-dirty-reads-phantom-reads-and-more-1gmh</link>
      <guid>https://dev.to/abdeldjalilchougui/isolation-levels-dirty-reads-phantom-reads-and-more-1gmh</guid>
      <description>&lt;p&gt;Two transactions run at the same time on the same data. What does each one see of the other? The answer is set by the isolation level, and it is one of the least understood settings in a database. Most bugs it causes never show up in tests, because tests rarely run two transactions at exactly the wrong moment. They show up in production, as a double booking or a balance that does not add up.&lt;/p&gt;

&lt;p&gt;This article walks through the anomalies that concurrent transactions can produce, the four standard isolation levels, and how real databases implement them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why not always run fully isolated?
&lt;/h2&gt;

&lt;p&gt;The safest behaviour is serializable: every set of concurrent transactions produces the same result as some one-at-a-time order. Databases can provide that, but it costs concurrency: more waiting on locks, or more transactions aborted and retried. So the SQL standard defines weaker levels, each allowing some anomalies in exchange for throughput.&lt;/p&gt;

&lt;p&gt;To choose a level, you need to know the anomalies.&lt;/p&gt;

&lt;h2&gt;
  
  
  The anomalies, one by one
&lt;/h2&gt;

&lt;p&gt;We will use two transactions, T1 and T2, and an &lt;code&gt;accounts&lt;/code&gt; table.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dirty read.&lt;/strong&gt; T1 reads data that T2 has written but not yet committed.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;T2: &lt;code&gt;UPDATE accounts SET balance = 0 WHERE id = 1;&lt;/code&gt; (not committed)&lt;/li&gt;
&lt;li&gt;T1: &lt;code&gt;SELECT balance FROM accounts WHERE id = 1;&lt;/code&gt; returns 0&lt;/li&gt;
&lt;li&gt;T2: &lt;code&gt;ROLLBACK;&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;T1 acted on a value that never officially existed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Non-repeatable read.&lt;/strong&gt; T1 reads the same row twice and gets different values, because T2 committed a change in between.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;T1: reads balance of account 1: 100&lt;/li&gt;
&lt;li&gt;T2: updates it to 50 and commits&lt;/li&gt;
&lt;li&gt;T1: reads it again: 50&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Within one transaction, the "same" data changed under you.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Phantom read.&lt;/strong&gt; T1 runs the same query with a condition twice and gets a different set of rows, because T2 inserted or deleted matching rows.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;T1: &lt;code&gt;SELECT COUNT(*) FROM bookings WHERE room = 7 AND day = '2026-12-01';&lt;/code&gt; returns 0&lt;/li&gt;
&lt;li&gt;T2: inserts a booking for room 7 on that day and commits&lt;/li&gt;
&lt;li&gt;T1: the same count now returns 1&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;No existing row changed; a new one appeared. That is why locking individual rows does not prevent phantoms.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lost update.&lt;/strong&gt; Two transactions read the same value, compute a new value in application code, and both write it back. The second write erases the first.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;T1 reads stock = 10, T2 reads stock = 10&lt;/li&gt;
&lt;li&gt;T1 writes stock = 9 and commits; T2 writes stock = 9 and commits&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Two items sold, stock dropped by one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Write skew.&lt;/strong&gt; Two transactions read overlapping data, each checks a rule, and each updates a different row. Each change is valid on its own; together they break the rule. The classic example: a hospital requires at least one doctor on call. Two doctors are on call. Each, in a separate transaction, checks "is someone else on call?", sees yes, and takes themselves off. Both commit, and no one is on call.&lt;/p&gt;

&lt;h2&gt;
  
  
  The four standard levels
&lt;/h2&gt;

&lt;p&gt;The SQL standard defines levels by which of the first three anomalies they must prevent:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Level&lt;/th&gt;
&lt;th&gt;Dirty read&lt;/th&gt;
&lt;th&gt;Non-repeatable read&lt;/th&gt;
&lt;th&gt;Phantom read&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Read Uncommitted&lt;/td&gt;
&lt;td&gt;possible&lt;/td&gt;
&lt;td&gt;possible&lt;/td&gt;
&lt;td&gt;possible&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Read Committed&lt;/td&gt;
&lt;td&gt;prevented&lt;/td&gt;
&lt;td&gt;possible&lt;/td&gt;
&lt;td&gt;possible&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Repeatable Read&lt;/td&gt;
&lt;td&gt;prevented&lt;/td&gt;
&lt;td&gt;prevented&lt;/td&gt;
&lt;td&gt;possible&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Serializable&lt;/td&gt;
&lt;td&gt;prevented&lt;/td&gt;
&lt;td&gt;prevented&lt;/td&gt;
&lt;td&gt;prevented&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The standard says what each level must prevent, not exactly what it allows, so real databases often prevent more than the minimum. Lost updates and write skew are not in the table at all, and that is where many surprises come from.&lt;/p&gt;

&lt;h2&gt;
  
  
  What real databases do
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;PostgreSQL&lt;/strong&gt; uses MVCC: each transaction reads from a snapshot.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Read Uncommitted behaves exactly like Read Committed; PostgreSQL never shows dirty data.&lt;/li&gt;
&lt;li&gt;Read Committed (the default) takes a new snapshot for each statement. So two &lt;code&gt;SELECT&lt;/code&gt;s in the same transaction can see different committed data.&lt;/li&gt;
&lt;li&gt;Repeatable Read takes one snapshot for the whole transaction. That also prevents phantoms for plain reads. If T1 tries to update a row that T2 changed and committed after T1's snapshot, T1 fails with a serialization error and must retry, which prevents the lost update. Write skew is still possible: this level is snapshot isolation.&lt;/li&gt;
&lt;li&gt;Serializable uses Serializable Snapshot Isolation, which tracks read and write dependencies and aborts a transaction when the pattern could produce a non-serializable result. Write skew is prevented, at the cost of occasional retries.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;MySQL InnoDB&lt;/strong&gt; defaults to Repeatable Read. Plain &lt;code&gt;SELECT&lt;/code&gt;s read from a consistent snapshot taken at the first read. Locking reads (&lt;code&gt;SELECT ... FOR UPDATE&lt;/code&gt;) and updates read the latest committed data and take next-key locks, which lock gaps in the index to block inserts of phantom rows. Serializable turns plain &lt;code&gt;SELECT&lt;/code&gt;s into locking reads when autocommit is off.&lt;/p&gt;

&lt;p&gt;Other databases differ again, so check the documentation for yours. The names are standard; the exact behaviour is not.&lt;/p&gt;

&lt;h2&gt;
  
  
  Choosing and coding defensively
&lt;/h2&gt;

&lt;p&gt;You do not always need Serializable. You need to know which anomalies would hurt your code.&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;-- Lost update: let the database compute the new value&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- Or lock the row you read and will update&lt;/span&gt;
&lt;span class="k"&gt;BEGIN&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;stock&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt; &lt;span class="k"&gt;FOR&lt;/span&gt; &lt;span class="k"&gt;UPDATE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;COMMIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Read Committed&lt;/strong&gt; is fine for most simple request handlers, if you write updates as single atomic statements or lock what you read.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Repeatable Read&lt;/strong&gt; is useful for reports and exports that must see one consistent picture across many queries.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Serializable&lt;/strong&gt; is the simplest correct option for complex check-then-write logic (bookings, on-call rules, limits) where locking every row by hand is error-prone. Your code must retry on serialization failures.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Constraints&lt;/strong&gt; are often the cleanest answer: a &lt;code&gt;UNIQUE (room, day)&lt;/code&gt; index makes double booking impossible at any isolation level.
&lt;/li&gt;
&lt;/ul&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="no"&gt;ISOLATION&lt;/span&gt; &lt;span class="no"&gt;LEVEL&lt;/span&gt; &lt;span class="no"&gt;SERIALIZABLE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- check and write&lt;/span&gt;
&lt;span class="k"&gt;COMMIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;-- may fail with a serialization error: retry the whole transaction&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Common mistakes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Not knowing your default.&lt;/strong&gt; PostgreSQL and SQL Server default to Read Committed, MySQL InnoDB to Repeatable Read. ORMs usually keep the database default.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Assuming a transaction prevents lost updates.&lt;/strong&gt; At Read Committed, read-modify-write in application code can lose updates even inside a transaction.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Raising the level without adding retries.&lt;/strong&gt; Higher levels mean more aborted transactions. Without a retry loop, users see errors.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Running long transactions at high levels.&lt;/strong&gt; They increase conflicts and, in MVCC databases, keep old row versions alive.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  In short
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Anomalies: dirty read, non-repeatable read, phantom read, lost update, write skew.&lt;/li&gt;
&lt;li&gt;Levels from weakest to strongest: Read Uncommitted, Read Committed, Repeatable Read, Serializable.&lt;/li&gt;
&lt;li&gt;Real databases implement the same names differently; read your database's documentation.&lt;/li&gt;
&lt;li&gt;Use atomic updates, &lt;code&gt;FOR UPDATE&lt;/code&gt;, constraints, or Serializable with retries, depending on which anomaly threatens your logic.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://abdeldjalilchougui.biz/blog/isolation-levels-dirty-reads-phantom-reads-and-more" rel="noopener noreferrer"&gt;abdeldjalilchougui.biz&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>database</category>
      <category>backend</category>
      <category>sql</category>
    </item>
    <item>
      <title>What Is a Database Transaction? ACID Explained</title>
      <dc:creator>Abdeldjalil Chougui</dc:creator>
      <pubDate>Mon, 30 Nov 2026 03:49:16 +0000</pubDate>
      <link>https://dev.to/abdeldjalilchougui/what-is-a-database-transaction-acid-explained-1io1</link>
      <guid>https://dev.to/abdeldjalilchougui/what-is-a-database-transaction-acid-explained-1io1</guid>
      <description>&lt;p&gt;A customer transfers 5,000 DZD from a savings account to a current account. Your code runs two statements: subtract from one balance, add to the other. Now imagine the server crashes between them. The money has left one account and never arrived in the other. Or imagine two transfers from the same account running at the same moment, both reading the old balance and both deciding there is enough money.&lt;/p&gt;

&lt;p&gt;Transactions exist to make these problems go away. This article explains what a transaction is, what each letter of ACID really promises, and how to use transactions well.&lt;/p&gt;

&lt;h2&gt;
  
  
  What a transaction is
&lt;/h2&gt;

&lt;p&gt;A transaction is a group of database operations that the database treats as a single unit. You start it, run your statements, and then either commit (make all the changes permanent) or roll back (undo all of them).&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;UPDATE&lt;/span&gt; &lt;span class="n"&gt;accounts&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;5000&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;accounts&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;5000&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;COMMIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If anything goes wrong before &lt;code&gt;COMMIT&lt;/code&gt;, whether an error in a statement, a failed check in your application or a crash, the database returns to the state it was in before &lt;code&gt;BEGIN&lt;/code&gt;. Other sessions never see a half-finished transfer.&lt;/p&gt;

&lt;p&gt;In application code it usually looks like this (Python with a DB-API driver):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="c1"&gt;# commits on success, rolls back on exception&lt;/span&gt;
    &lt;span class="n"&gt;cur&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"UPDATE accounts SET balance = balance - %s WHERE id = %s"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;cur&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"UPDATE accounts SET balance = balance + %s WHERE id = %s"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&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 guarantees a transaction gives are summarized by four letters: ACID.&lt;/p&gt;

&lt;h2&gt;
  
  
  A is for Atomicity
&lt;/h2&gt;

&lt;p&gt;Atomic means indivisible. Either every change in the transaction takes effect, or none does. There is no state where the first &lt;code&gt;UPDATE&lt;/code&gt; happened and the second did not.&lt;/p&gt;

&lt;p&gt;How it works: the database records what each transaction changes (in PostgreSQL, through row versions and a log; in InnoDB, with undo logs). On rollback or after a crash, changes from transactions that never committed are discarded or undone.&lt;/p&gt;

&lt;p&gt;Note what atomicity is not about: it says nothing about other transactions running at the same time. That is isolation.&lt;/p&gt;

&lt;h2&gt;
  
  
  C is for Consistency
&lt;/h2&gt;

&lt;p&gt;Consistency means a transaction moves the database from one valid state to another valid state. "Valid" is defined by your rules: constraints such as &lt;code&gt;NOT NULL&lt;/code&gt;, &lt;code&gt;UNIQUE&lt;/code&gt;, foreign keys and &lt;code&gt;CHECK (balance &amp;gt;= 0)&lt;/code&gt;, plus business rules that live in your code.&lt;/p&gt;

&lt;p&gt;The database enforces the constraints you declare. If the transfer would make a balance negative and you have a &lt;code&gt;CHECK&lt;/code&gt; constraint, the statement fails and the transaction cannot commit. But the database cannot know rules you never told it about. If your rule is "the sum of all balances never changes", it is your code that must keep it, by writing transactions that respect it. In that sense, C is partly the application's job, helped by the other three letters.&lt;/p&gt;

&lt;h2&gt;
  
  
  I is for Isolation
&lt;/h2&gt;

&lt;p&gt;Isolation is about concurrency. Many transactions run at the same time, and isolation controls how much they can see of each other's unfinished work.&lt;/p&gt;

&lt;p&gt;The ideal is serializability: the result is the same as if transactions ran one after another in some order. In the double-spend example, two withdrawals from the same account would behave as if one ran fully before the other, and the second would see the reduced balance.&lt;/p&gt;

&lt;p&gt;Full serializability costs performance, so databases offer weaker isolation levels: Read Uncommitted, Read Committed, Repeatable Read and Serializable. Each one allows certain anomalies, such as non-repeatable reads or phantom rows, in exchange for more concurrency. Many databases default to Read Committed (PostgreSQL) or Repeatable Read (MySQL InnoDB), not Serializable. This is the letter developers most often misunderstand, and it deserves its own article.&lt;/p&gt;

&lt;p&gt;Databases implement isolation with locks, with multi-version concurrency control (MVCC), or with a mix of both.&lt;/p&gt;

&lt;h2&gt;
  
  
  D is for Durability
&lt;/h2&gt;

&lt;p&gt;Durability means that once the database says "committed", the change survives a crash or power loss.&lt;/p&gt;

&lt;p&gt;The usual mechanism is the write-ahead log (WAL). Before confirming a commit, the database writes a record of the changes to a log file and forces it to stable storage with &lt;code&gt;fsync&lt;/code&gt;. The actual table pages can be written later. After a crash, the database replays the log to restore every committed change.&lt;/p&gt;

&lt;p&gt;Durability has limits. If the disk itself is lost, you need replicas and backups. Some settings trade durability for speed, such as PostgreSQL's &lt;code&gt;synchronous_commit = off&lt;/code&gt; or relaxed log flushing in InnoDB, where the last moments of commits can be lost in a crash. Know which settings your database runs with.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using transactions well
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Keep transactions short.&lt;/strong&gt; A transaction may hold locks or keep old row versions alive. Do not call an external API, send an email or wait for user input inside one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Do the read and the write in the same transaction.&lt;/strong&gt; "Read balance, check in code, then update" is only safe if the read and update are protected together, for example with &lt;code&gt;SELECT ... FOR UPDATE&lt;/code&gt;, a stricter isolation level, or a single conditional statement:&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;UPDATE&lt;/span&gt; &lt;span class="n"&gt;accounts&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;5000&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;-- 0 rows updated means not enough money&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Handle failures and retries.&lt;/strong&gt; Under higher isolation levels, the database may abort a transaction with a serialization failure or deadlock error. The correct response is to retry the whole transaction.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Watch autocommit.&lt;/strong&gt; Most drivers and the database shell run each statement in its own transaction unless you start one. Two statements that must go together need an explicit transaction.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common mistakes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Assuming ACID means no concurrency bugs.&lt;/strong&gt; With the default isolation level, lost updates and other anomalies are still possible. Check what your level guarantees.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Catching an error and carrying on.&lt;/strong&gt; In PostgreSQL, after an error the transaction is aborted and later statements fail until you roll back.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Long transactions in web requests.&lt;/strong&gt; They hold resources and, in MVCC databases, prevent cleanup of old row versions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Spreading one logical operation across several transactions.&lt;/strong&gt; If two writes must happen together, put them in one transaction, or design for partial failure explicitly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Expecting ACID across services.&lt;/strong&gt; A transaction covers one database. Coordinating changes across services needs other patterns, such as the outbox pattern or sagas.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  In short
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A transaction groups operations into one unit: commit all or roll back all.&lt;/li&gt;
&lt;li&gt;Atomicity: all or nothing. Consistency: valid state to valid state, using your constraints. Isolation: concurrent transactions do not trip over each other, to the degree your isolation level allows. Durability: committed means saved, thanks to the write-ahead log.&lt;/li&gt;
&lt;li&gt;Keep transactions short, read and write in the same transaction, and retry on serialization failures.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://abdeldjalilchougui.biz/blog/what-is-a-database-transaction-acid-explained" rel="noopener noreferrer"&gt;abdeldjalilchougui.biz&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>database</category>
      <category>backend</category>
      <category>acid</category>
    </item>
    <item>
      <title>B-Tree vs LSM-Tree: Two Ways to Store Data</title>
      <dc:creator>Abdeldjalil Chougui</dc:creator>
      <pubDate>Mon, 30 Nov 2026 03:47:15 +0000</pubDate>
      <link>https://dev.to/abdeldjalilchougui/b-tree-vs-lsm-tree-two-ways-to-store-data-51mi</link>
      <guid>https://dev.to/abdeldjalilchougui/b-tree-vs-lsm-tree-two-ways-to-store-data-51mi</guid>
      <description>&lt;p&gt;Under every database there is a storage engine: the part that decides how keys and values are laid out on disk. Two designs dominate. B-trees power PostgreSQL, MySQL's InnoDB, SQLite and most relational databases. Log-structured merge trees (LSM-trees) power RocksDB, LevelDB, Cassandra, ScyllaDB and the storage layers of many newer distributed databases.&lt;/p&gt;

&lt;p&gt;Both store sorted key-value data. Both support point lookups and range scans. They differ in one basic choice: whether to change data where it lives, or to always write somewhere new and clean up later. That choice shapes everything else.&lt;/p&gt;

&lt;h2&gt;
  
  
  The B-tree: update in place
&lt;/h2&gt;

&lt;p&gt;A B-tree keeps keys sorted in fixed-size pages arranged as a shallow, balanced tree. Internal pages hold separator keys that route a search; leaf pages hold the actual entries.&lt;/p&gt;

&lt;p&gt;To write a key, the engine:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Walks down from the root to the leaf where the key belongs.&lt;/li&gt;
&lt;li&gt;Changes that leaf page in memory (inserting, updating or deleting the entry).&lt;/li&gt;
&lt;li&gt;Eventually writes the whole page back to its place on disk. If the page was full, it splits into two and the parent gets a new separator.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For crash safety, the change is first recorded in a write-ahead log (WAL), so the modified page can be written later.&lt;/p&gt;

&lt;p&gt;Reads are simple and predictable: a lookup is a walk of three or four pages from root to leaf, and there is exactly one place where each key can be.&lt;/p&gt;

&lt;p&gt;The cost is on the write side. Changing a 100-byte row means eventually rewriting a whole page (often 8 or 16 KB), plus the WAL record. Writes land at random places on disk, wherever the right leaf happens to be. And pages are often not full after splits, which wastes some space.&lt;/p&gt;

&lt;h2&gt;
  
  
  The LSM-tree: append, then merge
&lt;/h2&gt;

&lt;p&gt;An LSM-tree never modifies data on disk. It works in layers:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Write to the log and the memtable.&lt;/strong&gt; A new write is appended to a WAL for durability, then inserted into an in-memory sorted structure called the memtable (often a skip list).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flush to an SSTable.&lt;/strong&gt; When the memtable reaches a size limit, it is written to disk as one sorted, immutable file called an SSTable (sorted string table). This is a single sequential write.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Compact in the background.&lt;/strong&gt; Over time, many SSTables pile up. A background process called compaction merges them, keeping only the newest version of each key and dropping deleted ones, and writes new, larger sorted files.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Updates and deletes are just new writes. An update writes the key again with a newer value. A delete writes a marker called a tombstone. The old values stay on disk until compaction removes them.&lt;/p&gt;

&lt;p&gt;A read has more work to do, because a key might be in several places:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Check the memtable.&lt;/li&gt;
&lt;li&gt;Check the SSTables, from newest to oldest, and stop at the first match.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To avoid opening every file, each SSTable keeps a sparse index of its keys and usually a Bloom filter, a small structure that can say "this key is definitely not in this file" without touching the disk. Most files are skipped that way.&lt;/p&gt;

&lt;h2&gt;
  
  
  A concrete example
&lt;/h2&gt;

&lt;p&gt;Suppose you update the balance of user 42 five times in an hour.&lt;/p&gt;

&lt;p&gt;In a B-tree, the engine finds the leaf for key 42 each time and modifies the same entry. On disk, there is one copy, and that page is rewritten (sooner or later) after the changes.&lt;/p&gt;

&lt;p&gt;In an LSM-tree, each update is a new entry in the memtable. If flushes happen in between, you may have several SSTables on disk, each holding a different version of key 42. A read finds the newest one first. Later, compaction merges the files and only the last balance survives.&lt;/p&gt;

&lt;h2&gt;
  
  
  The three amplifications
&lt;/h2&gt;

&lt;p&gt;Storage engineers compare these designs with three ratios:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Write amplification:&lt;/strong&gt; bytes written to disk per byte of data you wrote. B-trees pay by rewriting whole pages. LSM-trees pay by rewriting data again at each compaction. Which is worse depends on the workload and settings, but LSM-trees turn random writes into sequential ones, which disks and SSDs handle well.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Read amplification:&lt;/strong&gt; how much work a read does. B-trees have one location per key. LSM-trees may check the memtable and several files; Bloom filters and caching keep this low for point lookups, but range scans must merge results from several files.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space amplification:&lt;/strong&gt; extra disk used. B-trees leave free space in pages. LSM-trees hold obsolete versions and tombstones until compaction, but files are written densely and compress well.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You cannot minimize all three at once. Compaction strategies (leveled versus size-tiered, for example) move the balance between them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Trade-offs side by side
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;B-tree&lt;/th&gt;
&lt;th&gt;LSM-tree&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Write path&lt;/td&gt;
&lt;td&gt;modify page in place&lt;/td&gt;
&lt;td&gt;append to memtable, flush sorted file&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Write throughput&lt;/td&gt;
&lt;td&gt;good&lt;/td&gt;
&lt;td&gt;usually higher, sequential I/O&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Point reads&lt;/td&gt;
&lt;td&gt;fast and predictable&lt;/td&gt;
&lt;td&gt;fast with Bloom filters, more variable&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Range scans&lt;/td&gt;
&lt;td&gt;very good, sorted leaves&lt;/td&gt;
&lt;td&gt;good, merges several files&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Background work&lt;/td&gt;
&lt;td&gt;vacuum or page cleanup&lt;/td&gt;
&lt;td&gt;compaction, can compete with traffic&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Typical users&lt;/td&gt;
&lt;td&gt;PostgreSQL, InnoDB, SQLite&lt;/td&gt;
&lt;td&gt;RocksDB, Cassandra, ScyllaDB&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Common mistakes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Assuming LSM means "faster".&lt;/strong&gt; It usually favours writes. Read-heavy workloads with lots of range scans often do well on a B-tree.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ignoring compaction.&lt;/strong&gt; In an LSM-tree, compaction uses disk bandwidth and CPU. If writes arrive faster than compaction can keep up, files pile up, reads slow down and some engines throttle writes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ignoring tombstones.&lt;/strong&gt; Deleting many keys in an LSM-tree does not free space right away, and scans can slow down while they skip over tombstones.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Forgetting the B-tree's own maintenance.&lt;/strong&gt; B-trees also need care: index bloat and, in PostgreSQL, vacuum to reclaim dead row versions.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  When each one fits
&lt;/h2&gt;

&lt;p&gt;Pick (or prefer a database built on) a &lt;strong&gt;B-tree&lt;/strong&gt; when reads dominate, when you need predictable latency for lookups and range scans, and for general transactional workloads. That is why most relational databases use it.&lt;/p&gt;

&lt;p&gt;Pick an &lt;strong&gt;LSM-tree&lt;/strong&gt; when writes dominate: event logs, metrics, time series, messaging, high-volume ingestion, and key-value workloads where most reads hit recent data. It is also a good fit for SSDs, where sequential writes reduce wear.&lt;/p&gt;

&lt;p&gt;Some systems let you choose. MySQL can use MyRocks, an LSM engine built on RocksDB, as an alternative to InnoDB, and many distributed SQL databases put an LSM engine under a SQL layer.&lt;/p&gt;

&lt;h2&gt;
  
  
  In short
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;B-trees update pages in place: one home per key, predictable reads, random writes.&lt;/li&gt;
&lt;li&gt;LSM-trees append to memory, flush sorted immutable files and merge them in the background: fast writes, reads that check several places.&lt;/li&gt;
&lt;li&gt;Bloom filters and compaction are what make LSM reads and disk usage acceptable.&lt;/li&gt;
&lt;li&gt;Read-heavy and transactional: B-tree. Write-heavy ingestion: LSM-tree.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://abdeldjalilchougui.biz/blog/b-tree-vs-lsm-tree-two-ways-to-store-data" rel="noopener noreferrer"&gt;abdeldjalilchougui.biz&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>database</category>
      <category>backend</category>
      <category>systemdesign</category>
    </item>
  </channel>
</rss>
