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

sqlite 批量查询问题

  •  
  •   SlipStupig · Sep 9, 2016 · 4850 views
    This topic created in 3676 days ago, the information mentioned may be changed or developed.
    我有一个查询需要查询多个 id ,我现在采用的是 map(lambda x:sqlite3.execute(param), param)但是觉得好慢好慢,请教一下各位怎么才能解决
    7 replies  •  2016-09-09 13:52:26 +08:00
    chevalier
        1
    chevalier  
       Sep 9, 2016   ❤️ 1
    用 in ,一次查询多个 ID 的数据回来存到字典,再用。原则是尽量减少跟数据库之间的交互。
    suueyoung
        2
    suueyoung  
       Sep 9, 2016
    懒汉如我, 就找 orm 做了.
    codehz
        3
    codehz  
       Sep 9, 2016
    sqlite3 的话,一般来说没必要做这个优化。。。。如果觉得有性能问题,先 profile 查查谁最耗时。。。
    poorguy
        4
    poorguy  
       Sep 9, 2016
    应该在 SQL 层面优化,你的 param 是怎么写的?查询的 id 是否有规律?
    SlipStupig
        5
    SlipStupig  
    OP
       Sep 9, 2016
    @poorguy
    @chevalier
    select price from product_price where id=1,问题在于我一行一个只对应一个价格,但是一个商品有多个价格,我去过用 in 我就搞不清楚哪个价格是哪个商品的了
    clino
        6
    clino  
       Sep 9, 2016   ❤️ 1
    即使你要查这么多次,也可以考虑在一个事务里做,这样可能会快不少
    poorguy
        7
    poorguy  
       Sep 9, 2016   ❤️ 1
    @SlipStupig 还是没搞懂你的需求,感觉两次查询可以实现: 1. 查某商品的 product_price 的 ids , 2. select price from product_price where id in ids

    退一步说,楼上用事务的方案也是可行的,总比你不停查询数据库好
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   884 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 23:05 · PVG 07:05 · LAX 16:05 · JFK 19:05
    ♥ Do have faith in what you're doing.