前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >管理MySQL常用的统计语句

管理MySQL常用的统计语句

作者头像
数据库交流
发布2022-06-15 13:59:46
6220
发布2022-06-15 13:59:46
举报
文章被收录于专栏:悦专栏悦专栏

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

1 查看所有业务库

代码语言:javascript
复制
select schema_name from information_schema.schemata where schema_name not in ('information_schema','sys','mysql','performance_schema');

注意:

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

代码语言:javascript
复制
SET GLOBAL information_schema_stats_expiry=0;

2 批量操作某个前缀的表

代码语言:javascript
复制
select concat('select count(*) from martin.',table_name,';') from information_schema.tables where table_schema='martin' and table_name like 'a_%';

效果如下:

代码语言:javascript
复制
+-------------------------------------------------------+
| concat('select count(*) from martin.',table_name,';') |
+-------------------------------------------------------+
| select count(*) from martin.a_01;                     |
| select count(*) from martin.a_02;                     |
+-------------------------------------------------------+

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

代码语言:javascript
复制
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 批量构造修改存储引擎的语句

代码语言:javascript
复制
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'));

效果如下:

代码语言:javascript
复制
+-------------------------------------------------------------------------+
| concat('alter table ',table_schema,'.',table_name,' engine=innodb',';') |
+-------------------------------------------------------------------------+
| alter table martin.b_myisam engine=innodb;                              |
+-------------------------------------------------------------------------+
1 row in set (1.53 sec)

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

代码语言:javascript
复制
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;

效果如下:

代码语言:javascript
复制
+--------------+--------------+------------+
| 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 某个库所有表的字段详情

代码语言:javascript
复制
select table_schema,table_name,column_name,column_type,collation_name from information_schema.columns  where table_schema='martin';

效果如下:

代码语言:javascript
复制
+--------------+--------------+--------------+-------------------+--------------------+
| 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 某个库中所有表详情

代码语言:javascript
复制
select table_schema,table_name,engine,table_collation from information_schema.tables where table_schema='martin';

8 查看某张表的具体信息

代码语言:javascript
复制
select * from information_schema.tables where table_schema='martin' and table_name='student_info'\G

效果如下:

代码语言:javascript
复制
  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: 16384
MAX_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: NULL
TABLE_COLLATION: utf8mb4_0900_ai_ci
       CHECKSUM: NULL
 CREATE_OPTIONS: 
  TABLE_COMMENT: 学生信息表
1 row in set (0.46 sec)

9 查看 MySQL 所有用户

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

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

代码语言:javascript
复制
show grants for 'root'@'localhost';

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

代码语言:javascript
复制
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;

效果如下:

代码语言:javascript
复制
+--------------+--------+-----------+-------------+--------------+
| 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 |
+--------------+--------+-----------+-------------+--------------+
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-05-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 悦专栏 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档