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

利用 django 的 management 写了一个可执行命令,更新表内字段值,但是因为数据量比较大( 200W),导致程序报错,内存爆了。如何优化呢?

  •  
  •   no13bus ·
    no13bus · Nov 25, 2014 · 3307 views
    This topic created in 4325 days ago, the information mentioned may be changed or developed.
    下面是代码


    看网上有使用F方法的,但是F不支持多个models之间的值进行操作。我使用了iterator将orm的缓存释放,但是依旧不行。还有的说是分段执行,这个分段执行是什么意思呢?请大家指点下有什么优化办法。
    2 replies  •  2014-11-26 09:22:56 +08:00
    mengzhuo
        1
    mengzhuo  
       Nov 25, 2014
    打开一个临时文件,然后按buffer量写入,直到写完
    然后把文件hash之后移动到media目录
    返回URL即可

    又让我想到当年那4G的csv表格了……
    raquelken
        2
    raquelken  
       Nov 26, 2014
    UPDATE models_b, models_a
    SET models_b.value_b = models_b.value_b + models_a.value_a + models_a.value_a_1
    WHERE models_b.foreign_id = models_a.id and models_b.value_b <> 0.0
    这不是一条sql搞定的事情么?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   3033 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 11:15 · PVG 19:15 · LAX 04:15 · JFK 07:15
    ♥ Do have faith in what you're doing.