2014年8月18日 星期一

Getting row count and table size in Microsoft Azure SQL database


Azure SQL database in SQL Management Studio does not provide Disk Usage Report, here is a query to view the row count and page size by table.
Query:
SELECT
       t.name,
       SUM(s.row_count) AS [Row Count],
       (
             SELECT   
                   SUM(reserved_page_count) * 8.0 / 1024 AS [Size in MB]
             FROM sys.dm_db_partition_stats, sys.objects
             WHERE sys.dm_db_partition_stats.object_id = sys.objects.object_id
             AND sys.objects.object_id = t.object_id
             GROUP BY sys.objects.name
       ) AS [Size in MB]
FROM sys.tables t
       INNER JOIN sys.dm_db_partition_stats s ON t.object_id = s.object_id
             AND t.type_desc = 'USER_TABLE'
             AND t.name NOT LIKE '%dss%'
             AND s.index_id = 1
GROUP BY t.object_id, t.name
ORDER BY t.name

Output: