Introduction#
We’re building Dolt, the world’s first version-controlled SQL database. We measure Dolt’s performance with sysbench, an industry standard set of SQL benchmarks that you can use to get an apples-to-apples performance comparison of the same workload on different database products. Our results on these benchmarks indicate that Dolt is faster than MySQL on nearly every benchmark.
But the real world is more complicated than benchmarks, and they don’t tell the whole story. Out in the wild, customers have very different schemas than our benchmarks, they write vastly more complicated queries than our benchmarks run, and they have application designs and access patterns that no benchmark can adequately capture. So it’s not uncommon that a customer comes to us with a query or access pattern with terrible performance and asks us to fix it. Usually, they’re doing something we hadn’t seen before, and which wasn’t well captured in our benchmarks or testing.
Last month that happened with foreign keys. A customer was running their database application with a very large number of foreign keys — hundreds declared on some tables. Nothing in our benchmark or testing data compared to this scale of foreign key use. They noticed their write performance tanked when they added all the foreign key constraints, so they asked us to fix it.
We did. Read on for details on how.

Deserialization of foreign keys was quadratic#
When Dolt or Doltgres processes a write query like INSERT or DELETE, it needs to load the
foreign keys from their serialized form on disk. This is usually cached, but in a writing loop it
isn’t necessarily. Deserialization itself was fast, but it added keys to a collection type in a
loop, and adding each key was linear time, for an overall quadratic run time. Not a big deal for a
handful of foreign keys in a database, but a performance killer when you have hundreds. We rewrote
how the collection stores its keys and removed redundant checks to make deserialization linear.
Merges were very slow when foreign keys were present#
When Dolt merges two database revisions together, it needs to run foreign key checks to make sure
that the merge doesn’t introduce a constraint violation, just like it needs to run those checks on
an INSERT statement. In Dolt, merges happen under concurrency when a transaction is committed, so
merge performance is critical.
As it turned out, we were doing a couple of very dumb things related to foreign key performance during merge time.
- We were loading and deserializing the foreign key collection on each side of a merge to compare them, even when they hadn’t changed from the merge base. Worse, we were doing it multiple times per merge. Now we compare the hashes of the foreign key collections as a short-circuit in the common case, just like we do for other database entities.
- We were running foreign key check logic even if neither table in the constraint had changed from the merge base. Now we short-circuit to avoid this unnecessary work.
Two dumb oversights, both now fixed.
ForeignKeyCollection copied itself constantly#
We are the versioned database company, and we like immutable data structures. In the case of
ForeignKeyCollection, we made the collection immutable by deep-copying it every time it was
accessed from the database root, so no client could modify it and impact other clients. This worked,
but it’s very expensive when you have hundreds of foreign keys.
A much faster way to get immutable data structures is via copy-on-write semantics. Most clients who want to examine a foreign key have no interest in modifying it, so defensively copying it is a waste of time. Instead, make every operation that modifies the collection return a copy of that collection with the given modification, rather than modifying it in place.
Making ForeignKeyCollection copy-on-write turned several linear-time operations per request into
constant-time operations. Even better, it made it possible to share these objects in caches.
We planned foreign key editors that we didn’t need and never used#
During write operations, the database enforces foreign key constraints by inserting a
ForeignKeyEditor into the editor chain for a table. Its job is to make sure any edit doesn’t
violate the constraint, and to update child rows, or return an error to abort the write, if it does.
The problem: we were inserting such an editor into the chain even for UPDATE statements that
didn’t modify any columns that could cause a foreign key violation, which becomes a massive
inefficiency at the scale of hundreds of such constraints. Worse, we were creating a useless and
expensive child table editor even for foreign keys declared RESTRICT or NO ACTION. Now, planning
rules omit these useless constructs, resulting in much faster DML operations in some schemas when
many foreign keys are present.
Overall result#

With these optimizations, we achieved over a 99% reduction in latency for the customer who reported the issue. This makes us feel good, but also dumb. As the old saying in computer science goes:
If you speed something up by 25%, you started doing something clever. If you speed something up by 10x, you stopped doing something dumb.
Better to stop doing something dumb than to keep doing it, though!
Conclusion#
Dolt is free and open source, so go check it out! Performance is always near the top of our priorities, and we rely on our customers to find the real-world use cases that our benchmarking and testing miss. Try it yourself and let us know where we can improve.
Have questions about Dolt or Doltgres? Join us on Discord to talk to our engineering team and meet other users.