直接复制执行
1、查询所有数据库占用磁盘空间大小
select
TABLE_SCHEMA,
concat(truncate(sum(data_length)/1024/1024,2),' MB') as data_size,
concat(truncate(sum(index_length)/1024/1024,2),'MB') as index_size
from information_schema.tables
group by TABLE_SCHEMA
ORDER BY data_size desc;
select concat(truncate(sum(data_length)/1024/1024,2),'MB') as data_size,
concat(truncate(sum(max_data_length)/1024/1024,2),'MB') as max_data_size,
concat(truncate(sum(data_free)/1024/1024,2),'MB') as data_free,
concat(truncate(sum(index_length)/1024/1024,2),'MB') as index_size
from information_schema.tables
where TABLE_SCHEMA = 'databasename'; # databasename 具体库名
2、查询单个库中所有表磁盘占用大小
select concat(truncate(sum(data_length)/1024/1024,2),'MB') as data_size,
concat(truncate(sum(max_data_length)/1024/1024,2),'MB') as max_data_size,
concat(truncate(sum(data_free)/1024/1024,2),'MB') as data_free,
concat(truncate(sum(index_length)/1024/1024,2),'MB') as index_size
from information_schema.tables
where TABLE_NAME = 'tablename'; # tablename 具体表名
-- 查看数据库中所有表的信息
SELECT
concat( table_schema, '.', table_name ) AS 'Table Name',
table_rows AS 'Number of Rows',
concat( round( data_length / ( 1024 * 1024 * 1024 ), 6 ), ' G' ) AS 'Data Size',
concat( round( index_length / ( 1024 * 1024 * 1024 ), 6 ), ' G' ) AS 'Index Size',
concat( round( ( data_length + index_length ) / ( 1024 * 1024 * 1024 ), 6 ), ' G' ) AS 'Total'
FROM
information_schema.TABLES W
WHERE
table_schema LIKE 'database name';
3、查看表空间
# 单位GB
SELECT concat( round( sum( data_length ) / ( 1024 * 1024 * 1024 ), 6 ), 'GB' ) AS 'Total Data Size'
FROM information_schema.TABLES
WHERE table_schema LIKE 'database name';
4、查看索引空间
SELECT CONCAT( ROUND( SUM( index_length ) / ( 1024 * 1024 * 1024 ), 6 ), ' GB' ) AS 'Total Index Size'
FROM information_schema.TABLES
WHERE table_schema LIKE 'dabatase name';
5、使用optimize命令
optimize table tb_report_inventory;
使用的时间比较长,需要耐心等待。
注意:optimize执行时会将表锁住,所以不要在高峰期使用。也不要经常使用,每月一次就足够了
问题
若执行 2、查询单个库中所有表磁盘占用大小出现以下报错:
Expression #2 of SELECT list is not in GROUP BY clause and contains
nonaggregated column ‘sss.month_id’ which is not functionally
dependent on columns in GROUP BY clause; this is incompatible with
sql_mode=only_full_group_by
解决方法一:
此方法只是临时解决,重启mysql服务器会还原。
打开navcat,
用sql查询:
select @@global.sql_mode
查询出来的值为:
ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
去掉ONLY_FULL_GROUP_BY,重新设置值。
set @@global.sql_mode
=’STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION’;
解决方法二:
成功的步骤:
打开文件
sudo vim /etc/mysql/conf.d/mysql.cnf
滚动到文件底部复制并粘贴
[mysqld]
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
到文件的底部
保存并退出输入模式
sudo service mysql restart
重启MySQL。
完成!
标签:1024,NO,数据库,length,MySQL,磁盘,data,sum,concat
From: https://www.cnblogs.com/xiaoxi-jinchen/p/16967024.html