如何估计SQL Server索引大小
-
05-07-2019 - |
题
虽然估算直行和表格大小是相当简单的数学,但我们发现猜测每个索引将占用多少空间(对于给定的表格大小)是一项挑战。我们可以学习哪些方面来计算指数的更好估计和增长率?
其他提示
如果可能,我通常会从原始表中获取1000条记录, 将它们插入我自己的表中, 并且使用下面的脚本我可以使用一个样本。
好吧,这不准确,但可以给我一个起点。
--Find out the disk size of an index:
--USE [DB NAME HERE]
go
SELECT
OBJECT_NAME(I.OBJECT_ID) AS TableName,
I.name AS IndexName,
8 * SUM(AU.used_pages) AS 'Index size (KB)',
CAST(8 * SUM(AU.used_pages) / 1024.0 AS DECIMAL(18,2)) AS 'Index size (MB)'
FROM
sys.indexes I
JOIN sys.partitions P ON P.OBJECT_ID = I.OBJECT_ID AND P.index_id = I.index_id
JOIN sys.allocation_units AU ON AU.container_id = P.partition_id
--WHERE
-- OBJECT_NAME(I.OBJECT_ID) = '<TableName>'
GROUP BY
I.OBJECT_ID,
I.name
ORDER BY
TableName
--========================================================================================
--http://msdn.microsoft.com/en-us/library/fooec9de780-68fd-4551-b70b-2d3ab3709b3e.aspx
--I believe that keeping the GROUP BY
--is the best option in this case
--because of sys.allocation_units
--can have 4 types of data inside
--as below:
--type tinyint
--Type of allocation unit.
--0 = Dropped
--1 = In-row data (all data types, except LOB data types)
--2 = Large object (LOB) data (text, ntext, image, xml, large value types, and CLR user-defined types)
--3 = Row-overflow data
--marcelo miorelli 8-NOV-2013
--========================================================================================
不隶属于 StackOverflow