INFORMATION_SCHEMA.STATISTICS 统计 表 库 大小

时间:2022-05-29 15:32:50

INFORMATION_SCHEMA

MySQL :: MySQL 5.5 Reference Manual :: 21 INFORMATION_SCHEMA Tables

https://dev.mysql.com/doc/refman/5.5/en/information-schema.html

INFORMATION_SCHEMA Usage Notes

INFORMATION_SCHEMA is a database within each MySQL instance, the place that stores information about all the other databases that the MySQL server maintains. The INFORMATION_SCHEMA database contains several read-only tables. They are actually views, not base tables, so there are no files associated with them, and you cannot set triggers on them. Also, there is no database directory with that name.

Although you can select INFORMATION_SCHEMA as the default database with a USE statement, you can only read the contents of tables, not perform INSERT,UPDATE, or DELETE operations on them.

问题来源, 删除索引前的存在性检测。

http://dev.mysql.com/doc/refman/5.7/en/statistics-table.html

http://m.blog.csdn.net/lilin_esri/article/details/69944346

查询所有的数据库占用磁盘空间大小

SELECT
TABLE_SCHEMA,
CONCAT(
TRUNCATE (
SUM(DATA_LENGTH) / 1024 / 1024,
2
),
' MB'
) AS data_size,
concat(
TRUNCATE (
SUM(DATA_LENGTH) / 1024 / 1024,
2
),
'MB'
) AS index_size
FROM
information_schema. TABLES
GROUP BY
TABLE_SCHEMA
ORDER BY
DATA_LENGTH DESC;

SELECT
*
FROM
information_schema. TABLES;

查询单个数据库各个表占用磁盘空间大小

SELECT
TABLE_NAME,
CONCAT(
TRUNCATE (DATA_LENGTH / 1024 / 1024, 2),
' MB'
) AS data_size,
CONCAT(
TRUNCATE (INDEX_LENGTH / 1024 / 1024, 2),
' MB'
) AS index_size
FROM
information_schema. TABLES
WHERE
TABLE_SCHEMA = 'mydb'
GROUP BY
TABLE_NAME
ORDER BY
DATA_LENGTH DESC;

统计  表 库  大小

[root@d mysql]# bin/mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.15 Source distribution Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sem |
| sem_bak |
| sys |
+--------------------+
6 rows in set (0.00 sec) mysql> desc information_schema.tables;
+-----------------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+---------------------+------+-----+---------+-------+
| TABLE_CATALOG | varchar(512) | NO | | | |
| TABLE_SCHEMA | varchar(64) | NO | | | |
| TABLE_NAME | varchar(64) | NO | | | |
| TABLE_TYPE | varchar(64) | NO | | | |
| ENGINE | varchar(64) | YES | | NULL | |
| VERSION | bigint(21) unsigned | YES | | NULL | |
| ROW_FORMAT | varchar(10) | YES | | NULL | |
| TABLE_ROWS | bigint(21) unsigned | YES | | NULL | |
| AVG_ROW_LENGTH | bigint(21) unsigned | YES | | NULL | |
| DATA_LENGTH | bigint(21) unsigned | YES | | NULL | |
| MAX_DATA_LENGTH | bigint(21) unsigned | YES | | NULL | |
| INDEX_LENGTH | bigint(21) unsigned | YES | | NULL | |
| DATA_FREE | bigint(21) unsigned | YES | | NULL | |
| AUTO_INCREMENT | bigint(21) unsigned | YES | | NULL | |
| CREATE_TIME | datetime | YES | | NULL | |
| UPDATE_TIME | datetime | YES | | NULL | |
| CHECK_TIME | datetime | YES | | NULL | |
| TABLE_COLLATION | varchar(32) | YES | | NULL | |
| CHECKSUM | bigint(21) unsigned | YES | | NULL | |
| CREATE_OPTIONS | varchar(255) | YES | | NULL | |
| TABLE_COMMENT | varchar(2048) | NO | | | |
+-----------------+---------------------+------+-----+---------+-------+
21 rows in set (0.00 sec) mysql> select * from information_schema.tables limit 5;
+---------------+--------------------+---------------------------------------+-------------+--------+---------+------------+------------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------------+
| TABLE_CATALOG | TABLE_SCHEMA | TABLE_NAME | TABLE_TYPE | ENGINE | VERSION | ROW_FORMAT | TABLE_ROWS | AVG_ROW_LENGTH | DATA_LENGTH | MAX_DATA_LENGTH | INDEX_LENGTH | DATA_FREE | AUTO_INCREMENT | CREATE_TIME | UPDATE_TIME | CHECK_TIME | TABLE_COLLATION | CHECKSUM | CREATE_OPTIONS | TABLE_COMMENT |
+---------------+--------------------+---------------------------------------+-------------+--------+---------+------------+------------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------------+
| def | information_schema | CHARACTER_SETS | SYSTEM VIEW | MEMORY | 10 | Fixed | NULL | 384 | 0 | 16434816 | 0 | 0 | NULL | 2018-11-29 14:37:45 | NULL | NULL | utf8_general_ci | NULL | max_rows=43690 | |
| def | information_schema | COLLATIONS | SYSTEM VIEW | MEMORY | 10 | Fixed | NULL | 231 | 0 | 16704765 | 0 | 0 | NULL | 2018-11-29 14:37:45 | NULL | NULL | utf8_general_ci | NULL | max_rows=72628 | |
| def | information_schema | COLLATION_CHARACTER_SET_APPLICABILITY | SYSTEM VIEW | MEMORY | 10 | Fixed | NULL | 195 | 0 | 16357770 | 0 | 0 | NULL | 2018-11-29 14:37:45 | NULL | NULL | utf8_general_ci | NULL | max_rows=86037 | |
| def | information_schema | COLUMNS | SYSTEM VIEW | InnoDB | 10 | Dynamic | NULL | 0 | 16384 | 0 | 0 | 8388608 | NULL | NULL | NULL | NULL | utf8_general_ci | NULL | max_rows=2789 | |
| def | information_schema | COLUMN_PRIVILEGES | SYSTEM VIEW | MEMORY | 10 | Fixed | NULL | 2565 | 0 | 16757145 | 0 | 0 | NULL | 2018-11-29 14:37:45 | NULL | NULL | utf8_general_ci | NULL | max_rows=6540 | |
+---------------+--------------------+---------------------------------------+-------------+--------+---------+------------+------------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------------+
5 rows in set (0.03 sec) mysql> select distinct(TABLE_CATALOG) from information_schema.tables ;
+---------------+
| TABLE_CATALOG |
+---------------+
| def |
+---------------+
1 row in set (0.00 sec) mysql> select distinct(TABLE_SCHEMA) from information_schema.tables ;
+--------------------+
| TABLE_SCHEMA |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sem |
| sem_bak |
| sys |
+--------------------+
6 rows in set (0.01 sec) mysql> select table_name, (data_length/1024/1024) as data_mb , (index_length/1024/1024) as index_mb, ((data_length+index_length)/1024/1024) as all_mb, table_rows from tables where TABLE_SCHEMA = 'sem';
ERROR 1046 (3D000): No database selected
mysql> select table_name, (data_length/1024/1024) as data_mb , (index_length/1024/1024) as index_mb, ((data_length+index_length)/1024/1024) as all_mb, table_rows from information_schema.tables where TABLE_SCHEMA = 'sem';
+-------------+----------------+--------------+----------------+------------+
| table_name | data_mb | index_mb | all_mb | table_rows |
+-------------+----------------+--------------+----------------+------------+
| article | 455.90625000 | 0.00000000 | 455.90625000 | 5827967 |
| cinfo | 0.01562500 | 0.01562500 | 0.03125000 | 0 |
| cinfo1 | 496.90625000 | 145.70312500 | 642.60937500 | 9256895 |
| cinfo2 | 471.89062500 | 125.70312500 | 597.59375000 | 8754276 |
| cinfo3 | 109.60937500 | 31.57812500 | 141.18750000 | 2268454 |
| cinfoview | NULL | NULL | NULL | NULL |
| cinfoview1 | NULL | NULL | NULL | NULL |
| cinfoview2 | NULL | NULL | NULL | NULL |
| cinfoview3 | NULL | NULL | NULL | NULL |
| content | 0.01562500 | 0.00000000 | 0.01562500 | 0 |
| content1 | 24400.00000000 | 139.70312500 | 24539.70312500 | 7781986 |
| content2 | 26372.00000000 | 125.70312500 | 26497.70312500 | 7713312 |
| content3 | 6749.00000000 | 31.57812500 | 6780.57812500 | 1855395 |
| info | 443.87500000 | 0.00000000 | 443.87500000 | 8864137 |
| info1 | 486.89062500 | 0.00000000 | 486.89062500 | 9513996 |
| info2 | 320.79687500 | 0.00000000 | 320.79687500 | 7274232 |
| info_c | 0.01562500 | 0.00000000 | 0.01562500 | 0 |
| info_c1 | 0.01562500 | 0.00000000 | 0.01562500 | 0 |
| info_c2 | 0.01562500 | 0.00000000 | 0.01562500 | 0 |
| infoview | NULL | NULL | NULL | NULL |
| infoview1 | NULL | NULL | NULL | NULL |
| infoview2 | NULL | NULL | NULL | NULL |
| semtags | 0.10937500 | 0.01562500 | 0.12500000 | 50 |
| tags | 553.98437500 | 0.00000000 | 553.98437500 | 9242997 |
| tags1 | 504.95312500 | 0.00000000 | 504.95312500 | 8448984 |
| tags2 | 526.96875000 | 0.00000000 | 526.96875000 | 8797155 |
| tags3 | 131.64062500 | 31.57812500 | 163.21875000 | 2243969 |
| tags_201703 | 15.51562500 | 0.10937500 | 15.62500000 | 5374 |
| tags_201704 | 0.01562500 | 0.01562500 | 0.03125000 | 0 |
| tags_201705 | 0.01562500 | 0.01562500 | 0.03125000 | 0 |
| tags_201706 | 0.01562500 | 0.01562500 | 0.03125000 | 0 |
| tags_201707 | 0.01562500 | 0.01562500 | 0.03125000 | 0 |
+-------------+----------------+--------------+----------------+------------+
32 rows in set (0.00 sec) mysql> select table_name, (data_length/1024/1024) as data_mb , (index_length/1024/1024) as index_mb, ((data_length+index_length)/1024/1024) as all_mb, table_rows from information_schema.tables where TABLE_SCHEMA = 'sem_bak';
+------------+------------+------------+------------+------------+
| table_name | data_mb | index_mb | all_mb | table_rows |
+------------+------------+------------+------------+------------+
| info1 | 0.01562500 | 0.00000000 | 0.01562500 | 2 |
| semtags | 0.01562500 | 0.01562500 | 0.03125000 | 4 |
+------------+------------+------------+------------+------------+
2 rows in set (0.00 sec) mysql> select table_schema, sum(data_length+index_length)/1024/1024 as total_mb, sum(data_length)/1024/1024 as data_mb, sum(index_length)/1024/1024 as index_mb, count(*) as tables, curdate() as today from information_schema.tables group by table_schema;
+--------------------+----------------+----------------+--------------+--------+------------+
| table_schema | total_mb | data_mb | index_mb | tables | today |
+--------------------+----------------+----------------+--------------+--------+------------+
| information_schema | 0.15625000 | 0.15625000 | 0.00000000 | 61 | 2018-11-29 |
| mysql | 2.42948151 | 2.21561432 | 0.21386719 | 31 | 2018-11-29 |
| performance_schema | 0.00000000 | 0.00000000 | 0.00000000 | 87 | 2018-11-29 |
| sem | 62671.93750000 | 62040.18750000 | 631.75000000 | 32 | 2018-11-29 |
| sem_bak | 0.04687500 | 0.03125000 | 0.01562500 | 2 | 2018-11-29 |
| sys | 0.01562500 | 0.01562500 | 0.00000000 | 101 | 2018-11-29 |
+--------------------+----------------+----------------+--------------+--------+------------+
6 rows in set (0.04 sec) mysql>