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

求一个数据库更新思路

  •  
  •   SlipStupig · Nov 30, 2016 · 2190 views
    This topic created in 3535 days ago, the information mentioned may be changed or developed.

    我有一个业务需要根据上一次数据库的数据进行更新标志位(数据库用的是 mongodb ),当我新数据更新的时候我需要和老数据进行对比决定是否更新,目前已经做了缓存但是还是没有达到我的目标


    • 伪代码:
    
    new_item = queue.get()  #得到一条数据
    
    #查询是否在缓存内
    if new_item in cache_list:  
       return new_item
    
    else:
        query_result = fetch_flag_change(new_item)  #得到标志位是否相同
        if  query_result == signal_1:
             cache_list.update({query_result: signal_1})
        elif query_result == signal_2:
             cache_list.update({query_result: signal_2})
       else:
             return cache_list
    
    

    我认为可能是因为我的数据是一条一条查询的数据库所以很慢,有什么更好的办法能批量查询吗?

    4 replies    2016-11-30 13:50:51 +08:00
    kindjeff
        1
    kindjeff  
       Nov 30, 2016
    没太看懂这个代码。这个 cache_list 作为缓存发挥的作用是什么?
    SlipStupig
        2
    SlipStupig  
    OP
       Nov 30, 2016
    @kindjeff 缓存上一次查询到的结果
    SlipStupig
        3
    SlipStupig  
    OP
       Nov 30, 2016
    @kindjeff 我将上一次数据库里面查询到的结果保存起来,如果相同就不需要再查数据库了
    enenaaa
        4
    enenaaa  
       Nov 30, 2016
    那你的目表是什么呢。
    如果嫌查询慢, 那
    1 、启动时一次性将尽量多的数据加载到 cache
    2 、更新时顺带更新 cache
    3 、优化数据库查询
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2893 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 347ms · UTC 15:00 · PVG 23:00 · LAX 08:00 · JFK 11:00
    ♥ Do have faith in what you're doing.