SQL statements commonly used in MySQL operation and maintenance

###################Analysis class############################## ##################################

-- Query tables with more than 1000 rows of data

select concat(table_schema,'.',table_name) as table_name,table_rows   

 from information_schema.tables where table_rows > 1000  

   order by table_rows desc;   

   


-- View partition data

SELECT PARTITION_NAME,TABLE_ROWS FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_NAME = 'xxxx';


   

-- Query the 5 largest tables   

 SELECT concat(table_schema,'.',table_name) table_name,   

     concat(round(data_length/(1024*1024),2),'M') data_length   

     FROM information_schema.TABLES   

     ORDER BY data_length DESC LIMIT 5;   

 

 

 

-- Query a table without a primary key

SELECT CONCAT(t.table_schema,".",t.table_name) as table_name   

FROM information_schema.TABLES t   

LEFT JOIN information_schema.TABLE_CONSTRAINTS tc   

ON t.table_schema = tc.table_schema   

AND t.table_name = tc.table_name   

AND tc.constraint_type = 'PRIMARY KEY'  

WHERE tc.constraint_name IS NULL   

AND t.table_type = 'BASE TABLE';  



###################Troubleshooting############################# ##################################


-- Sort in reverse order by execution time in MySQL

mysqladmin processlist --verbose |grep 'Query'|awk -F "|" '{print $7 $2 $9}'|sort -rn -k1




-- cat killLongQuerySession.sh kills sessions whose execution time exceeds 300


#!/bin/bash

executetime=(`mysql -Bse 'show processlist'| grep 'Query'|awk '{print $6 " " $1}'|sort -rn|head -1`) #The sixth column is the running time, the first column is session id

time=${executetime[0]}

id=${executetime[1]}

while :

do

    maxtime=300

    if [ $time  -gt  $maxtime  ] ; then

        echo $time $id >> /tmp/killqueryid.log

        mysql -Bse "kill  $id"

    #else

    #   echo $time $id

    be

    sleep 10 #sleep 10s

done



-- cat killWaitSession.sh kills the session waiting for Waiting in the select


#!/bin/bash

for i in `mysql -Bse 'show full processlist' | grep -i select |grep -i "Waiting | awk  '{print $1}'`

do

        mysql -Bse "kill  $i"

done



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326176494&siteId=291194637