Notes

Part 5 of 10

When to use which model

  • For documents: flexible schemas, speed from locality, and a closer match to app objects in some apps.
  • For relational: stronger joins, and good support for many-to-one and many-to-many links.

If your data is a tree of one-to-many lists that you usually load whole, use documents. Splitting such a tree into many tables (ingredients, steps, tags) is called shredding, and it can lead to clumsy schemas and complicated code.

You cannot point straight at an item nested in a document; you must say "the fourth step of recipe 92". If other data must point at nested items, referring to rows by ID in tables works better.

Lists that users reorder by hand, like a to-do list or an issue tracker, are easy as a JSON array, which keeps order. Relational databases have no standard way to store that order. Tricks include:

  • An integer position column, renumbered when you insert in the middle.
  • A linked list of IDs.
  • Fractional indexing.

Who checks the schema

Most document stores, and JSON columns in relational databases, do not enforce a schema; XML support often offers optional checking. Any keys and values can be added, and readers get no promise about which fields exist.

"Schemaless" is misleading. Code that reads the data still expects a structure, so there is an implicit schema the database does not check.

  • Schema-on-read: structure is implicit and interpreted when read, like dynamic (runtime) type checking.
  • Schema-on-write: structure is explicit and the database makes every write fit it, as relational databases traditionally do, like static (compile-time) type checking.

People debate which is better, and neither clearly wins. The difference shows when the format changes. Say you split one "address" field into "street" and "city".

  • Schema-on-read: new records get the new fields, and readers handle old ones (say, by splitting the address when street is missing), forever and in every reader.
  • Schema-on-write: run a migration that adds the columns, then updates every row to fill them.

Adding a column with a default is quick in most relational databases, even on a huge table. Updating every row is slow, since each row gets rewritten. Other changes, like a new column type, usually copy the whole table. Tools can run migrations in the background without downtime, but big ones stay hard to operate. A workaround: add the column with a NULL default (quick) and fill it in when reading, as a document store would.

Schema-on-read suits mixed data:

  • Many kinds of object, where a table per kind is impractical.
  • Structure set by outside systems you do not control, which may change at any time.

When all records share one structure, a schema documents and enforces it. Chapter 5 covers schema evolution.

Locality

A document is usually stored as one continuous string: JSON, XML, or a binary form like MongoDB's BSON. If you often need the whole thing, say to render a page, this helps. Data split across tables needs several index lookups, so more disk seeks and more time.

Locality only pays off when you read most of a document. The database usually loads the whole document even for one field, which wastes work, and usually rewrites the whole document on any update. So keep documents small and avoid frequent tiny edits.

Relational systems can get locality too:

  • Google Spanner can nest (interleave) a table's rows inside a parent table.
  • Oracle offers multi-table index cluster tables.
  • Wide-column stores (Google Bigtable, HBase, Accumulo) use column families to control what sits together.

Querying documents

Relational databases mostly use SQL; document stores vary:

  • Some only fetch by primary key.
  • Some add secondary indexes on values inside documents.
  • Some offer rich query languages.

MongoDB's aggregation pipeline queries JSON collections and includes $lookup for joins. To count Android crash reports per week, SQL filters, rounds each timestamp down to its week, groups, and sums; the pipeline uses a $match stage, then a $group stage by year and week with $sum.

The pipeline covers roughly a subset of SQL in JSON syntax; picking one is mostly taste.

The two models meet

They began far apart and have grown alike:

  • Relational databases added JSON types, JSON operators, and indexes on fields inside documents.
  • Document databases (MongoDB, Couchbase, RethinkDB) added joins, secondary indexes, and declarative queries.

That helps developers, since real apps need both: documents need references, and tables gain from flexible schemas in places. Hybrids are powerful.