fix(mongodb): skip $$REMOVE check for required fields to enable index usage#5724
Open
Kkartik14 wants to merge 1 commit intoprisma:mainfrom
Open
fix(mongodb): skip $$REMOVE check for required fields to enable index usage#5724Kkartik14 wants to merge 1 commit intoprisma:mainfrom
Kkartik14 wants to merge 1 commit intoprisma:mainfrom
Conversation
CodSpeed Performance ReportMerging this PR will not alter performanceComparing Summary
Footnotes
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes prisma/prisma#29000
This PR fixes a critical performance issue where all MongoDB queries bypass indexes due to unconditional
$$REMOVEchecks.Problem
Previously, every scalar filter in
mongodb-query-connectorwas wrapped with:This forces MongoDB to use
$expr(aggregation expressions), which cannot use indexes, causing full collection scans (COLLSCAN) on every query—including_idand@uniquefield lookups.Root Cause
The
exclude_undefineds()function was designed to handle optional fields that might beundefinedin MongoDB documents. However, it was being called unconditionally for all fields, including:_idprimary key (impossible to be missing)@uniquerequired fields (have indexes that should be used)Solution
Only add the
$$REMOVEcheck for optional fields. Required fields and_idcannot be undefined by definition.Added
!field.is_required()condition before callingexclude_undefineds()in:default_scalar_filter()(line 278)insensitive_scalar_filter()(line 421)Related Issues
$exprand$andin Mongo, resulting in the query by-passing indexes prisma#22878