MySQL Operations common SQL statements

Source: Internet
Author: User

################## #分析类 ###############################################################

--table with more than 1000 rows of data in the query table

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;

--Querying tables 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 ';



################## #故障诊断 ###############################################################


--Reverse sort by execution time in MySQL

Mysqladmin processlist--verbose |grep ' Query ' |awk-f ' | ' ' {print $7 $ $9} ' |sort-rn-k1




--Cat killlongquerysession.sh kills session with more than 300 execution time


#!/bin/bash

Executetime= (' Mysql-bse ' show processlist ' | grep ' Query ' |awk ' {print $6 ' "$} ' |sort-rn|head-1 ') #第6列是运行时间, first column Sess Ion 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

Fi

Sleep #睡眠10s

Done



--Cat killwaitsession.sh kills waiting for waiting session in Select


#!/bin/bash

For i in ' mysql-bse ' show full Processlist ' | Grep-i select |grep-i "Waiting | awk ' {print '} '

Do

Mysql-bse "Kill $i"

Done



MySQL Operations common SQL statements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.