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

sqlalchemy 提交后初次主键查询很慢,求解

  •  
  •   unknowfly · 2021-07-05 12:33:29 +08:00 · 1068 次点击
    这是一个创建于 1239 天前的主题,其中的信息可能已经有所发展或是发生改变。

    表行数约 100w

    操作:在一个接口插入一条数据,返回新插入的数据,获取 commit 后的对象数据的时候会查询一次数据库

    session.add(inst)
    session.commit()
    print(inst.id)
    
    2021-07-05 11:33:11,331 INFO sqlalchemy.engine.Engine BEGIN (implicit)
    2021-07-05 11:33:11,333 INFO sqlalchemy.engine.Engine INSERT INTO system_file_info (id, create_time, update_time, create_by, update_by, file_name, file_bucket, file_type, origin_name, file_size, extra_data, content_type) VALUES (%(id)s, %(create_time)s, %(update_time)s, %(create_by)s, %(update_by)s, %(file_name)s, %(file_bucket)s, %(file_type)s, %(origin_name)s, %(file_size)s, %(extra_data)s, %(content_type)s)
    2021-07-05 11:33:11,333 INFO sqlalchemy.engine.Engine [generated in 0.00039s] {'id': 1411890876728463360, 'create_time': datetime.datetime(2021, 7, 5, 11, 33, 11, 333675), 'update_time': datetime.datetime(2021, 7, 5, 11, 33, 11, 333675), 'create_by': None, 'update_by': None, 'file_name': 'd49402ac-fc36-4ec6-8119-02429be3a119.JPEG', 'file_bucket': 'ga-backend-default', 'file_type': 'jpg', 'origin_name': 'src=http___www.jxatkeji.com_uploads_allimg_160115_1-16011516211V28.jpg&refer=http___www.jxatkeji.jpg', 'file_size': '190264', 'extra_data': '', 'content_type': 'image/jpeg'}
    2021-07-05 11:33:11,342 INFO sqlalchemy.engine.Engine COMMIT
    create cost 0.29399919509887695
    2021-07-05 11:33:11,431 INFO sqlalchemy.engine.Engine BEGIN (implicit)
    2021-07-05 11:33:11,434 INFO sqlalchemy.engine.Engine SELECT system_file_info.id AS system_file_info_id, system_file_info.create_time AS system_file_info_create_time, system_file_info.update_time AS system_file_info_update_time, system_file_info.create_by AS system_file_info_create_by, system_file_info.update_by AS system_file_info_update_by, system_file_info.file_name AS system_file_info_file_name, system_file_info.file_bucket AS system_file_info_file_bucket, system_file_info.file_type AS system_file_info_file_type, system_file_info.origin_name AS system_file_info_origin_name, system_file_info.file_size AS system_file_info_file_size, system_file_info.extra_data AS system_file_info_extra_data, system_file_info.content_type AS system_file_info_content_type, system_file_info.related_id AS system_file_info_related_id 
    FROM system_file_info 
    WHERE system_file_info.id = %(pk_1)s
    2021-07-05 11:33:11,434 INFO sqlalchemy.engine.Engine [generated in 0.00027s] {'pk_1': 1411890876728463360}
    1411890876728463360
    refresh cost 0.9015250205993652
    

    可以看到插入完之后根据主键查询刚刚插入的行花了 0.9 秒,但是其实在查询接口同样的 sql 查询只要 0.1 秒,想不出是什么问题

    当表只有几万行的时候不会这么慢

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1699 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 16:46 · PVG 00:46 · LAX 08:46 · JFK 11:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.