m$sql check tables size

# 一個情況下遇到了m$sql express的log.mdf爆了, 先查一下各資料表容量

USE DATABASE_NAME
GO
SELECT t.NAME AS TableName,
i.name as indexName,
sum(p.rows) as RowCounts,
sum(a.total_pages) as TotalPages,
sum(a.used_pages) as UsedPages,
sum(a.data_pages) as DataPages,
(sum(a.total_pages) * 8) / 1024 as TotalSpaceMB,
(sum(a.used_pages) * 8) / 1024 as UsedSpaceMB,
(sum(a.data_pages) * 8) / 1024 as DataSpaceMB
FROM sys.tables t
INNER JOIN sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN sys.allocation_units a ON p.partition_id = a.container_id
WHERE t.NAME NOT LIKE 'dt%' 
AND i.OBJECT_ID > 255 
AND i.index_id <= 1
GROUP BY t.NAME, i.object_id, i.index_id, i.name
ORDER BY object_name(i.object_id)

# 縮小體積

USE DATABASE_NAME;
DBCC SHRINKFILE (DATABASE_NAME, 7);
GO
DBCC SHRINKFILE (DATABASE_NAME_log, 7);
GO

# 看了一下結果無解, 只好清空它

TRUNCATE TABLE TABLE_NAME;

# 若是不想清空清除部份, 為了不讓ID跑掉(auto increment)

DBCC CHECKIDENT(table_name, RESEED, new_reseed_value);

# 另外這是查看目錄的id值指令

DBCC CHECKIDENT(table_name);

# 另外, mysql版查各資料庫大小

SELECT table_schema AS "Database",
ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS "Size (MB)"
FROM information_schema.TABLES
GROUP BY table_schema;

# mysql版查各資料表大小

SELECT
table_schema AS "Database",
table_name AS Table,
round(((data_length + index_length) / 1024 / 1024), 2) Size in MB
FROM
information_schema.TABLES
ORDER BY
table_schema
DESC;

發佈留言