mysql如何快速查询


本文摘自PHP中文网,作者coldplay.xixi,侵删。

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

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

mysql快速查询的方法:

1.查询正在运行中的事务

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.查看当前连接,并且能够知晓连接数

1

select SUBSTRING_INDEX(host,‘:‘,1) as ip , count(*) from information_schema.processlist group by ip;

3.查看一个表的大小

1

select concat(round(sum(DATA_LENGTH/1024/1024),2),‘M‘) from information_schema.tables where table_schema=‘数据库名‘ AND table_name=‘表名‘;

4.查看某个数据库所有表的大小

1

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.查看库的大小,剩余空间的大小

1

2

3

4

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.查找关于锁

1

2

3

4

5

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.查看各个库下的表数据大小

1

2

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.查看各个数据库的数据大小

1

select TABLE_SCHEMA, concat(round(sum(data_length)/1024/1024,2),‘ MB‘) as data_size  from information_schema.tables group by table_schema;

3.查看实例有没有主键

1

2

3

4

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.查看实例中哪些字段可以为null

1

select TABLE_SCHEMA,TABLE_NAME from COLUMNS where IS_NULLABLE=‘YES‘ and TABLE_SCHEMA not in (‘information_schema‘,‘performance_schema‘,‘mysql‘, ‘sys‘)\G

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

1

2

3

4

5

6

7

8

#存储过程

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.查看实例中哪些表字段字符集和默认字符集不一致

阅读剩余部分

相关阅读 >>

mysql创建表的sql语句

最全mysql数据类型梳理汇总

mysql怎么获取不重复的数据?

如何启动和关闭mysql服务?

如何使用navicat进行mysql命令行操作?

mysql精粹系列(精粹)

mysql如何来创建表

详解 mysql int 类型的长度值问题

mysql如何创建用户?

如何使用mysql表连接

更多相关阅读请进入《mysql》频道 >>


数据库系统概念 第6版
书籍

数据库系统概念 第6版

机械工业出版社

本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...