pymongo的比較排序查詢性能比較,sort參數和sort函數, find和find_one




sort參數與sort函數

官方文檔中,find函數中的說明表明,在find中傳參應該和cursor后面調用sort函數一樣

find( filter=None, projection=None, skip=0, limit=0, no_cursor_timeout=False, cursor_type=CursorType.NON_TAILABLE, sort=None, allow_partial_results=False, oplog_replay=False, modifiers=None, manipulate=True)


  • sort (optional): a list of (key, direction) pairs specifying the sort order for this query. See sort() for details.


查詢條件和排序條件如下

query = {
# 'start': {'$lte': int_ip},
'end': {'$gte': int_ip}
}
sort = [('end', 1)]

sort作為入參查詢方式
cursor = db.aiwen.find(query, sort=sort, limit=1)

sort函數調用方式
db.aiwen.find(query).sort([('end', 1)]).limit(1)

為了保證格式統一,將find_one替換成find函數
sort參數方式調用 耗時  cost:1.97000002861
sort函數方式調用 耗時  cost:1.97200012207

但用Robomongo工具查詢,發現時延很短
image

find_one函數

cursor = db.aiwen.find_one(query, sort=sort)
發現查詢結果一下得到提升,與robomongo相當
cost:0.00399994850159


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM