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

sqlite 批量查询问题

  •  
  •   SlipStupig · 2016-09-09 09:58:09 +08:00 · 3965 次点击
    这是一个创建于 2788 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我有一个查询需要查询多个 id ,我现在采用的是 map(lambda x:sqlite3.execute(param), param)但是觉得好慢好慢,请教一下各位怎么才能解决
    7 条回复    2016-09-09 13:52:26 +08:00
    chevalier
        1
    chevalier  
       2016-09-09 10:05:00 +08:00   ❤️ 1
    用 in ,一次查询多个 ID 的数据回来存到字典,再用。原则是尽量减少跟数据库之间的交互。
    suueyoung
        2
    suueyoung  
       2016-09-09 10:25:13 +08:00
    懒汉如我, 就找 orm 做了.
    codehz
        3
    codehz  
       2016-09-09 10:32:26 +08:00
    sqlite3 的话,一般来说没必要做这个优化。。。。如果觉得有性能问题,先 profile 查查谁最耗时。。。
    poorguy
        4
    poorguy  
       2016-09-09 10:40:05 +08:00
    应该在 SQL 层面优化,你的 param 是怎么写的?查询的 id 是否有规律?
    SlipStupig
        5
    SlipStupig  
    OP
       2016-09-09 11:37:20 +08:00
    @poorguy
    @chevalier
    select price from product_price where id=1,问题在于我一行一个只对应一个价格,但是一个商品有多个价格,我去过用 in 我就搞不清楚哪个价格是哪个商品的了
    clino
        6
    clino  
       2016-09-09 11:39:49 +08:00   ❤️ 1
    即使你要查这么多次,也可以考虑在一个事务里做,这样可能会快不少
    poorguy
        7
    poorguy  
       2016-09-09 13:52:26 +08:00   ❤️ 1
    @SlipStupig 还是没搞懂你的需求,感觉两次查询可以实现: 1. 查某商品的 product_price 的 ids , 2. select price from product_price where id in ids

    退一步说,楼上用事务的方案也是可行的,总比你不停查询数据库好
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3407 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 11:09 · PVG 19:09 · LAX 04:09 · JFK 07:09
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.