From ae2da27652e552f101559466d165b82a3c122d71 Mon Sep 17 00:00:00 2001 From: Louis Williams Date: Tue, 1 Jun 2021 13:37:26 -0400 Subject: SERVER-56509 Wrap unique index insertion _keyExists call in a WT cursor reconfigure This improves, but does not entirely fix a performance regression with inserts and updates on unique indexes (cherry picked from commit c5ac2eb1ea145693e1c6b974e88a2cfc18780134) (cherry picked from commit 297e2977ef3e394e02d61aedc954c9aaadc37e73) --- src/mongo/db/storage/wiredtiger/wiredtiger_index.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/mongo/db/storage/wiredtiger/wiredtiger_index.cpp b/src/mongo/db/storage/wiredtiger/wiredtiger_index.cpp index f39007f4970..5e704c8fe46 100644 --- a/src/mongo/db/storage/wiredtiger/wiredtiger_index.cpp +++ b/src/mongo/db/storage/wiredtiger/wiredtiger_index.cpp @@ -1640,7 +1640,12 @@ Status WiredTigerIndexUnique::_insertTimestampSafe(OperationContext* opCtx, invariantWTOK(ret); // Second phase looks up for existence of key to avoid insertion of duplicate key - if (_keyExists(opCtx, c, keyString.getBuffer(), sizeWithoutRecordId)) { + // The usage of 'prefix_key=true' enables an optimization that allows this search to return + // more quickly. See SERVER-56509. + c->reconfigure(c, "prefix_key=true"); + ON_BLOCK_EXIT([c] { c->reconfigure(c, "prefix_key=false"); }); + auto keyExists = _keyExists(opCtx, c, keyString.getBuffer(), sizeWithoutRecordId); + if (keyExists) { auto key = KeyString::toBson( keyString.getBuffer(), sizeWithoutRecordId, _ordering, keyString.getTypeBits()); return buildDupKeyErrorStatus( -- cgit v1.2.1