基于小米SAOR实现SQL自助优化接口

Posted _雪辉_

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于小米SAOR实现SQL自助优化接口相关的知识,希望对你有一定的参考价值。

#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import pymysql
import os
import json
import time
import logging
import subprocess
from connect_db_forbatch import connect_mysql
from flask_cors import *
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
from flask import Flask,request,jsonify
app = Flask(__name__)

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logdate = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
log_path = '/data/mysql/logs/'
log_name = log_path + logdate + '.log'
logfile = log_name
fh = logging.FileHandler(logfile, mode='w')
fh.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
fh.setFormatter(formatter)
logger.addHandler(fh)

@app.route('/optimizesql/', methods=['POST'])
def soar():
    try:
        if not request.data:  #检测是否有数据
            return ('fail')
        params= request.data.decode('utf-8')
        prams = json.loads(params)
        clustername = prams['clustername']
        dbname = prams['dbname']
        sql = prams['sql'].replace('\\\\','').replace('`','\\`')
        #获取mysql实例的元数据信息
        get_ip = "select ip,port from mysql_instance where clustername='%s'" %(clustername)
        host = connect_mysql('127.0.0.1', 33071, 'dbname', 'tablename').select_metadata(get_ip)
        ip = host[0][0]
        port = host[0][1]
        html_cmd = "soar -online-dsn=\\"user:password@%s:%s/%s\\" -test-dsn=\\"user:password@host:port/optimizesql\\" -query=\\"%s\\"  -report-type markdown -log-output=/data/ansible/logs/optimizesql.log" %(ip,port,dbname,sql)
        (status, content) = subprocess.getstatusoutput(html_cmd)
        if status != 0 :
            content = "analyze failed"
        return jsonify(content)
    except Exception as err:
        logger.error('%s',err, exc_info=True)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5590)

以上是关于基于小米SAOR实现SQL自助优化接口的主要内容,如果未能解决你的问题,请参考以下文章

分享一个基于小米 soar 的开源 sql 分析与优化的 WEB 图形化工具

SQL自动审核-自助上线平台

Qunit零侵入切面技术应用及分布式运行平台

《java精品毕设》基于javaweb宠物领养平台管理系统(源码+毕设论文+sql):主要实现:个人中心,信息修改,填写领养信息,交流论坛,新闻,寄养信息,公告,宠物领养信息,我的寄养信息等(代码片段

小米工程师提交优化补丁被批,Linux内核维护者:太疯狂!

Centos7下安装小米SQL优化工具SOAR