使用pymongo在mongodb中多次查詢單個字段的執行速度 (Querying single field multiple times execution speed in mongodb using pymongo)


問題描述

使用pymongo在mongodb中多次查詢單個字段的執行速度 (Querying single field multiple times execution speed in mongodb using pymongo)

pymongo 中的兩個查詢的執行時間是否存在差異:

db.collection.find({'date': {'$gte': datetime(2000, 01, 01), '$lt': datetime(2016, 11, 23)}})

db.collection.find({'date': {'$gte': datetime(2000, 01, 01)}, 'date': {'$lt': datetime(2016, 11, 23)}})

在第二種情況下,我查詢同一個字段兩次。我使用 mongo shell 檢查並運行 .explain("executionStats") 唯一的區別是查詢 ("queryPlanner.parsedQuery") 是第一個查詢:

"$and" : [
    {
        "date" : {
            "$lt" : ISODate("2016‑11‑23T00:00:00Z")
        }
    },
    {
        "date" : {
            "$gte" : ISODate("2000‑01‑01T00:00:00Z")
        }
    }
]

這是第二個:

"date" : {
    "$lt" : ISODate("2016‑11‑23T00:00:00Z")
}

我正在查詢一個包含數万個文檔的集合,並且被多次查詢。所以,我需要優化查詢。


參考解法

方法 1:

If you look closely at your second query ‑

{'date': {'$gte': datetime(2000, 01, 01)}, 'date': {'$lt': datetime(2016, 11, 23)}}

You should observe that this is an object(dict in python) having keys date and date, which of course makes no sense. You are trying to create an object with same keys. Thus, only one of them will be valid (the second one in this case). Thus the query to mongo is {date': {'$lt': datetime(2016, 11, 23)}} which wont give you the desired output.

I would suggest that you should define the query in terms of logical operators as clearly as possible. This would be the output of the explain stage in this case. Thus using $and to express two different conditions should be the best way to query.

{"$and" : [
    {"date" : {"$lt" : ISODate("2016‑11‑23T00:00:00Z")}},
    {"date" : {"$gte" : ISODate("2000‑01‑01T00:00:00Z")}}
]}

Coming to the topic of how to make the query faster, you should be using an index on date. Defining this index as a individual index {date: 1} would help narrow down to the relevant docs.

(by Lokesh Meherhyades)

參考文件

  1. Querying single field multiple times execution speed in mongodb using pymongo (CC BY‑SA 2.5/3.0/4.0)

#pymongo #mongoDB






相關問題

InvalidDocument:無法編碼對象:<pymongo.cursor.Cursor 對象位於 (InvalidDocument: Cannot encode object: <pymongo.cursor.Cursor object at)

為什麼 PyMongo 將 uuid.uuid1() 編碼為 BSON::Binary? (Why does PyMongo encode uuid.uuid1() as a BSON::Binary?)

一起使用 MongoEngine 和 PyMongo (Use MongoEngine and PyMongo together)

從 mongoDB 中提取信息 (Extracting information from mongoDB)

Pymongo, truy vấn tổng hợp nào trong số các truy vấn tổng hợp này sẽ hoạt động tốt hơn (Pymongo, which of these aggregare query will perform better)

查詢嵌入列表中的數組 (Querying for array in embedded list)

使用pymongo在mongodb中多次查詢單個字段的執行速度 (Querying single field multiple times execution speed in mongodb using pymongo)

使用 pymongo 用 CSV 數據更新 mongodb (update mongodb with CSV data using pymongo)

如何刪除 mongodb 和 pymogo 上的子文檔 (How to delete a subdocument on mongodb and pymogo)

有沒有辦法在 MongoDB 的一個語句中添加遞增的 id? (Is there a way to add an incrementing id in one statement in MongoDB?)

刪除文檔中的父對象 (Remove parent object in document)

MongoDB:跨文檔列表中的項目不同 (MongoDB: distinct of items inside a list across documents)







留言討論