如何快速查询mysql_mysql如何快速查询

mysql快速查询的方法:1、查询正在运行中的事务;2、查看当前连接,并且能够知晓连接数;3、查看一个表的大小;4、查看某个数据库所有表的大小。

d537e1ecba3add6403e4ad9143018af6.png更多相关免费学习推荐:mysql教程(视频)

mysql快速查询的方法:

1.查询正在运行中的事务select p.id,p.user,p.host,p.db,p.command,p.time,i.trx_state,i.trx_started,p.info from information_schema.processlist p,information_schema.innodb_trx i where p.id=i.trx_mysql_thread_id;

2.查看当前连接,并且能够知晓连接数select SUBSTRING_INDEX(host,‘:‘,1) as ip , count(*) from information_schema.processlist group by ip;

3.查看一个表的大小select concat(round(sum(DATA_LENGTH/1024/1024),2),‘M‘) from information_schema.tables where table_schema=‘数据库名‘ AND table_name=‘表名‘;

4.查看某个数据库所有表的大小select table_name,concat(round(sum(DATA_LENGTH/1024/1024),2),‘M‘) from information_schema.tables where table_schema=‘t1‘ group by table_name;

5.查看库的大小,剩余空间的大小select table_schema,round((sum(data_length / 1024 / 1024) + sum(index_length / 1024 / 1024)),2) dbsize,round(sum(DATA_FREE / 1024 / 1024),2) freesize,

round((sum(data_length / 1024 / 1024) + sum(index_length / 1024 / 1024)+sum(DATA_FREE / 1024 / 1024)),2) spsize

from information_schema.tables

where table_schema not in (‘mysql‘,‘information_schema‘,‘performance_schema‘)

group by table_schema order by freesize desc;

6.查找关于锁select r.trx_id waiting_trx_id,r.trx_mysql_thread_id waiting_thread,r.trx_query waiting_query,b.trx_id blocking_trx_id,b.trx_mysql_thread_id blocking_thread,b.trx_query blocking_query

from information_schema.innodb_lock_waits w

inner join information_schema.innodb_trx b

on b.trx_id = w.blocking_trx_id

inner join information_schema.innodb_trx r on r.trx_id = w.requesting_trx_id\G

information_schema的使用

1.查看各个库下的表数据大小select table_name,concat(round(sum(DATA_LENGTH/1024/1024),2),‘M‘)

from information_schema.tables where table_schema=‘db_name‘ group by table_name;

2.查看各个数据库的数据大小select TABLE_SCHEMA, concat(round(sum(data_length)/1024/1024,2),‘ MB‘) as data_size from information_schema.tables group by table_schema;

3.查看实例有没有主键select table_schema,table_name from information_schema.tables

where (table_schema,table_name)

not in(select distinct table_schema,table_name from information_schema.STATISTICS where INDEX_NAME=‘PRIMARY‘ )

and table_schema not in ( ‘sys‘,‘mysql‘,‘information_schema‘,‘performance_schema‘);

4.查看实例中哪些字段可以为nullselect TABLE_SCHEMA,TABLE_NAME from COLUMNS where IS_NULLABLE=‘YES‘ and TABLE_SCHEMA not in (‘information_schema‘,‘performance_schema‘,‘mysql‘, ‘sys‘)\G

5.查看实例中有哪些存储过程和函数#存储过程

select ROUTINE_SCHEMA,ROUTINE_NAME,ROUTINE_TYPE

from information_schema.ROUTINES

where ROUTINE_TYPE=‘PROCEDURE‘ and ROUTINE_SCHEMA not in (‘mysql‘,‘sys‘,‘information_schema‘,‘performance_schema‘);

#函数

select ROUTINE_SCHEMA,ROUTINE_NAME,ROUTINE_TYPE

from information_schema.ROUTINES

where ROUTINE_TYPE=‘FUNCTION‘ and ROUTINE_SCHEMA not in (‘mysql‘,‘sys‘,‘information_schema‘,‘performance_schema‘);

6.查看实例中哪些表字段字符集和默认字符集不一致select TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,CHARACTER_SET_NAME

from information_schema.COLUMNS

where (CHARACTER_SET_NAME is null or CHARACTER_SET_NAME <> ‘utf8‘)

and TABLE_SCHEMA not in (‘information_schema‘,‘performance_schema‘,‘test‘,‘mysql‘,‘sys‘);

7.查看实例中哪些表字段字符校验规则和默认的不一致

查看当前字符集和校对规则设置show variables like ‘collation_%‘;

select TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,CHARACTER_SET_NAME,COLLATION_NAME

from information_schema.COLUMNS

where (COLLATION_NAME is null or COLLATION_NAME <> ‘utf8_general_ci‘)

and TABLE_SCHEMA not in (‘information_schema‘,‘performance_schema‘,‘test‘,‘mysql‘,‘sys‘);

8.查看哪些账号有除了select、update、insert以外的权限select GRANTEE,PRIVILEGE_TYPE,concat(TABLE_SCHEMA,‘-‘,TABLE_NAME,‘-‘,COLUMN_NAME) from COLUMN_PRIVILEGES where PRIVILEGE_TYPE not in (‘select‘,‘insert‘,‘update‘)

union

select GRANTEE,PRIVILEGE_TYPE,TABLE_SCHEMA from SCHEMA_PRIVILEGES where PRIVILEGE_TYPE not in (‘select‘,‘insert‘,‘update‘)

union

select GRANTEE,PRIVILEGE_TYPE,concat(TABLE_SCHEMA,‘-‘,TABLE_NAME) from TABLE_PRIVILEGES where PRIVILEGE_TYPE not in (‘select‘,‘insert‘,‘update‘)

union

select GRANTEE,PRIVILEGE_TYPE,concat(‘user‘) from USER_PRIVILEGES where PRIVILEGE_TYPE not in (‘select‘,‘insert‘,‘update‘);

9.查看实例中哪些表不是默认存储引擎,以默认存储引擎为innodb为例select TABLE_NAME,ENGINE

from information_schema.tables

where ENGINE!=‘innodb‘ and TABLE_SCHEMA not in (‘information_schema‘,‘performance_schema‘,‘test‘,‘mysql‘, ‘sys‘);

10.查看实例中哪些表有外键select a.TABLE_SCHEMA,a.TABLE_NAME,a.CONSTRAINT_TYPE,a.CONSTRAINT_NAME,b.REFERENCED_TABLE_NAME,b.REFERENCED_COLUMN_NAME

from information_schema.TABLE_CONSTRAINTS a LEFT JOIN information_schema.KEY_COLUMN_USAGE b

ON a.CONSTRAINT_NAME=b.CONSTRAINT_NAME where a.CONSTRAINT_TYPE=‘FOREIGN KEY‘;

11.查看实例中哪些表字段有级联更新select TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,REFERENCED_TABLE_SCHEMA,REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME

from information_schema.KEY_COLUMN_USAGE

where REFERENCED_TABLE_SCHEMA is not null

and REFERENCED_TABLE_NAME is not null

and REFERENCED_COLUMN_NAME is not null and table_schema not in (‘information_schema‘,‘performance_schema‘,‘test‘,‘mysql‘, ‘sys‘);

12.如何根据用户名、连接时间、执行的sql等过滤当前实例中的连接信息select USER,HOST,DB from processlist where TIME>2;

13.查看数据库中没有索引的表select TABLE_SCHEMA,TABLE_NAME from information_schema.tables

where TABLE_NAME not in (select distinct(any_value(TABLE_NAME)) from information_schema.STATISTICS group by INDEX_NAME)

and TABLE_SCHEMA not in (‘sys‘,‘mysql‘,‘information_schema‘,‘performance_schema‘);

14.查看数据库中有索引的表,建立了哪些索引

显示结果:库名、表名、索引名select TABLE_SCHEMA,TABLE_NAME,group_concat(INDEX_NAME)

from information_schema.STATISTICS where TABLE_SCHEMA not in (‘sys‘,‘mysql‘,‘information_schema‘,‘performance_schema‘) group by TABLE_NAME ;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值