MySQL 常用SQL查询语句

目录

连接字符集查看

查看进程的流量变化

长事物:

锁等待查看: 

DDL/DML锁

全局读锁:

内存使用:

分区表相关

各实例的大小

各表的大小

长时间未更新的表

无主键、唯一建以及二级索引

无主键、唯一健、仅有二级索引

仅有主键、唯一健表

无主键,无唯一健

存储引擎相关


连接字符集查看

select t1.VARIABLE_NAME ,
t1.VARIABLE_VALUE ,
t2.PROCESSLIST_ID,
concat(t2.PROCESSLIST_USER,"@",t2.PROCESSLIST_HOST) ,
t2.PROCESSLIST_DB,
t2.PROCESSLIST_COMMAND 
from performance_schema.variables_by_thread  t1 , performance_schema.threads t2 
where t1.THREAD_ID=t2.THREAD_ID and t1.VARIABLE_NAME like 'character%'  ;

查看进程的流量变化

select t1.VARIABLE_NAME ,
t1.VARIABLE_VALUE ,
t2.PROCESSLIST_ID,
concat(t2.PROCESSLIST_USER,"@",t2.PROCESSLIST_HOST) as 'User@host' ,
t2.PROCESSLIST_DB,
t2.PROCESSLIST_COMMAND
from performance_schema.status_by_thread t1 , performance_schema.threads t2
where t1.THREAD_ID=t2.THREAD_ID and t1.VARIABLE_NAME like 'Byte%'   and processlist_id=1139 ;

长事物:

超过5s 未提交的事物 
information_schema.innodb_trx
information_schema.PROCESSLIST

select trx_mysql_thread_id as processlist_id , 
NOW() ,
trx_started ,
to_seconds(now()) - to_seconds(trx_started) as trx_last_time, 
user, 
host,
db,
trx_query 
from information_schema.innodb_trx  trx join information_schema.processlist pc1 on trx.trx_mysql_thread_id=pc1.id where  trx_mysql_thread_id != connection_id()  and to_seconds(now()) - to_seconds(trx_started) >=5 ;

锁等待查看: 

select 
locked_table,
locked_index,
locked_type,
blocking_pid,
concat(t2.user,'@',t2.host) as "blocking(user@ip:port)",
blocking_lock_mode,
waiting_trx_rows_modified,
waiting_pid ,
concat(t3.user,'@',t3.host) as "blocking(user@ip:port)",
waiting_lock_mode,
waiting_trx_rows_modified,
wait_age_secs,
waiting_query
from sys.x$innodb_lock_waits t1 
left join information_schema.processlist t2 on  t1.blocking_pid=t2.id 
left join information_schema.processlist t3 on  t3.id=t1.waiting_pid ;

影响锁等待的超时的参数
innodb_lock_wait_timeout 默认50s

DDL/DML锁

select trx_mysql_thread_id as processlist_id , 
NOW() ,
trx_started ,
to_seconds(now()) - to_seconds(trx_started) as trx_last_time, 
user, 
host,
db,
trx_query 
from information_schema.innodb_trx  trx join information_schema.processlist pc1 on trx.trx_mysql_thread_id=pc1.id where  trx_mysql_thread_id != connection_id()  and 
to_seconds(now()) - to_seconds(trx_started) >= (select max(Time) from information_schema.processlist where state ='Wait for table metadata lock' and info like 'alter%table%' or info like 'truncate%table%'  ) ;

场景2: kill掉 下发DDL语句用户连接,取消ddl语句下发,保证业务不被阻塞 
select *  from information_schema.processlist where info like 'alter%table%'  ; 

全局读锁:

场景1: 杀掉添加ftwal的会话,恢复业务运行  FTWRL = “flush table with read lock”
select  processlist_id ,
md1.object_type,
object_schema,
object_name,
lock_type,
lock_duration,
lock_status
from performance_schema.metadata_locks md1
inner join performance_schema.threads thd on md1.OWNER_THREAD_ID=thd.thread_id  
and processlist_id <> connection_id()
and lock_duration='EXPLICIT' ;


内存使用:

 默认只对 performance_schema库进行内存统计,对于全局内存统计需要手动开启 
//动态开启 
update  performance_schema.setup_instruments set enabled='yes'  where name like 'memory%' 
//修改参数文件,从其生效 
performance-schema-instrument='memory/%=COUNTED' 

select event_name, 
current_alloc  
from sys.memory_global_by_current_bytes  where event_name like 'memory%innodb%' ;


分区表相关

 select table_schema ,
table_name ,
count(partition_name) as partition_count , 
sum(table_rows) as table_total_rows,
concat( round(sum(data_length) / (1024*1024),2),'M' ) data_length ,
concat( round(sum(index_length) / (1024*1024),2),'M' ) index_length ,
concat( round ( round ( sum(data_length+index_length)) / (1024 * 1024),2 ),'M' ) total_size
from information_schema.partitions 
where  table_name not in (
'sys',
'mysql',
'INFORMATION_SCHEMA',
'performance_schema')
and partition_name is not null group  by table_schema,table_name  
order by  sum(data_length+index_length) desc ;

各实例的大小

select  table_schema, 
round( sum(data_length+index_length)/1024/1024,2 ) as  total_size_mb ,
round( sum(data_length)/1024/1024,2  ) as data_size_mb ,
round( sum(index_length)/1024/1024,2 ) as index_size_mb ,
count(*)  as tables 
from information_schema.tables 
where table_schema not in ( 
'sys',
'mysql',
'INFORMATION_SCHEMA',
'performance_schema') 
group by table_schema order by 2 desc ;

各表的大小

select table_schema,
table_name,
round(data_length/1024/1024,2)  as data_length_mb ,
round(index_length/1024/1024,2) as  index_length_mb, 
round( (data_length+index_length)/1024/1024,2 ) as  total_size_mb ,
engine 
from  information_schema.tables 
where table_schema not in ( 
'sys',
'mysql',
'INFORMATION_SCHEMA',
'performance_schema') 
and table_schema='wlcj'   ----指定schema
order by  (data_length+index_length) desc limit 10 ;

长时间未更新的表

information_schema.tables   update_time未null表示重启之后没有发生更改
select table_schema,
table_name, 
update_time  
from information_schema.tables 
where table_schema not in ( 
'sys',
'mysql',
'INFORMATION_SCHEMA',
'performance_schema') 
and table_type='BASE TABLE' 
order by  update_time;

无主键、唯一建以及二级索引

select 
t1.table_schema,
t1.table_name
from information_schema.columns t1 join information_schema.tables t2 on 
t1.table_schema=t2.table_schema and t1.table_name = t2.table_name 
where t1.table_schema not in ( 
'sys',
'mysql',
'INFORMATION_SCHEMA',
'performance_schema') 
and t2.table_type='BASE TABLE'
and t1.table_schema='wlcj' 
group by  t1.table_schema,t1.table_name having max(column_key)='' ;

无主键、唯一健、仅有二级索引

select 
t1.table_schema,
t1.table_name
from information_schema.columns t1 join information_schema.tables t2 on 
t1.table_schema=t2.table_schema and t1.table_name = t2.table_name 
where t1.table_schema not in ( 
'sys',
'mysql',
'INFORMATION_SCHEMA',
'performance_schema') 
and t2.table_type='BASE TABLE'
and t1.table_schema='wlcj' 
and t1.column_key !=''
group by  t1.table_schema,t1.table_name having  group_concat(column_key) not regexp 'PRI|UNI';

仅有主键、唯一健表

select 
t1.table_schema,
t1.table_name
from information_schema.columns t1 join information_schema.tables t2 on 
t1.table_schema=t2.table_schema and t1.table_name = t2.table_name 
where t1.table_schema not in ( 
'sys',
'mysql',
'INFORMATION_SCHEMA',
'performance_schema') 
and t2.table_type='BASE TABLE'
and t1.table_schema='wlcj' 
and t1.column_key !=''
group by  t1.table_schema,t1.table_name having  group_concat(column_key) not regexp 'MUL';

无主键,无唯一健

select 
t1.table_schema,
t1.table_name
from information_schema.columns t1 join information_schema.tables t2 on 
t1.table_schema=t2.table_schema and t1.table_name = t2.table_name 
where t1.table_schema not in ( 
'sys',
'mysql',
'INFORMATION_SCHEMA',
'performance_schema') 
and t2.table_type='BASE TABLE'
and t1.table_schema='wlcj' 
group by  t1.table_schema,t1.table_name having  group_concat(column_key) not regexp 'PRI|UMI';

存储引擎相关

select table_schema, 
engine,
count(*) 
from information_schema.tables 
where table_schema not in ( 
'sys',
'mysql',
'INFORMATION_SCHEMA',
'performance_schema')  
and table_type='BASE TABLE' 
group by table_schema, engine ;


 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值