[SERVER-48777] Covered Queries do not work with partial indices Created: 15/Jun/20  Updated: 30/May/23  Resolved: 16/Jun/20

Status: Closed
Project: Core Server
Component/s: Performance
Affects Version/s: 4.2.7, 4.2.8, 4.4.0-rc9
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Thilo Schmalfuß Assignee: Carl Champain (Inactive)
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-28889 Partial index shouldn't do fetch on c... Closed
Operating System: ALL
Steps To Reproduce:

Please see the example below. Why does 2nd query perform document lookups?

db.createCollection('inventory')
db.getCollection('inventory').insertMany([
 
    { type: 'food', color: 'yellow', name: 'banana' },
    { type: 'food', color: 'red',    name: 'cherry' },
    { type: 'car',  color: 'yellow', name: 'taxi' }
 
])
// create partial index, which "should" cover name & color field
db.getCollection('inventory').createIndex({ name: 1 },          { name: 'onlyRed', partialFilterExpression: {  color: 'red' } })
 
// totalDocsExamined is one but should be zero
db.getCollection('inventory').find({ color: 'red', name: /c/ }, { name: 1, _id: 0 }).explain('executionStats').executionStats.totalDocsExamined

Here is the full explain result:

{
    "queryPlanner" : {
        "plannerVersion" : 1,
        "namespace" : "demo.inventory",
        "indexFilterSet" : false,
        "parsedQuery" : {
            "$and" : [ 
                {
                    "color" : {
                        "$eq" : "red"
                    }
                }, 
                {
                    "name" : {
                        "$regex" : "c"
                    }
                }
            ]
        },
        "winningPlan" : {
            "stage" : "PROJECTION_SIMPLE",
            "transformBy" : {
                "name" : 1.0,
                "_id" : 0.0
            },
            "inputStage" : {
                "stage" : "FETCH",
                "filter" : {
                    "color" : {
                        "$eq" : "red"
                    }
                },
                "inputStage" : {
                    "stage" : "IXSCAN",
                    "filter" : {
                        "name" : {
                            "$regex" : "c"
                        }
                    },
                    "keyPattern" : {
                        "name" : 1.0
                    },
                    "indexName" : "onlyRed",
                    "isMultiKey" : false,
                    "multiKeyPaths" : {
                        "name" : []
                    },
                    "isUnique" : false,
                    "isSparse" : false,
                    "isPartial" : true,
                    "indexVersion" : 2,
                    "direction" : "forward",
                    "indexBounds" : {
                        "name" : [ 
                            "[\"\", {})", 
                            "[/c/, /c/]"
                        ]
                    }
                }
            }
        },
        "rejectedPlans" : []
    },
    "executionStats" : {
        "executionSuccess" : true,
        "nReturned" : 1,
        "executionTimeMillis" : 0,
        "totalKeysExamined" : 1,
        "totalDocsExamined" : 1,
        "executionStages" : {
            "stage" : "PROJECTION_SIMPLE",
            "nReturned" : 1,
            "executionTimeMillisEstimate" : 0,
            "works" : 2,
            "advanced" : 1,
            "needTime" : 0,
            "needYield" : 0,
            "saveState" : 0,
            "restoreState" : 0,
            "isEOF" : 1,
            "transformBy" : {
                "name" : 1.0,
                "_id" : 0.0
            },
            "inputStage" : {
                "stage" : "FETCH",
                "filter" : {
                    "color" : {
                        "$eq" : "red"
                    }
                },
                "nReturned" : 1,
                "executionTimeMillisEstimate" : 0,
                "works" : 2,
                "advanced" : 1,
                "needTime" : 0,
                "needYield" : 0,
                "saveState" : 0,
                "restoreState" : 0,
                "isEOF" : 1,
                "docsExamined" : 1,
                "alreadyHasObj" : 0,
                "inputStage" : {
                    "stage" : "IXSCAN",
                    "filter" : {
                        "name" : {
                            "$regex" : "c"
                        }
                    },
                    "nReturned" : 1,
                    "executionTimeMillisEstimate" : 0,
                    "works" : 2,
                    "advanced" : 1,
                    "needTime" : 0,
                    "needYield" : 0,
                    "saveState" : 0,
                    "restoreState" : 0,
                    "isEOF" : 1,
                    "keyPattern" : {
                        "name" : 1.0
                    },
                    "indexName" : "onlyRed",
                    "isMultiKey" : false,
                    "multiKeyPaths" : {
                        "name" : []
                    },
                    "isUnique" : false,
                    "isSparse" : false,
                    "isPartial" : true,
                    "indexVersion" : 2,
                    "direction" : "forward",
                    "indexBounds" : {
                        "name" : [ 
                            "[\"\", {})", 
                            "[/c/, /c/]"
                        ]
                    },
                    "keysExamined" : 1,
                    "seeks" : 1,
                    "dupsTested" : 0,
                    "dupsDropped" : 0
                }
            }
        }
    },
    "serverInfo" : {
        "host" : "mongo",
        "port" : 27017,
        "version" : "4.4.0-rc9",
        "gitVersion" : "bea79f76addfe4b754c8696db029c5b3c762041c"
    },
    "ok" : 1.0,
    "$clusterTime" : {
        "clusterTime" : Timestamp(1591984064, 1),
        "signature" : {
            "hash" : { "$binary" : "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "$type" : "00" },
            "keyId" : NumberLong(0)
        }
    },
    "operationTime" : Timestamp(1591984064, 1)
}

 

Participants:

 Description   

A query matching a partial index performs document lookups, although all fields could be covered by the index.

This issue was observed with the following versions of MongoDB: 4.2.1, 4.2.7, 4.4.0-rc9

I raised this already on StackOverflow: https://stackoverflow.com/questions/62342543/covered-queries-do-not-work-with-partial-indices



 Comments   
Comment by Asya Kamsky [ 30/May/23 ]

This is actually duplicate of SERVER-28889

Comment by Asya Kamsky [ 30/May/23 ]

Looks like this was duped to the wrong ticket... fixing.

Comment by Thilo Schmalfuß [ 16/Jun/20 ]

Hi Carl, 

thanks for looking into this. It seems indeed to be the same problem.

Comment by Carl Champain (Inactive) [ 16/Jun/20 ]

Hi thilo@staffbase.com,

Thanks for the report.
I believe this is being worked on in SERVER-26580 so I'm going to close this ticket as a duplicate. Please let me know if this is actually a different issue.

Kind regards,
Carl
 

Generated at Thu Feb 08 05:18:03 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.