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

为什么 Python peewee 第一次运行时间很长

  •  
  •   lixuda · Aug 1, 2020 · 3807 views
    This topic created in 2297 days ago, the information mentioned may be changed or developed.

    数据库 mysql,10 多万条数据,排序后获取 N 条,第一次运行需要几秒,但是再运行获取只需要 0.0 几秒

    哪位大神赐教下,谢谢

    Supplement 1  ·  Aug 1, 2020
    是用在 flask 上,并索引。
    同一数据库,用 php 写,读取列表没有问题,换成 flask+peewee,就读取很慢
    Supplement 2  ·  Aug 8, 2020
    现在用 from playhouse.flask_utils import FlaskDB
    方式链接数据库,这个问题貌似解决了,没有出现长时间
    13 replies  •  2020-08-11 09:30:41 +08:00
    pabupa
    pabupa
    Aug 1, 2020 via Android
    1
    主机名是 localhost 还是 127 ?
    RyougiShiki
    RyougiShiki
    Aug 1, 2020
    2
    排序就是比较耗时,不过十万条不算很多,加索引试试,再次快因为数据库有缓存。
    lixuda
    lixuda
    OP
    Aug 1, 2020
    3
    @pabupa localhost,127 测试下也一样情况
    loading
    loading
    Aug 1, 2020
    4
    你用其他图形化管理工具连 mysql,然后泡一下你的 SQL,你看看。
    ClericPy
    ClericPy
    Aug 1, 2020
    5
    PySnooper 看看?

    有点怀疑连接池惰性初始化
    sujin190
    sujin190
    Aug 1, 2020
    6
    没索引吧,首次查询全表扫描肯定很慢,之后的很快是因为查询缓存的存在吧,估计不是 peewee 的问题,你可以直接 cli 命令行试试 sql
    lixuda
    lixuda
    OP
    Aug 1, 2020
    7
    @sujin190 已索引的
    black11black
    black11black
    Aug 2, 2020
    8
    十万排序对 py 和 mysql 都不是瓶颈,排序业务,有明显迟滞感的量级目前对 py 来说是亿级,mysql 是千万级。建议业务代码 debug,或者新建裸环境测试 peewee
    lixuda
    lixuda
    OP
    Aug 2, 2020
    9
    @black11black 应该是我对 peewee 不熟,不知道哪里出问题
    dogeglat
    dogeglat
    Aug 2, 2020
    10
    @ClericPy
    正解

    可以 profile 看下耗时在哪
    summerwar
    summerwar
    Aug 2, 2020
    11
    代码类问题 尽量贴代码,不然都是瞎猜
    di94sh
    di94sh
    Aug 8, 2020 via iPhone
    12
    logging 打出 sql 来看执行计划
    lixuda
    lixuda
    OP
    Aug 11, 2020
    13
    @ClericPy 用 connect("mysql+pool: 连接池方式链接,没有加载的时间了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1988 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 14:59 · PVG 22:59 · LAX 06:59 · JFK 09:59
    ♥ Do have faith in what you're doing.