KyoMel / Yogyakarta, Indonesia

Term 277277 / 344

The Order of Data

Month
July 2026
Day
15
Source
2026/july-term.md

Definition:

“The Order of Data” is a concept from database systems that describes how the order of query results affects correctness, performance, and user experience — and the hidden dangers of assuming data comes back in any particular order without explicitly specifying it. The core lesson is deceptively simple but repeatedly trips up developers: if you don’t tell the database exactly what order you want, you get whatever order it feels like — and that order can change arbitrarily.

The three problems that “order of data” covers are:

  1. Default ordering is unreliable — Different databases return unsorted data in different orders, and the same database can change its default order after updates, vacuum operations, or version upgrades
  2. Sorting unindexed columns is expensive — Sorting a column without an index can be 500-700x slower than sorting an indexed column, turning a fast query into a multi-second disaster at scale
  3. Pagination without deterministic ordering = data corruption — Using OFFSET for pagination with a non-unique sort column causes rows to appear on multiple pages, get skipped entirely, or show overlapping results

Example:

A visual walkthrough of the three problems and their solutions.

═══════════════════════════════════════════════════════════════
  SCENARIO: You run SELECT * FROM users LIMIT 3
  (No ORDER BY specified)
═══════════════════════════════════════════════════════════════

  ┌─────────────────────────────────────────────────────────┐
  │  POSTGRESQL (Default: Insertion Order)                  │
  │                                                         │
  │  Insert order:                                          │
  │    1st → Alice                                           │
  │    2nd → Bob                                             │
  │    3rd → Charlie                                         │
  │    4th → Diana                                           │
  │                                                         │
  │  SELECT * FROM users LIMIT 3;                            │
  │  → Alice, Bob, Charlie  ✅ (insertion order)             │
  │                                                         │
  │  Now UPDATE Bob's email:                                 │
  │  → Postgres moves Bob to the END (MVCC behavior)        │
  │                                                         │
  │  SELECT * FROM users LIMIT 3;                            │
  │  → Alice, Charlie, Diana  ❌ (Bob moved!)                │
  │                                                         │
  │  The same query returns DIFFERENT results!              │
  └─────────────────────────────────────────────────────────┘

  ┌─────────────────────────────────────────────────────────┐
  │  MYSQL (Default: Primary Key Order / InnoDB Clustered   │
  │        Index)                                            │
  │                                                         │
  │  Insert order:                                          │
  │    id=1 → Alice                                          │
  │    id=2 → Bob                                            │
  │    id=3 → Charlie                                        │
  │    id=4 → Diana                                          │
    │                                                         │
    │  SELECT * FROM users LIMIT 3;                            │
    │  → Alice, Bob, Charlie  ✅ (PK order)                    │
    │                                                         │
    │  UPDATE Bob's email:                                     │
    │  → MySQL keeps Bob at id=2 (clustered index is stable)  │
    │                                                         │
    │  SELECT * FROM users LIMIT 3;                            │
    │  → Alice, Bob, Charlie  ✅ (still the same!)             │
    └─────────────────────────────────────────────────────────┘
  
    ┌─────────────────────────────────────────────────────────┐
    │  MONGODB (Default: Insertion Order, like Postgres)      │
    │                                                         │
    │  Same as Postgres — update moves document to end        │
    │  if document grows beyond its allocated space.          │
    │                                                         │
    │  BUT: Updates that don't change document size           │
    │  leave it in place. Inconsistent.                       │
    └─────────────────────────────────────────────────────────┘
  
    ═══════════════════════════════════════════════════════════
    ✅ THE FIX: Always specify ORDER BY explicitly
    ═══════════════════════════════════════════════════════════
  
    SELECT * FROM users ORDER BY id LIMIT 3;
  
    → Works the same on Postgres, MySQL, MongoDB, SQLite,
      SQL Server, Oracle — every time, every database.
    → Insertions? Updates? Deletions? Vacuum? Doesn't matter.
      ORDER BY id guarantees the same result every time.
  
    RULE: If you care about order (and you should), type ORDER BY.

Keep reading