Mad*_*ddy 8 mysql database database-design mysqldump
嗨,我是数据库新手.我正在研究庞大的数据库并试图清理混乱.我想首先找到占据整个数据库中最高内存的前十个表.因为桌子太多,我无法找到每张桌子的记忆.我需要占据最大空间的前10或20个桌子.任何帮助将非常感激.谢谢.
也许是这样的:
SELECT CONCAT(table_schema, '.', table_name),
CONCAT(ROUND(table_rows / 1000000, 2), 'M') rows,
CONCAT(ROUND(data_length / ( 1024 * 1024 * 1024 ), 2), 'G') DATA,
CONCAT(ROUND(index_length / ( 1024 * 1024 * 1024 ), 2), 'G') idx,
CONCAT(ROUND(( data_length + index_length ) / ( 1024 * 1024 * 1024 ), 2), 'G') total_size,
ROUND(index_length / data_length, 2) idxfrac
FROM information_schema.TABLES
ORDER BY data_length + index_length DESC
LIMIT 10;
Run Code Online (Sandbox Code Playgroud)
参考这里
MyISAM 只为其索引占用内存
要查找在最坏情况下可以使用最多内存的前 10 个 MyISAM 表,请尝试以下操作:
SELECT * FROM
(
SELECT table_schema,table_name,index_length
FROM information_schema.tables
WHERE engine='MyISAM' AND
table_schema NOT IN ('information_schema','mysql','performance_schema')
ORDER BY index_length DESC
) LIMIT 10;
Run Code Online (Sandbox Code Playgroud)
InnoDB 为其数据和索引占用内存
要查找在最坏情况下可以使用最多内存的前 10 个 InnoDB 表,请尝试以下操作:
SELECT * FROM
(
SELECT table_schema,table_name,data_length+index_length tblsize
FROM information_schema.tables
WHERE engine='InnoDB'
ORDER BY index_length DESC
) LIMIT 10;
Run Code Online (Sandbox Code Playgroud)
这是按大小降序排列的前 50 个表格的另一个显示
SELECT * FROM
(SELECT TN TableName,LPAD(REPLACE(FORMAT(TS/POWER(1024,1),2),',',''),Z,' ') KB,
LPAD(REPLACE(FORMAT(TS/POWER(1024,2),2),',',''),Z,' ') MB,
LPAD(REPLACE(FORMAT(TS/POWER(1024,3),2),',',''),Z,' ') GB
FROM (SELECT CONCAT(table_schema,'.',table_name) TN,
(data_length+index_length) TS FROM information_schema.tables
WHERE table_schema NOT IN ('information_schema','mysql','performance_schema')
AND engine IS NOT NULL) A,(SELECT 13 Z) B ORDER BY TS DESC) MMM LIMIT 50;
Run Code Online (Sandbox Code Playgroud)
如果您有兴趣,我有一些查询可以为您提供有关 MySQL 实例的整个故事
此查询显示存储引擎占用的磁盘空间量(以 GB 为单位)
SELECT IFNULL(B.engine,'Total') "Storage Engine",
CONCAT(LPAD(REPLACE(FORMAT(B.DSize/POWER(1024,pw),3),',',''),17,' '),
' ',SUBSTR(' KMGTP',pw+1,1),'B') "Data Size",
CONCAT(LPAD(REPLACE(FORMAT(B.ISize/POWER(1024,pw),3),',',''),17,' '),
' ',SUBSTR(' KMGTP',pw+1,1),'B') "Index Size", CONCAT(LPAD(REPLACE(
FORMAT(B.TSize/POWER(1024,pw),3),',',''),17,' '),' ',
SUBSTR(' KMGTP',pw+1,1),'B') "Table Size"
FROM (SELECT engine,SUM(data_length) DSize,SUM(index_length) ISize,
SUM(data_length+index_length) TSize FROM information_schema.tables
WHERE table_schema NOT IN ('mysql','information_schema','performance_schema')
AND engine IS NOT NULL GROUP BY engine WITH ROLLUP) B,(SELECT 3 pw) A ORDER BY TSize;
Run Code Online (Sandbox Code Playgroud)
此查询显示数据库占用的磁盘空间量(以 GB 为单位)
SELECT DBName,CONCAT(LPAD(FORMAT(SDSize/POWER(1024,pw),3),17,' '),
' ',SUBSTR(' KMGTP',pw+1,1),'B') "Data Size",CONCAT(LPAD(FORMAT(SXSize/
POWER(1024,pw),3),17,' '),' ',SUBSTR(' KMGTP',pw+1,1),'B') "Index Size",
CONCAT(LPAD(FORMAT(STSize/POWER(1024,pw),3),17,' '),' ',
SUBSTR(' KMGTP',pw+1,1),'B') "Total Size" FROM
(SELECT IFNULL(DB,'All Databases') DBName,SUM(DSize) SDSize,
SUM(XSize) SXSize,SUM(TSize) STSize FROM (SELECT table_schema DB,
data_length DSize,index_length XSize,data_length+index_length TSize
FROM information_schema.tables WHERE table_schema NOT IN
('mysql','information_schema','performance_schema')) AAA
GROUP BY DB WITH ROLLUP) AA,(SELECT 3 pw) BB ORDER BY (SDSize+SXSize);
Run Code Online (Sandbox Code Playgroud)
此查询显示存储引擎数据库占用的磁盘空间量(以 GB 为单位)
SELECT IF(ISNULL(B.table_schema)+ISNULL(B.engine)=2,"Storage for All Databases",
IF(ISNULL(B.table_schema)+ISNULL(B.engine)=1,CONCAT("Storage for ",B.table_schema),
CONCAT(B.engine," Tables for ",B.table_schema))) Statistic,CONCAT(LPAD(REPLACE(
FORMAT(B.DSize/POWER(1024,pw),3),',',''),17,' '),' ',SUBSTR(' KMGTP',pw+1,1),'B')
"Data Size",CONCAT(LPAD(REPLACE(FORMAT(B.ISize/POWER(1024,pw),3),',',''),17,' '),
' ',SUBSTR(' KMGTP',pw+1,1),'B') "Index Size",CONCAT(LPAD(REPLACE(FORMAT(B.TSize/
POWER(1024,pw),3),',',''),17,' '),' ',SUBSTR(' KMGTP',pw+1,1),'B') "Table Size"
FROM (SELECT table_schema,engine,SUM(data_length) DSize,SUM(index_length) ISize,
SUM(data_length+index_length) TSize FROM information_schema.tables WHERE
table_schema NOT IN ('mysql','information_schema','performance_schema')
AND engine IS NOT NULL GROUP BY table_schema,engine WITH ROLLUP) B,
(SELECT 3 pw) A ORDER BY TSize;
Run Code Online (Sandbox Code Playgroud)
我之前发布的三个查询有一个共同的特点:子查询(SELECT 3 pw)
(SELECT 0 pw)
,报告以字节为单位(SELECT 1 pw)
,报告以千字节为单位(SELECT 2 pw)
,报告以兆字节为单位(SELECT 3 pw)
,报告以千兆字节为单位(SELECT 4 pw)
,报告以 TeraBytes 为单位(SELECT 5 pw)
,报告的单位是 PetaBytes(如果您需要这个,请发布结果!!!)