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
Gigo163
V2EX  ›  Python

pymongo count 很慢, 求正确姿势

  •  
  •   Gigo163 · 2017-04-30 17:39:00 +08:00 · 1887 次点击
    这是一个创建于 2551 天前的主题,其中的信息可能已经有所发展或是发生改变。

    三万条数据, 每条数据只包含一个随机数 {"digit": 随机数}
    要求: 统计出现最多次数的数字
    数据库表 table

    def main():
        digits = []
        for d in table.find():
            n = d['digit']
            digits.append(n)
        dig = set(digits)
    
        news = []
        i = 0
        for d in dig:
            c = table.find({"digit": d}).count()
            zz = (d, c)
            news.append(zz)
            print(i)
            i += 1
    
    if __name__ == '__main__':
        start = time.time()
        main()
        print('Cost: {}'.format(time.time() - start))
    

    运行一次需要五六分钟吧, 用多线程开 100 也快不了多少, 风扇还特响...
    请问正确姿势是怎样的

    2 条回复    2017-05-02 19:00:33 +08:00
    moonlitlaputa
        1
    moonlitlaputa  
       2017-05-02 07:53:38 +08:00
    aggregate
    Gigo163
        2
    Gigo163  
    OP
       2017-05-02 19:00:33 +08:00
    @moonlitlaputa 是的, 忘了补充了..thx~
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5439 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 06:57 · PVG 14:57 · LAX 23:57 · JFK 02:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.