设为首页 加入收藏

TOP

pymongo的比较排序查询性能比较,sort参数和sort函数, find和find_one
2017-09-30 17:48:29 】 浏览:1426
Tags:pymongo 比较 排序 查询 性能 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
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Python开发【笔记】:单线程下执.. 下一篇python 深、浅拷贝

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目