V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
4ever911
V2EX  ›  Python

MongoDB + SSH 转发的问题请教

  •  
  •   4ever911 · 2019-01-24 21:27:51 +08:00 · 1305 次点击
    这是一个创建于 1916 天前的主题,其中的信息可能已经有所发展或是发生改变。

    因为安全问题,服务器上的 MongoDB 设置为只允许服务器本地访问,所以在调试程序的时候,我用 SSH 转发,把端口映射到 Client 本地,读写都没问题。

    我担心网络传输会影响性能,所以随便写了一行代码作了下测试:

    大致上是

    def Search(): db.mycollection.find_one({'blabla':'blabla'})

    timeit(Search, number=1)

    发现这条执行的时间只有 n.e-7, 相当不到 0.000001 秒,

    这是如何做到的? 主机在美国,ping 一下也要 200ms 呀,这数据如何这么快就查询过来了?

    3 条回复    2019-01-24 23:00:20 +08:00
    zeraba
        1
    zeraba  
       2019-01-24 22:29:24 +08:00 via Android
    看代码的逻辑 都已经告诉你了,统计 Search 函数的执行时间,没统计结果从远程到本地的时间啊
    Vegetable
        2
    Vegetable  
       2019-01-24 22:49:23 +08:00
    还真没注意过,因为 findOne 一般都直接当 dict 用了,感觉不像是 lazyobject,我也去测试一下
    Vegetable
        3
    Vegetable  
       2019-01-24 23:00:20 +08:00
    ```
    from pymongo import MongoClient
    import timeit
    db = MongoClient()
    co = db["Risk"]["orders"]

    def search():co.find_one({})

    print(timeit.timeit(search,number=1))
    ```
    我这段 0.01s,符合预期,没有复现你的问题
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   981 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 21:13 · PVG 05:13 · LAX 14:13 · JFK 17:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.