zss0109211
作者zss0109211·2013-05-29 18:02
数据库管理员·null

SNAPSHOT TOP 10 SQL

字数 4228阅读 1604评论 0赞 0

 

import re,os
import sys,getopt
def usage():
    print("Usage:%s [-h] [-f] [-t]   -f snapshotxxx.out -t topsql");
   
   
   
def TOPSQL(infile):
    myfile=open(infile,'r')
    m_lines=myfile.readlines()
    m_file=''.join(m_lines)
    pattern=re.compile(r'Number of executionss+=.*?n*Statement texts+=.*?n',re.MULTILINE | re.DOTALL)
    DynSQL=pattern.findall(m_file)
   
    Dynsqllist={}
    SortDynsqllist = []
   
    # main
    #analysis the dynamice sql snapshot ,get the top sql
    for i in range(len(DynSQL)):
        Dynallcols=DynSQL[i].split('n')
        Dynallcols=Dynallcols[:len(Dynallcols)-1]
        Dyn_dict={}
        for j in range(len(Dynallcols)):
            Dyncols=Dynallcols[j].split('=',1)
            Dyncols[0]=Dyncols[0].replace(' ','')
            Dyncols[1]=Dyncols[1].replace(' ','',1)
            Dyn_dict[Dyncols[0]]=Dyncols[1]
        Dynsqllist[i]= Dyn_dict
       
    for k  in range(len(Dynsqllist)):
       col1= float (Dynsqllist[k]['Rowsread'] )/ float( int( Dynsqllist[k]['Numberofexecutions'] ) +1 )
       col2= float (Dynsqllist[k]['Totalexecutiontime(sec.microsec)'] )/ float( int( Dynsqllist[k]['Numberofexecutions'] ) +1 )
       col3= float (Dynsqllist[k]['Numberofexecutions'] )/ float( int( Dynsqllist[k]['Numberofexecutions'] ) +1 )
       SortDynsqllist.append((Dynsqllist[k]['Statementtext'] ,col1,col2,col3))
   
    TopDynsqllist = sorted(SortDynsqllist,cmp=lambda x,y : cmp(x[1],y[1]))
   
    str1=""
    str2=""
    for i in range(-10,-1):
        for k in  range(len(DynSQL)):
            str1="".join(DynSQL[k])
            str2="".join(TopDynsqllist[i][0])
            if ( str1.find(str2)<> -1 ):
                 print str1
                 str1=""
                 str2=""
                

def main():
    options,args = getopt.getopt(sys.argv[1:],"hf:t:",["help","infile=","type="])
    for option,arg in options:
        if  option in ("-h","--help"):
            usage();
            sys.exit(1);
        elif option in ("-f","--infile"):
            infile = options[0][1]
            print infile
        else:
            type   = options[1][1]
            if (type == "topsql") :
                TOPSQL(infile)
            print type

main();

如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续创作!

0

添加新评论0 条评论

Ctrl+Enter 发表

作者其他文章

相关文章

相关问题

相关资料

X社区推广