defradb icon indicating copy to clipboard operation
defradb copied to clipboard

Optimize 1-to-1 links validation upon saving a document

Open islamaliev opened this issue 8 months ago • 0 comments

upon saving a document to a collection for every relation 1-to-1 field we run a full scan to find out if there is any other document that refers to the same object. This is done to make sure 1-to-1 invariant is kept valid.

It uses the full scan. I didn't run any benchmarks yet, but this is potentially a huge performance hit and one of the reasons creation of multiple documents with related objects is so slow.

Check validateOneToOneLinkDoesntAlreadyExist in internal/db/collection.go

One of the way to optimize if would be utilize secondary indexes, but this will require forcing sec. indexes on all relations.

Related explain metrics ticket https://github.com/sourcenetwork/defradb/issues/2689

islamaliev avatar Jun 06 '24 11:06 islamaliev