具有过滤器的Mongo日期范围索引

时间:2017-10-23 18:52:53

标签: mongodb mongodb-query mongodb-indexes

我们有以下查询

db.Comment.find(
    {
        $and: [ 
            { reportCount: { $gt: 0 } },
            { assignee: { $exists: false } }, 
            { creationDate: { $gt: new Date(1507831097809) } },
            { creationDate: { $lt: new Date(1508522297966) } },  
            { siteId: 'MAIN' }, 
            { parent: { $exists: false } }, 
            { status: 'ACTIVE' }
        ]
    })
    .sort({ creationDate: 1 })

我们有一个索引

 {
    "v" : 2,
    "key" : {
        "creationDate" : 1,
        "reportCount" : 1,
        "label" : 1
    }
}

以下是explain结果:

{
"queryPlanner" : {
    "plannerVersion" : 1,
    "namespace" : "myNameSpace",
    "indexFilterSet" : false,
    "parsedQuery" : {
        "$and" : [ 
            {
                "siteId" : {
                    "$eq" : "MAIN"
                }
            }, 
            {
                "status" : {
                    "$eq" : "ACTIVE"
                }
            }, 
            {
                "creationDate" : {
                    "$lt" : ISODate("2017-10-20T17:58:17.966Z")
                }
            }, 
            {
                "creationDate" : {
                    "$gt" : ISODate("2017-10-12T17:58:17.809Z")
                }
            }, 
            {
                "reportCount" : {
                    "$gt" : 0.0
                }
            }, 
            {
                "$nor" : [ 
                    {
                        "assignee" : {
                            "$exists" : true
                        }
                    }
                ]
            }, 
            {
                "$nor" : [ 
                    {
                        "parent" : {
                            "$exists" : true
                        }
                    }
                ]
            }
        ]
    },
    "winningPlan" : {
        "stage" : "FETCH",
        "filter" : {
            "$and" : [ 
                {
                    "siteId" : {
                        "$eq" : "MAIN"
                    }
                }, 
                {
                    "status" : {
                        "$eq" : "ACTIVE"
                    }
                }, 
                {
                    "$nor" : [ 
                        {
                            "assignee" : {
                                "$exists" : true
                            }
                        }
                    ]
                }, 
                {
                    "$nor" : [ 
                        {
                            "parent" : {
                                "$exists" : true
                            }
                        }
                    ]
                }
            ]
        },
        "inputStage" : {
            "stage" : "IXSCAN",
            "keyPattern" : {
                "creationDate" : 1.0,
                "reportCount" : 1.0,
                "label" : 1.0
            },
            "indexName" : "creationDate_1_reportCount_1_label_1",
            "isMultiKey" : false,
            "multiKeyPaths" : {
                "creationDate" : [],
                "reportCount" : [],
                "label" : []
            },
            "isUnique" : false,
            "isSparse" : false,
            "isPartial" : false,
            "indexVersion" : 2,
            "direction" : "forward",
            "indexBounds" : {
                "creationDate" : [ 
                    "(new Date(1507831097809), new Date(1508522297966))"
                ],
                "reportCount" : [ 
                    "(0.0, inf.0]"
                ],
                "label" : [ 
                    "[MinKey, MaxKey]"
                ]
            }
        }
    },
    "rejectedPlans" : [ 
        {
            "stage" : "SORT",
            "sortPattern" : {
                "creationDate" : 1.0
            },
            "inputStage" : {
                "stage" : "SORT_KEY_GENERATOR",
                "inputStage" : {
                    "stage" : "FETCH",
                    "filter" : {
                        "$and" : [ 
                            {
                                "$nor" : [ 
                                    {
                                        "parent" : {
                                            "$exists" : true
                                        }
                                    }
                                ]
                            }, 
                            {
                                "siteId" : {
                                    "$eq" : "MAIN"
                                }
                            }, 
                            {
                                "status" : {
                                    "$eq" : "ACTIVE"
                                }
                            }, 
                            {
                                "creationDate" : {
                                    "$lt" : ISODate("2017-10-20T17:58:17.966Z")
                                }
                            }, 
                            {
                                "creationDate" : {
                                    "$gt" : ISODate("2017-10-12T17:58:17.809Z")
                                }
                            }, 
                            {
                                "reportCount" : {
                                    "$gt" : 0.0
                                }
                            }, 
                            {
                                "$nor" : [ 
                                    {
                                        "assignee" : {
                                            "$exists" : true
                                        }
                                    }
                                ]
                            }
                        ]
                    },
                    "inputStage" : {
                        "stage" : "IXSCAN",
                        "keyPattern" : {
                            "parent" : 1.0
                        },
                        "indexName" : "parent_1",
                        "isMultiKey" : false,
                        "multiKeyPaths" : {
                            "parent" : []
                        },
                        "isUnique" : false,
                        "isSparse" : false,
                        "isPartial" : false,
                        "indexVersion" : 2,
                        "direction" : "forward",
                        "indexBounds" : {
                            "parent" : [ 
                                "[null, null]"
                            ]
                        }
                    }
                }
            }
        }, 
        {
            "stage" : "SORT",
            "sortPattern" : {
                "creationDate" : 1.0
            },
            "inputStage" : {
                "stage" : "SORT_KEY_GENERATOR",
                "inputStage" : {
                    "stage" : "FETCH",
                    "filter" : {
                        "$and" : [ 
                            {
                                "$nor" : [ 
                                    {
                                        "assignee" : {
                                            "$exists" : true
                                        }
                                    }
                                ]
                            }, 
                            {
                                "siteId" : {
                                    "$eq" : "MAIN"
                                }
                            }, 
                            {
                                "status" : {
                                    "$eq" : "ACTIVE"
                                }
                            }, 
                            {
                                "creationDate" : {
                                    "$lt" : ISODate("2017-10-20T17:58:17.966Z")
                                }
                            }, 
                            {
                                "creationDate" : {
                                    "$gt" : ISODate("2017-10-12T17:58:17.809Z")
                                }
                            }, 
                            {
                                "reportCount" : {
                                    "$gt" : 0.0
                                }
                            }, 
                            {
                                "$nor" : [ 
                                    {
                                        "parent" : {
                                            "$exists" : true
                                        }
                                    }
                                ]
                            }
                        ]
                    },
                    "inputStage" : {
                        "stage" : "IXSCAN",
                        "keyPattern" : {
                            "assignee" : 1.0
                        },
                        "indexName" : "assignee_1",
                        "isMultiKey" : false,
                        "multiKeyPaths" : {
                            "assignee" : []
                        },
                        "isUnique" : false,
                        "isSparse" : false,
                        "isPartial" : false,
                        "indexVersion" : 2,
                        "direction" : "forward",
                        "indexBounds" : {
                            "assignee" : [ 
                                "[null, null]"
                            ]
                        }
                    }
                }
            }
        }, 
        {
            "stage" : "SORT",
            "sortPattern" : {
                "creationDate" : 1.0
            },
            "inputStage" : {
                "stage" : "SORT_KEY_GENERATOR",
                "inputStage" : {
                    "stage" : "FETCH",
                    "filter" : {
                        "$and" : [ 
                            {
                                "status" : {
                                    "$eq" : "ACTIVE"
                                }
                            }, 
                            {
                                "creationDate" : {
                                    "$lt" : ISODate("2017-10-20T17:58:17.966Z")
                                }
                            }, 
                            {
                                "creationDate" : {
                                    "$gt" : ISODate("2017-10-12T17:58:17.809Z")
                                }
                            }, 
                            {
                                "reportCount" : {
                                    "$gt" : 0.0
                                }
                            }, 
                            {
                                "$nor" : [ 
                                    {
                                        "assignee" : {
                                            "$exists" : true
                                        }
                                    }
                                ]
                            }, 
                            {
                                "$nor" : [ 
                                    {
                                        "parent" : {
                                            "$exists" : true
                                        }
                                    }
                                ]
                            }
                        ]
                    },
                    "inputStage" : {
                        "stage" : "IXSCAN",
                        "keyPattern" : {
                            "siteId" : 1.0,
                            "updatedDate" : 1.0,
                            "label" : 1.0
                        },
                        "indexName" : "siteId_1_updatedDate_1_label_1",
                        "isMultiKey" : false,
                        "multiKeyPaths" : {
                            "siteId" : [],
                            "updatedDate" : [],
                            "label" : []
                        },
                        "isUnique" : false,
                        "isSparse" : false,
                        "isPartial" : false,
                        "indexVersion" : 2,
                        "direction" : "forward",
                        "indexBounds" : {
                            "siteId" : [ 
                                "[\"MAIN\", \"MAIN\"]"
                            ],
                            "updatedDate" : [ 
                                "[MinKey, MaxKey]"
                            ],
                            "label" : [ 
                                "[MinKey, MaxKey]"
                            ]
                        }
                    }
                }
            }
        }, 
        {
            "stage" : "SORT",
            "sortPattern" : {
                "creationDate" : 1.0
            },
            "inputStage" : {
                "stage" : "SORT_KEY_GENERATOR",
                "inputStage" : {
                    "stage" : "FETCH",
                    "filter" : {
                        "$and" : [ 
                            {
                                "$nor" : [ 
                                    {
                                        "parent" : {
                                            "$exists" : true
                                        }
                                    }
                                ]
                            }, 
                            {
                                "$nor" : [ 
                                    {
                                        "assignee" : {
                                            "$exists" : true
                                        }
                                    }
                                ]
                            }, 
                            {
                                "siteId" : {
                                    "$eq" : "MAIN"
                                }
                            }, 
                            {
                                "status" : {
                                    "$eq" : "ACTIVE"
                                }
                            }, 
                            {
                                "creationDate" : {
                                    "$lt" : ISODate("2017-10-20T17:58:17.966Z")
                                }
                            }, 
                            {
                                "creationDate" : {
                                    "$gt" : ISODate("2017-10-12T17:58:17.809Z")
                                }
                            }, 
                            {
                                "reportCount" : {
                                    "$gt" : 0.0
                                }
                            }
                        ]
                    },
                    "inputStage" : {
                        "stage" : "AND_SORTED",
                        "inputStages" : [ 
                            {
                                "stage" : "IXSCAN",
                                "keyPattern" : {
                                    "parent" : 1.0
                                },
                                "indexName" : "parent_1",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                    "parent" : []
                                },
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 2,
                                "direction" : "forward",
                                "indexBounds" : {
                                    "parent" : [ 
                                        "[null, null]"
                                    ]
                                }
                            }, 
                            {
                                "stage" : "IXSCAN",
                                "keyPattern" : {
                                    "assignee" : 1.0
                                },
                                "indexName" : "assignee_1",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                    "assignee" : []
                                },
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 2,
                                "direction" : "forward",
                                "indexBounds" : {
                                    "assignee" : [ 
                                        "[null, null]"
                                    ]
                                }
                            }
                        ]
                    }
                }
            }
        }
    ]
},
"executionStats" : {
    "executionSuccess" : true,
    "nReturned" : 19,
    "executionTimeMillis" : 8,
    "totalKeysExamined" : 533,
    "totalDocsExamined" : 56,
    "executionStages" : {
        "stage" : "FETCH",
        "filter" : {
            "$and" : [ 
                {
                    "siteId" : {
                        "$eq" : "MAIN"
                    }
                }, 
                {
                    "status" : {
                        "$eq" : "ACTIVE"
                    }
                }, 
                {
                    "$nor" : [ 
                        {
                            "assignee" : {
                                "$exists" : true
                            }
                        }
                    ]
                }, 
                {
                    "$nor" : [ 
                        {
                            "parent" : {
                                "$exists" : true
                            }
                        }
                    ]
                }
            ]
        },
        "nReturned" : 19,
        "executionTimeMillisEstimate" : 0,
        "works" : 534,
        "advanced" : 19,
        "needTime" : 513,
        "needYield" : 0,
        "saveState" : 20,
        "restoreState" : 20,
        "isEOF" : 1,
        "invalidates" : 0,
        "docsExamined" : 56,
        "alreadyHasObj" : 0,
        "inputStage" : {
            "stage" : "IXSCAN",
            "nReturned" : 56,
            "executionTimeMillisEstimate" : 0,
            "works" : 533,
            "advanced" : 56,
            "needTime" : 476,
            "needYield" : 0,
            "saveState" : 20,
            "restoreState" : 20,
            "isEOF" : 1,
            "invalidates" : 0,
            "keyPattern" : {
                "creationDate" : 1.0,
                "reportCount" : 1.0,
                "label" : 1.0
            },
            "indexName" : "creationDate_1_reportCount_1_label_1",
            "isMultiKey" : false,
            "multiKeyPaths" : {
                "creationDate" : [],
                "reportCount" : [],
                "label" : []
            },
            "isUnique" : false,
            "isSparse" : false,
            "isPartial" : false,
            "indexVersion" : 2,
            "direction" : "forward",
            "indexBounds" : {
                "creationDate" : [ 
                    "(new Date(1507831097809), new Date(1508522297966))"
                ],
                "reportCount" : [ 
                    "(0.0, inf.0]"
                ],
                "label" : [ 
                    "[MinKey, MaxKey]"
                ]
            },
            "keysExamined" : 533,
            "seeks" : 477,
            "dupsTested" : 0,
            "dupsDropped" : 0,
            "seenInvalidated" : 0
        }
    }
},

"ok" : 1.0
}

查询仍需要700-800 ms才能返回数据。如何更改索引以使查询运行得更快?不要考虑"keysExamined" : 533, "seeks" : 477,此数据。这只是测试数据。

看起来它使用索引但只是索引中的第一个字段? multuKey也是假的吗?

1 个答案:

答案 0 :(得分:2)

解释计划输出的几个关键点:

  • 该查询解决了以下属性:siteId, status, creationDate, reportCount, assignee, parent
  • 获奖计划分为两个阶段:
    • IX_SCAN使用creationDate_1_reportCount_1_label_1,它使用creationDatereportCount上的索引查找来识别56个文档,然后将这些文档转发到FETCH阶段
    • FETCH从IX_SCAN阶段接收56个文档,然后询问这些文档以应用siteIdstatusassigneeparent过滤器。该询问导致37个文件被丢弃,导致19个文件被退回。

因此,您的索引仅涵盖查询中6个属性中的2个,并且通过检查文档而不是索引来应用查询中的其余4个属性。如果您希望此查询完全覆盖索引,请创建以下索引:

db.collection.createIndex(
    {siteId: 1, status: 1, creationDate: 1, reportCount: 1, assignee: 1, parent: 1}
) 

如果您使用此索引运行,那么您应该发现(a)MongoDB选择此索引和(b)IX_SCAN阶段转发的文档数与您的查找调用返回的文档数相同。

我说"应该找到" ,因为这里还有其他方面可能导致MongoDB选择不同的索引,例如使用$nor和排序阶段(creationDate: 1)。我建议调整索引并运行解释' on'每次调整后,在executionStats子文档中查找这些关键项:

  • " nReturned"
  • " totalKeysExamined"
  • " totalDocsExamined"

一个简单的经验法则是:totalKeysExamined越接近nReturned,越接近totalDocsExamined为零......索引覆盖范围越大。

还有一个问题是索引的成本(就写入时间和索引存储的影响而言),因此我建议考虑您的非功能性要求 - 如果没有完整的索引覆盖,您可以实现所需的经过时间吗? ?如果没有,那么你应该继续进行经验测试,但要准备好根据explain()输出告诉你的内容来调整你的选择。

相关问题