参考 1:https://www.cnblogs.com/rgcLOVEyaya/p/RGC_LOVE_YAYA_605days.html

参考 2:https://gitee.com/Green-unicorn/codes/1u8rlt03gvpic7xfwz9je12

engine = create_engine(ProdConfig.SQLALCHEMY_DATABASE_URI, echo=True)
 
app = Flask(__name__)
app.config['FLASKY_DB_QUERY_TIMEOUT'] = 0.0001  # 设置 sql 执行超时时间,# 记录执行时间超过 0.0001 秒的
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True  # 断开设置
app.config['SQLALCHEMY_RECORD_QUERIES'] = True  # 启用慢查询记录功能
 
 
from flask_sqlalchemy import get_debug_queries
 
 
@app.after_request
def after_request(response):
    for query in get_debug_queries():
        if query.duration >= app.config['FLASKY_DB_QUERY_TIMEOUT']:
            print('#####Slow query:%s \nParameters:%s \nDuration:%fs\nContext:%s\n #####' %
                  (query.statement, query.parameters, query.duration,query.context))  # 打印超时 sql 执行信息
    return response
 
 
@app.teardown_request
def handle_teardown_request(ex):
    db.session.remove()