2.簡單工廠模式
阿新 • • 發佈:2022-12-03
直接複製執行
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。
完成!
直接複製執行
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。
完成!