码迷,mamicode.com
首页 > 数据库 > 详细

MySQL检查冗余索引代码

时间:2016-10-01 06:49:37      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:mysql   python   

  接手了个新需求检查公司所有库表冗余索引,保存下代码复用。

#!/usr/bin/python2.7

# -*- coding: utf-8 -*-

#liding@xywy.com

import MySQLdb

import sys

import os

import time,datetime

import logging

import mysqlcommon



path = sys.path[0]

fpath = os.path.dirname(path) + "/"

sys.path.append(fpath)


from instance import base

from lib import mysqllib

from lib import maillib

from lib import configgetter


getter = configgetter.Configuration("conf/global.cnf")

luser = getter.get("mysqlha", "user")

lpasswd = getter.get("mysqlha", "passwd")



email_addr = ‘dba@xywy.com‘


def MySQL_conn(host,port,db,sql):

    db = MySQLdb.connect(host=host, port=port, user=luser, passwd=lpasswd, db=db)

    cursor = db.cursor()

    cursor.execute(sql)

    result = cursor.fetchall()

    return result


def get_MySQL(host,port,db,tname):

#最长的索引数,也就是循环的次数

    sql = "select max(SEQ_IN_INDEX) from information_schema.STATISTICS where table_schema=‘%s‘ \

    and table_name=‘%s‘;" % (db,tname)

    num = MySQL_conn(host,port,db,sql)

    num = num[0][0]

    index_na_all=[]

    status = []

    if num != None:

        for numb in range(num):

            a = numb+1

            b = numb +2

            #查询第一个位置重复的字段名

            if a == 1:

                sql1 = "select COLUMN_NAME  from information_schema.STATISTICS where  table_schema=‘%s‘ and table_name=‘%s‘ \

                        and SEQ_IN_INDEX = %s and INDEX_NAME != ‘PRIMARY‘ group by COLUMN_NAME having count(*) >1;" % (db,tname,a)

            else:

                sql1 = "select COLUMN_NAME  from information_schema.STATISTICS where  table_schema=‘%s‘ and table_name=‘%s‘ \

                        and SEQ_IN_INDEX = %s and INDEX_NAME in (%s)  group by COLUMN_NAME having count(*) >1;" % (db,tname,a,idx_tw1)

            data = MySQL_conn(host,port,db,sql1)

            if data == ():

                break

            alist = []

            for data1 in data:

                alist.append(data1[0])

                colu = ‘\‘,\‘‘.join(alist)

                colu = "\‘" + colu + "\‘"

            #查询第一个位置相同的索引名

            if a == 1:

                sql2 = "select INDEX_NAME  from information_schema.STATISTICS where  table_schema=‘%s‘ and table_name=‘%s‘ \

                        and SEQ_IN_INDEX = %s and COLUMN_NAME in (%s);" % (db,tname,a,colu)

            else:

                sql2 = "select INDEX_NAME  from information_schema.STATISTICS where  table_schema=‘%s‘ and table_name=‘%s‘ \

                        and SEQ_IN_INDEX = %s and COLUMN_NAME in (%s) and INDEX_NAME IN (%s) ;" % (db,tname,a,colu,idx_tw1)

            in_name = []

            index_name = MySQL_conn(host,port,db,sql2)

            index_na = [i[0] for i in index_name]

            if index_na == []:

                continue

            for iname in index_na:

                in_name.append(iname)

            inname = ‘\‘,\‘‘.join(in_name)

            inname = "\‘" + inname + "\‘"

            #第一个位置相同而且有第二个位置的索引名

            sql4 = "select INDEX_NAME  from information_schema.STATISTICS where  table_schema=‘%s‘ and table_name=‘%s‘  \

                       and INDEX_NAME in (%s)  and SEQ_IN_INDEX=%s;" % (db,tname,inname,b)

            idx_two = MySQL_conn(host,port,db,sql4)

            idx_tw = [i[0] for i in idx_two]

            idx_tw1 = ‘\‘,\‘‘.join(idx_tw)

            idx_tw1 = "\‘" + idx_tw1 + "\‘"


            for idna in idx_tw:

                if idna in index_na:

                    index_na.remove(idna)

                    if index_na != []:

                        index_na_all.append(index_na)

    if index_na_all != []:

        produc = base.getProductByPort(port)

        prod = produc[‘result‘].values()

        if prod:

            produc = prod[0]

        else:

            produc = ‘‘

        if index_na_all[0] != []:

            status.append([port,produc,db,tname,index_na_all[0]])

            return status[0]

        else :

            return 123

    else :

        return 123








def call_MySQL():

    db = ‘backup‘

    host = ‘10.20.6.5‘

    port = 3440

    sql = "select  host,port from policy  ;"

    allph = MySQL_conn(host,port,db,sql)

    showdb = ‘mysql‘

    showsql = "show databases;"

    system_lib = ((‘information_schema‘,), (‘mondmm‘,), (‘mysql‘,), (‘performance_schema‘,))

    statu = []

    for i in allph:

        port = i[1]

        host = i[0]

        dbname = MySQL_conn(host,port,showdb,showsql)

        rdbname = set(dbname) - set(system_lib)

        for rname in rdbname:

            namesql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ‘%s‘;" % rname

            rtable_name = MySQL_conn(host,port,rname[0],namesql)

            for table_name in rtable_name:

                result = get_MySQL(host,int(port),rname[0],table_name[0])

                if result != 123:

                    statu.append(result)

    SendMail(statu)


def SendMail(statu):

    title = ‘report(数据库冗余索引报表_%s)‘ % (datetime.datetime.now().strftime(‘%Y-%m-%d‘))

    col_name = [‘端口‘,‘产品线‘,‘库名‘,‘表名‘,‘索引名‘]

    maillib.sendHtml(email_addr,title,statu,col_name)


call_MySQL()


本文出自 “游走的风” 博客,请务必保留此出处http://774148515.blog.51cto.com/10937712/1858066

MySQL检查冗余索引代码

标签:mysql   python   

原文地址:http://774148515.blog.51cto.com/10937712/1858066

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!