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

求一个数据库更新思路

  •  
  •   SlipStupig · 2016-11-30 12:45:15 +08:00 · 1693 次点击
    这是一个创建于 2708 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我有一个业务需要根据上一次数据库的数据进行更新标志位(数据库用的是 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 条回复    2016-11-30 13:50:51 +08:00
    kindjeff
        1
    kindjeff  
       2016-11-30 13:00:11 +08:00
    没太看懂这个代码。这个 cache_list 作为缓存发挥的作用是什么?
    SlipStupig
        2
    SlipStupig  
    OP
       2016-11-30 13:13:54 +08:00
    @kindjeff 缓存上一次查询到的结果
    SlipStupig
        3
    SlipStupig  
    OP
       2016-11-30 13:15:18 +08:00
    @kindjeff 我将上一次数据库里面查询到的结果保存起来,如果相同就不需要再查数据库了
    enenaaa
        4
    enenaaa  
       2016-11-30 13:50:51 +08:00
    那你的目表是什么呢。
    如果嫌查询慢, 那
    1 、启动时一次性将尽量多的数据加载到 cache
    2 、更新时顺带更新 cache
    3 、优化数据库查询
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1781 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 16:17 · PVG 00:17 · LAX 09:17 · JFK 12:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.