Measured with 1,000 tables on five databases
In Polhem, table structures are not maintained with hand-written SQL scripts. They are written as TableSchema XML definitions (example: the order table). Every column, type, length and index of a table is recorded in the definition file, and the database is built from it. To add a column or change an index, you change the definition. You never write the ALTER yourself.
So every release runs a database upgrade: the new definitions are compared with the current state of the database, and only the tables that differ are changed. The upgrade tool itself does not know which tables this release touched, so it has to compare every one of them.
For one database, that is a few seconds, and nobody minds. Renting the system out as a cloud service is a different story. Say there are 1,000 companies, each with its own database, and each database has 1,000 tables. One release now means comparing a million tables. This post records how long that actually takes, and how I cut it down.
1️⃣ What the upgrade tool does during a release
The new definitions are deployed together with the program, and the upgrade tool brings the actual structure of each database in line with them. It compares table by table:
- Read the table's current columns and indexes back from the database catalog (the system tables where a database describes its own structure, such as
sys.columnson SQL Server orUSER_TAB_COLUMNSon Oracle). - Compare them item by item with the definition and list the differences.
- Only if there are differences, generate the ALTER or rebuild statements and run them. Otherwise, skip the table.
How comparing, planning and executing are split into three stages is covered in Day 6: TableSchema and Database Structure Upgrades, so I will not repeat it here. This post only looks at the first two steps, because even when nothing ends up changing, the tool has to read the catalog once to know that nothing needs to change.
With many companies, the upgrade tool handles one database after another, so the total time is roughly the comparison time for one database multiplied by the number of companies.
2️⃣ Measuring one database with 1,000 tables
I wrote a small program and ran it against Docker containers on my own machine:
- On each database, 1,000 tables were created, each with 17 columns (system columns, two foreign keys, text, amount, quantity, date, timestamp and a remarks column), one primary key, one unique index and one composite index, shaped like the tables of a typical business system.
- The tables were created through the framework's own upgrade process, so every table matched its definition afterward, and every comparison should come back with no differences.
- The full comparison ran three times, and the numbers are the average of the last two runs.
- SQLite used a database file. The other four ran in local containers, with the program and the database on the same machine, so there was no network latency.
| Database | Full comparison of 1,000 tables | Average per table |
|---|---|---|
| Oracle | about 16.4 seconds | 16.4 ms |
| SQL Server | about 4.0 seconds | 4.0 ms |
| PostgreSQL | about 2.1 seconds | 2.1 ms |
| MySQL | about 1.8 seconds | 1.8 ms |
| SQLite | about 0.12 seconds | 0.12 ms |
Every table goes through the same work, so the time grows roughly in proportion to the number of tables. Reading one table's structure takes four queries in the framework (whether the table exists, the table's description, the columns and the indexes), and the gap between databases comes mostly from what it costs to read the catalog. Oracle's data dictionary queries are the heaviest, at about four times what SQL Server takes. SQLite keeps its structure description in the same file, so reading it costs almost nothing.
For a single database, 16 seconds is still bearable.
3️⃣ Multiplied by 1,000 companies
Multiply those numbers by 1,000 databases:
| Database | 1,000 companies × 1,000 tables |
|---|---|
| Oracle | about 4.6 hours |
| SQL Server | about 67 minutes |
| PostgreSQL | about 35 minutes |
| MySQL | about 30 minutes |
| SQLite | about 2 minutes |
These are the single-database measurements multiplied directly. In practice it can only take longer, because two things are not counted:
- In production, the database usually sits on another machine, sometimes on another network segment. Reading one table's structure takes several queries, and each one adds a network round trip.
- Every database needs its own connection, so 1,000 databases means 1,000 connections.
All of this time has to fit into the maintenance window of the release, while a release usually touches only a handful of tables.
4️⃣ A release only touches a few tables
The definition files are in version control, so which tables changed between the version running in production and the one being deployed can be found there. For every other table, the comparison is bound to find no differences, and reading the catalog only confirms it once more.
But finding the changed tables through version control adds a collection step before every release: first find out which version each database is on, then pull from version control the tables that changed between that version and the new one, and ship that list with the release. With staged releases, companies sit on different versions, and each needs a different list.
So I went the other way. Instead of asking version control, the database keeps its own record: for each table, which definition was used the last time it was upgraded successfully. At the next release, if the definition matches the record, there is no need to read the database.
To confirm that the definition is the same, there is no need to compare the whole definition item by item. A hash is enough: it turns the content into a fixed-length fingerprint, and changing a single character produces a different fingerprint.
5️⃣ An upgrade record table
Each database gets one more table, with one record per table:
| Column | Content |
|---|---|
| Table name | The primary key |
| Status | Upgrading, succeeded, failed or verified |
| Definition | The full text of the definition applied by the last successful upgrade |
| Definition hash | The SHA-256 of that full text |
| Format version | The version of the upgrade behavior of the tool that wrote the record (what it is for comes later) |
| Error message | The reason, when the upgrade failed |
The comparison during a release becomes:
- For each database, first read the name, status, format version and hash of every record in one query. The full definitions are not read, so even with several thousand tables, very little data comes back.
- Compute the hash of each new definition and compare it with the record.
- If the record says succeeded or verified, the format version is the current one and the hash matches, the table is treated as unchanged, and the catalog is not read.
- In every other case, including no record, a failed last upgrade, or a record table that cannot be read, fall back to the original full comparison.
The same 1,000 tables, compared through the records instead:
| Database | Full comparison | Record-based comparison | 1,000 companies (record-based) |
|---|---|---|---|
| Oracle | about 16.4 seconds | about 25 ms | about 25 seconds |
| SQL Server | about 4.0 seconds | about 11 ms | about 11 seconds |
| PostgreSQL | about 2.1 seconds | about 9 ms | about 9 seconds |
| MySQL | about 1.8 seconds | about 10 ms | about 10 seconds |
| SQLite | about 0.12 seconds | about 7 ms | about 7 seconds |
The time to read the records differs by only a dozen or so milliseconds between databases, because it is just one ordinary query plus 1,000 hash computations. Network round trips matter much less as well: one query per database, instead of several per table.
The few tables that really did change have hashes that no longer match, so they go through the full comparison and the upgrade as before, and their records are updated once the upgrade succeeds.
6️⃣ The cost: changes it cannot see
Skipping the catalog means not seeing what changes in it. If someone bypasses the upgrade tool and drops an index or alters a column directly in the database, the definition has not changed and neither has the record, so the record-based comparison will treat the table as fine.
That is why the full comparison was not removed. It is just no longer the default. When you suspect someone has changed the database by hand, add a parameter to run a full comparison. Tables it finds with no differences get their records filled in along the way, with the status set to verified, so the next release can use them directly.
7️⃣ Another multi-tenant risk: old and new tools mixed
Not all 1,000 companies upgrade on the same day. During a staged release, some scheduled job or some machine may still have the old version of the upgrade tool.
The behavior of the upgrade tool itself also changes between versions, for example how a date column's default value is written on a particular database. Once the new tool has changed a table to the new form, the old tool comes along, sees the new form as a difference and changes it back, and the next run of the new tool changes it again. With the two taking turns, the table keeps flipping back and forth, and on SQLite every round also rebuilds the whole table.
The format version in the record table is there to stop this. Before the upgrade tool changes anything, it checks the highest format version in that database. If it is newer than its own, it skips the whole database and reports it, without touching a single table. The format version only goes up when the upgrade behavior actually changes, not with every release, so ordinary releases do not block each other.
✅ Wrapping up
With one system, one database and a few dozen tables, the cost of a full comparison is not noticeable. Running a full comparison every time is the simplest choice, and it needs no extra record table.
The cost only surfaces once it is multiplied by the number of databases times the number of tables. In a multi-tenant SaaS, it grows from seconds to hours, and most of that time goes into confirming tables that have not changed. Record that fact, and a release only needs to deal with the tables that really changed.
This approach has one precondition: the database structure is changed only through the upgrade tool, and the definition files are the only source of truth. If your environment often needs structure changes made directly in the database, the records cannot be trusted, and a full comparison is the better fit.
Also, one database per company is only one way to deploy multi-tenancy. Having several companies share a database cuts the number of databases directly, but that is a different topic, and I will leave it out of this post.
Further reading
- Day 6: TableSchema and Database Structure Upgrades: the three stages of comparing, planning and executing, and how to choose between ALTER and a rebuild
- Day 11: Connections, Transactions and Query Conditions in the Data Access Layer: database categories, and deploying one database per company
📘 Original article on HackMD, in Traditional Chinese:
👉 https://hackmd.io/@jeff377/polhem-schema-compare
📢 You are welcome to republish this, please credit the source.
📬 Follow for more engineering notes:
Facebook | HackMD | GitHub | NuGet
Top comments (0)