[Operation and Maintenance] Query the data and index occupancy size of each table in the database

[SQL] Query the data and index occupancy size of each table in the database

SELECT
	a.*,
	CONCAT( a.总大小 / 1024000000, 'G' ) `总大小G` 
FROM
	(
	SELECT
		TABLE_SCHEMA,
		TABLE_NAME,
        sum( DATA_LENGTH ) 数据大小,
        sum( INDEX_LENGTH ) 索引大小,
		( sum( DATA_LENGTH )+ sum( INDEX_LENGTH ) ) 总大小
	FROM
		information_schema.TABLES 
	-- WHERE	TABLE_SCHEMA = '库名' 
	GROUP BY
		table_name 
	ORDER BY
	sum( DATA_LENGTH )+ sum( INDEX_LENGTH ) DESC 
	) a

Query all data sizes

# 查询所有的数据大小
SELECT
	concat( round( sum( DATA_LENGTH / 1024 / 1024 ), 2 ), 'M' ) 
FROM
	information_schema.TABLES;

Query the data size of a table

# 查询某个表的数据大小
SELECT
	concat( round( sum( DATA_LENGTH / 1024 / 1024 ), 2 ), 'M' ) 占用
FROM
information_schema.TABLES 
WHERE
	table_schema = '数据库名称' 
	AND table_name = '表名称';

View all database capacity sizes

#查看所有数据库容量大小
select
table_schema as '数据库',
sum(table_rows) as '记录数',
sum(truncate(data_length/1024/1024, 2)) as '数据容量(MB)',
sum(truncate(index_length/1024/1024, 2)) as '索引容量(MB)'
from information_schema.tables
group by table_schema
order by sum(data_length) desc, sum(index_length) desc;

View the capacity of each table in all databases

#查看所有数据库各表容量大小
select
table_schema as '数据库',
table_name as '表名',
table_rows as '记录数',
truncate(data_length/1024/1024, 2) as '数据容量(MB)',
truncate(index_length/1024/1024, 2) as '索引容量(MB)'
from information_schema.tables
order by data_length desc, index_length desc;

View the capacity of the specified database
Example: View the capacity of the mysql database: the code is as follows:

# 查看指定数据库容量大小
# 例:查看mysql库容量大小:代码如下:

select
table_schema as '数据库',
sum(table_rows) as '记录数',
sum(truncate(data_length/1024/1024, 2)) as '数据容量(MB)',
sum(truncate(index_length/1024/1024, 2)) as '索引容量(MB)'
from information_schema.tables
where table_schema='mysql';

View the capacity of each table in the specified database *
Example: view the capacity of each table in the mysql database

# 查看指定数据库各表容量大小*
# 例:查看mysql库各表容量大小

select
table_schema as '数据库',
table_name as '表名',
table_rows as '记录数',
truncate(data_length/1024/1024, 2) as '数据容量(MB)',
truncate(index_length/1024/1024, 2) as '索引容量(MB)'
from information_schema.tables
where table_schema='mysql'
order by data_length desc, index_length desc;

There is an information_schema database in mysql, which contains the metadata of mysql, including database information, information about tables in the database, and so on. So if you want to query the disk space occupied by the database, you can operate the information_schema database.

The tables in information_schema mainly include:

  schemata table: This table mainly contains information about all databases stored in mysql

  tables table: This table stores information about all tables in the database, including how many columns each table has.

  columns table: This table stores the table field information in all tables.

  Statistics table: Stores the information of the indexes in the table.

  user_privileges table: Stores user privilege information.

  schema_privileges table: Stores database permissions.

  table_privileges table: stores the permissions of the table.

  column_privileges table: Stores the permission information of the column.

  character_sets table: Stores information about the character sets that mysql can use.

  Collations table: Provides comparison information for each character set.

  collation_character_set_applicability table: It is equivalent to a comparison between the first two fields of the collations table and the character_sets table, and records the comparison information between character sets.

  table_constraints table: This table is mainly used to record the description of the table and the constraint type.

  key_column_usage table: records columns with constraints.

  routines table: records stored procedure and function information, does not contain custom procedure or function information.

  views table: records view information, and requires the show view permission.

  Triggers table: Stores trigger information and requires super permission.

Guess you like

Origin blog.csdn.net/G971005287W/article/details/131402426