The labs on this site need JavaScript to run. The same content is written out below.
Learn › Database › Scaling and distribution
Split an order into three tables, or put it in one document?
Why it exists — A relational database splits data into separate tables, so showing one order requires joining several tables, and even to add a new item you must first change the table structure. A NoSQL database such as a document database stores the data that is read together as one document and accepts documents of different shapes, which makes reads simpler and lets you add items without a structure change.
Where it's used today — Use a document database such as MongoDB for data that is read and written as one block and whose shape changes often, such as a product list where each product has different attributes, logs and event records, and per-user settings. Data that several places share and change often, such as a product name, and payment and inventory data that must change several tables at once, mostly stay in a relational database.
Store the same data (orders and order items) in two ways. The relational way splits products, orders, and order items into three tables and joins them when reading. The document model is the way used by document databases such as MongoDB: it treats one order as one document and puts the array of items and even the product names inside it.
Checked against real execution · All 20 combinations were checked by execution: the relational model on MySQL 8.4.11, and the document model on both a MySQL 8.4.11 JSON column and MongoDB 8.0.38
-- The document model, reproduced with a MySQL JSON column. One row is one document
CREATE TABLE order_doc (id INT PRIMARY KEY, doc JSON NOT NULL);
-- Example of a document (order 1)
-- {"member": "kim",
-- "items": [{"product_id": 1, "name": "keyboard", "qty": 1},
-- {"product_id": 2, "name": "mouse", "qty": 2}]}
-- In each document, find the position of 'keyboard' in the items array and change the name at that position
UPDATE order_doc
SET doc = JSON_SET(doc, JSON_UNQUOTE(JSON_SEARCH(doc, 'one', 'keyboard', NULL, '$.items[*].name')), 'mechanical keyboard')
WHERE JSON_SEARCH(doc, 'one', 'keyboard', NULL, '$.items[*].name') IS NOT NULL;
-- The commands that do the same thing in MongoDB, a document database (mongosh. In the order_doc collection, one document is one order)
-- db.order_doc.updateMany(
-- { "items.name": "keyboard" },
-- { $set: { "items.$[it].name": "mechanical keyboard" } },
-- { arrayFilters: [{ "it.name": "keyboard" }] })
On the screen, the document model is reproduced with a MySQL JSON column, and I also ran the same operations on MongoDB, a dedicated document database. I ran twenty combinations (two ways of storing, four things to do, and 2, 3, and 5 orders) with the same statements as on the screen on MySQL 8.4.11 in our test run, and checked that the following matched the screen: the error code; the number of tables and the number of result rows shown by EXPLAIN; the number of rows (documents) changed by UPDATE; the number of documents with gift; and the quantity total. I ran the ten combinations of the document model again on MongoDB 8.0.38 (the official mongo:8.0 image on Docker Hub, mongosh 2.14.3) with the commands written as comments below the code (find, updateMany with arrayFilters, insertOne, and $unwind with $group), and all three columns of the results table matched the screen. When one order was read by _id, the query plan had no join stage and only 1 document was examined; the number of documents changed by updateMany (modifiedCount) equaled the number of orders; and the document with a misspelled field name went in without an error, and $sum skipped that document. I also confirmed the following facts used in the explanations by execution. Reading without a join needs three queries, one for each table. After a column is added with ALTER TABLE, the same INSERT goes in, and the existing rows also get that column. If only one document is changed, the same product is left with two different names (on both MySQL and MongoDB). After a JSON_SCHEMA_VALID constraint is added, a document with a misspelled field name is rejected with error 3819. On MongoDB, after a $jsonSchema validation rule was added to the collection, the same document was rejected with error 121 (Document failed validation). I also saw where the two products differ. When gift is read from a document that has no gift, the MySQL JSON column gives NULL, and in MongoDB the document does not have that field at all (reading it in mongosh gives undefined). MongoDB also has the $lookup stage, which joins another collection, so a document that keeps only product_id without a copy of the product name could be read together with the product collection. MongoDB is the only document database I ran, and query syntax, how to change a value inside an array, the schema validation feature, and support for transactions and joins across several documents differ by product and version. Which way is faster depends on the data volume, the indexes, and the product, so it is not covered, and the results on the screen are the number of places read or changed, not the speed. Other kinds of NoSQL, such as key-value stores, wide-column stores, and graph databases, are not covered.
Previous lab: Redis single-threaded execution
Struck-through text was removed from the first version of the code; highlighted text was added.