表行数约 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 秒,想不出是什么问题
当表只有几万行的时候不会这么慢