这一节内容,整理一些管理 MySQL 会经常用到的统计语句,比如表的碎片率、非 InnoDB 的表、所有用户和所有业务库等。

1 查看所有业务库

select schema_name from information_schema.schemata where schema_name not in ('information_schema','sys','mysql','performance_schema');

注意:

information_schema 中的数据默认不是实时的数据,如果需要实时的数据,需要执行下面命令:

SET GLOBAL information_schema_stats_expiry=0;

2 批量操作某个前缀的表

select concat('select count(*) from martin.',table_name,';') from information_schema.tables where table_schema='martin' and table_name like 'a_%';

效果如下:

+-------------------------------------------------------+| concat('select count(*) from martin.',table_name,';') |+-------------------------------------------------------+| select count(*) from martin.a_01;                     || select count(*) from martin.a_02;                     |+-------------------------------------------------------+

3 查找业务库中的非 InnoDB 的表

select table_schema,table_name,engine from information_schema.tables  where table_schema not in('information_schema','sys','mysql','performance_schema') and  engine<>'InnoDB';

4 批量构造修改存储引擎的语句

select distinct concat('alter table ',table_schema,'.',table_name,' engine=innodb',';') from information_schema.tables where (engine <> 'innodb' and table_schema not in ('information_schema','sys','mysql','performance_schema'));

效果如下:

+-------------------------------------------------------------------------+| concat('alter table ',table_schema,'.',table_name,' engine=innodb',';') |+-------------------------------------------------------------------------+| alter table martin.b_myisam engine=innodb;                              |+-------------------------------------------------------------------------+1 row in set (1.53 sec)

5 查看每张表数据量,并按数据量排序

select table_schema,table_name, table_rows from information_schema.tables where table_schema not in ('information_schema','sys','mysql','performance_schema') order by table_rows desc;

效果如下:

+--------------+--------------+------------+| TABLE_SCHEMA | TABLE_NAME   | TABLE_ROWS |+--------------+--------------+------------+| martin       | student_info |          8 || martin       | a_02         |          2 || martin       | a_01         |          0 || martin       | b_myisam     |          0 |+--------------+--------------+------------+4 rows in set (0.50 sec)

注意:该命令统计的数据量为估值。

6 某个库所有表的字段详情

select table_schema,table_name,column_name,column_type,collation_name from information_schema.columns  where table_schema='martin';

效果如下:

+--------------+--------------+--------------+-------------------+--------------------+| TABLE_SCHEMA | TABLE_NAME | COLUMN_NAME | COLUMN_TYPE | COLLATION_NAME |+--------------+--------------+--------------+-------------------+--------------------+| martin | a_01 | id | int | NULL || martin | a_01 | stu_name | varchar(10) | utf8mb4_0900_ai_ci || martin | a_01 | stu_class | varchar(10) | utf8mb4_0900_ai_ci || martin | a_01 | stu_num | int | NULL |......

7 某个库中所有表详情

select table_schema,table_name,engine,table_collation from information_schema.tables where table_schema='martin';

8 查看某张表的具体信息

select * from information_schema.tables where table_schema='martin' and table_name='student_info'\G

效果如下:

  TABLE_CATALOG: def   TABLE_SCHEMA: martin     TABLE_NAME: student_info     TABLE_TYPE: BASE TABLE         ENGINE: InnoDB        VERSION: 10     ROW_FORMAT: Dynamic     TABLE_ROWS: 8 AVG_ROW_LENGTH: 2048    DATA_LENGTH: 16384MAX_DATA_LENGTH: 0   INDEX_LENGTH: 49152      DATA_FREE: 0 AUTO_INCREMENT: 13    CREATE_TIME: 2022-05-05 20:38:21    UPDATE_TIME: 2022-05-25 01:51:18     CHECK_TIME: NULLTABLE_COLLATION: utf8mb4_0900_ai_ci       CHECKSUM: NULL CREATE_OPTIONS:   TABLE_COMMENT: 学生信息表1 row in set (0.46 sec)

9 查看 MySQL 所有用户

select distinct concat("'",user,'''@''',host,"';") as user from mysql.user;
效果如下:
+---------------------------------+| user                            |+---------------------------------+| 'mysql.infoschema'@'localhost'; || 'mysql.session'@'localhost';    || 'mysql.sys'@'localhost';        || 'root'@'localhost';             |+---------------------------------+4 rows in set (0.03 sec)

这种结果就很方便执行 show grants,比如下面的:

show grants for 'root'@'localhost';

10 查看某个库所有表的碎片率

select table_name,data_free / (data_free + data_length + index_length) as aaa,data_free,data_length,index_length from information_schema.tables where table_schema = 'martin' order by aaa desc;

效果如下:

+--------------+--------+-----------+-------------+--------------+| TABLE_NAME   | aaa    | DATA_FREE | DATA_LENGTH | INDEX_LENGTH |+--------------+--------+-----------+-------------+--------------+| a_01         | 0.0000 |         0 |       16384 |        49152 || a_02         | 0.0000 |         0 |       16384 |        49152 || b_myisam     | 0.0000 |         0 |           0 |         1024 || student_info | 0.0000 |         0 |       16384 |        49152 |+--------------+--------+-----------+-------------+--------------+

MySQL常用的统计语句相关推荐

  1. MYSQL常用基本SQL语句总结。

    转载自品略图书馆 http://www.pinlue.com/article/2019/11/0502/329770616659.html SQL分类: DDL-----数据定义语言(CREATE-- ...

  2. Mysql常用的sql语句大全

    ​ 零.用户管理: 1.新建用户: >CREATE USER name IDENTIFIED BY 'ssapdrow'; 2.更改密码: >SET PASSWORD FOR name=P ...

  3. mysql常用的yu语句_mysql常用sql语句

    1.清空用户下的所有表里面的数据 select concat('truncate table ',table_name,';') from information_schema.TABLES wher ...

  4. 【mysql】mysql 常用建表语句

    [1]建立员工档案表 要求字段:员工员工编号,员工姓名,性别,工资,email,入职时间,部门. [2]合理选择数据类型及字段修饰符,要求有NOT NULL,auto_increment, prima ...

  5. 数据库 MySQL常用命令 SQL语句1

    文章目录 查看当前使用的是哪个数据库 查看MySQL的版本号 强制结束MySQL输入 如何退出MySQL 查看创建表的语句 简单的select 字段重命名的方法 查询所有字段 条件查询 where b ...

  6. 工作中mysql常用的sql语句

    安装服务端: sudo apt-get install mysql-server 安装客户端: sudo apt-get install mysql-client 配置文件:/etc/mysql 命令 ...

  7. mysql常用的yu语句_常用mysql语句备份

    1.场景:由于需求变动,需要将一张表里面的两个字段合并并存到其中的一个字段中: 可以这样: update table set column1=CONCAT_WS('|',ifnull(column1, ...

  8. MYSQL 常用DML DDL语句

    修改表名 ALTER TABLE 表 RENAME TO 新表名 新增字段 ALTER TABLE 表 ADD 字段 字段类型 删除字段 ALTER TABLE 表 DROP 字段 改变字段名称和属性 ...

  9. [转]mysql常用日期查询语句

    转载自: 网络 查询一天: select * from table where to_days(column_time) = to_days(now()); select * from table w ...

最新文章

  1. linux如何查看内存最大的几个文件,详解Linux如何查看当前占用CPU或内存最多的几个进程...
  2. ts包、表、子表、section的关系
  3. python的工作方向-Python最有发展潜力的方向已出来,是否有你喜欢的职位?
  4. 计算机网络:第一章 概述
  5. java 子类强转父类 父类强转子类
  6. Java 源码中 unchecked 什么意思
  7. Anaconda安装第三方包(whl文件)
  8. matlab机器人自动分拣_极智嘉分拣系统落地 助力打造智慧物流引擎
  9. B站韩顺平版Linux学习笔记(很全啊!)
  10. 照片放大不清晰怎么处理?用嗨格式图片无损放大器
  11. 如何取消WPS 2019启动就显示稻壳商城?关闭后如何打开?
  12. 电脑版微信每天自动发送
  13. 实战:RBAC(基于角色的权限控制)-2021.11.28
  14. 积分商城游戏化运营?积分游戏应该如何正确设置
  15. 利用LabVIEW开发应变量测试
  16. JavaScript---BOM基础
  17. 如何搭建一个自己的FTP服务器
  18. scratch学习1 积木区+程序区
  19. 时序模型:长短期记忆网络(LSTM)
  20. 弘辽科技:如何快速提升抖音小店

热门文章

  1. manjaro安装python_manjaro安装以及配置
  2. 【OpenCV】(五)图像直方图操作
  3. 23种设计模式——中介者模式
  4. 2 Arduino基本数字输出功能LED灯
  5. linux localhost识别,linux之localhost127.0.0.1及本机地址的差别
  6. android 本地存储路径,Android本地存储路径学习
  7. React中setState方法详解
  8. 制作基于RTL8367实现8+2POE千兆交换机
  9. 日常使用------利用VbA对Excel中的多个sheet工作表排序
  10. 如何安排活动日程?Fantastical轻松帮你管理不同时区的活动!