简体   繁体   English

索引会使find_one()更快吗?

[英]Will indexing make find_one() any faster?

If I have a pymongo query, in a collection with around 4000 documents, like the following: 如果我有一个pymongo查询,则包含约4000个文档,如下所示:

mong  =  pymongo.Connection()['ASD_2']['APS2'] 
py_mong = mong.find_one({'plate':'123456'})

Considering there is no .explain()["cursor"] or .explain()["nscanned"] allowed on find_one() , and therefore no method at hand to find out the nature of the scan, can anyone tell me if it's worth indexing the collection when I am only going to be using find_one() ? 考虑到有没有.explain()["cursor"].explain()["nscanned"]允许在find_one()因此没有在手的方法来找出扫描的性质,任何人都可以告诉我,如果它是值得在仅使用find_one()时索引集合吗?

是的,建立索引将避免线性搜索所需要的板并在对数时间内工作(快得多)。

Yes, absolutely. 是的,一点没错。

How do I know? 我怎么知道?

First, it would be utterly unacceptable if it didn't. 首先,如果不是这样,那将是完全不可接受的。 People would have complained about the surprising and unnecessary slowness of find_one as compared to find , and the dev team would have fixed it (or risk being viewed as dumb). 人们可能会抱怨find_onefind相比令人惊讶和不必要的缓慢,而开发团队将修复它(否则可能会被视为愚蠢的东西)。

Second, I checked the code. 其次,我检查了代码。 The implementation is exactly what you'd expect, meaning find_one is a mere wrapper around find : 该实现正是您所期望的,这意味着find_one只是对find包装:

def find_one(self, spec_or_id=None, *args, **kwargs):
    if spec_or_id is not None and not isinstance(spec_or_id, dict):
        spec_or_id = {"_id": spec_or_id}
    for result in self.find(spec_or_id, *args, **kwargs).limit(-1):
        return result
    return None

(pymongo version 2.4.2) (pymongo版本2.4.2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM