DECLARE @pagesizeKB int
SELECT @pagesizeKB = low / 1024 FROM master.dbo.spt_values
WHERE number = 1 AND type = 'E';

SELECT 
a2.name AS TableName,
a2.object_id as ObjectID,
a1.ROWS AS Rows,
(a1.reserved + ISNULL(a4.reserved,0))* @pagesizeKB AS ReservedKB,
a1.DATA * @pagesizeKB AS DataKB,
(CASE WHEN (a1.used + ISNULL(a4.used,0)) > a1.DATA THEN (a1.used + ISNULL(a4.used,0)) - a1.DATA ELSE 0 END) * @pagesizeKB AS IndexKB,
(CASE WHEN (a1.reserved + ISNULL(a4.reserved,0)) > a1.used THEN (a1.reserved + ISNULL(a4.reserved,0)) - a1.used ELSE 0 END) * @pagesizeKB AS UnusedKB,
SI.name as IndexName,
--SI.Index_ID as IndexID,
SI.type_desc AS IndexType,
sc.RecordLength as RecordLength,
dm_ius.user_seeks + dm_ius.user_scans + dm_ius.user_lookups AS IndexUses,
dm_ius.user_updates AS IndexUpdates 
FROM
    (SELECT ps.object_id, ps.index_id,
            CASE WHEN (ps.index_id < 2) THEN ROW_COUNT ELSE 0 END AS [ROWS],
            ps.reserved_page_count AS reserved,
            CASE WHEN (ps.index_id < 2) THEN (ps.in_row_data_page_count + ps.lob_used_page_count + ps.row_overflow_used_page_count)
                    ELSE (ps.lob_used_page_count + ps.row_overflow_used_page_count)
                    END AS DATA,
            ps.used_page_count AS used
    FROM sys.dm_db_partition_stats ps
    ) AS a1
        LEFT OUTER JOIN
            (SELECT it.parent_id,
                    SUM(ps.reserved_page_count) AS reserved,
                    SUM(ps.used_page_count) AS used
                    FROM sys.dm_db_partition_stats ps
                        INNER JOIN sys.internal_tables it ON (it.object_id = ps.object_id)
                    WHERE it.internal_type IN (202,204)
                    GROUP BY it.parent_id) AS a4 ON (a4.parent_id = a1.object_id)
        INNER JOIN sys.indexes SI ON (SI.object_id = a1.object_id AND SI.index_id = a1.index_id) ---
        INNER JOIN sys.all_objects a2  ON ( a1.object_id = a2.object_id ) 
        left join (SELECT sic.object_id, sic.index_id, SUM(sc.max_length)AS RecordLength
			FROM sys.index_columns as sic with (nolock)
			 LEFT JOIN sys.columns as sc with (nolock) ON sic.object_id = sc.object_id AND sic.column_id = sc.column_id
			group by sic.object_id, sic.index_id) sc 
		on sc.index_id = SI.index_id and sc.object_id = SI.object_id
	LEFT JOIN sys.dm_db_index_usage_stats dm_ius 
	ON SI.object_id = dm_ius.OBJECT_ID AND SI.index_id = dm_ius.index_id AND dm_ius.database_id = DB_ID()
WHERE a2.TYPE <> N'S' AND a2.TYPE <> N'IT'
