推荐学习书目
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
hihihihihi
V2EX  ›  Python

MongoDB + SSH 转发的问题请教

  •  
  •   hihihihihi · Jan 24, 2019 · 1755 views
    This topic created in 2695 days ago, the information mentioned may be changed or developed.

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

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

    大致上是

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

    timeit(Search, number=1)

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

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

    3 replies    2019-01-24 23:00:20 +08:00
    zeraba
        1
    zeraba  
       Jan 24, 2019 via Android
    看代码的逻辑 都已经告诉你了,统计 Search 函数的执行时间,没统计结果从远程到本地的时间啊
    Vegetable
        2
    Vegetable  
       Jan 24, 2019
    还真没注意过,因为 findOne 一般都直接当 dict 用了,感觉不像是 lazyobject,我也去测试一下
    Vegetable
        3
    Vegetable  
       Jan 24, 2019
    ```
    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,符合预期,没有复现你的问题
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   973 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 19:28 · PVG 03:28 · LAX 12:28 · JFK 15:28
    ♥ Do have faith in what you're doing.