How to get row count of every table in a SQL Azure Database

2 comments

Getting rowcount of a table using the count() function is not a good idea as it would take too long for a large table.  Worse, if you want to get the rowcount of every table in a database. Thankfully there is a dynamic view sys.dm_db_partition_stats that has the rowcounts readily available.


SELECT so.name as TableName, ddps.row_count as [RowCount]
FROM sys.objects so
JOIN sys.indexes si ON si.OBJECT_ID = so.OBJECT_ID
JOIN sys.dm_db_partition_stats AS ddps
       ON si.OBJECT_ID = ddps.OBJECT_ID  AND si.index_id = ddps.index_id
WHERE si.index_id < 2  AND so.is_ms_shipped = 0
ORDER BY ddps.row_count DESC

Hope this is usefull!

How to check Edition and Max Size of SQL Azure Database trough T-SQL query

1 comments

Its very often requierd to check what's the edition (Web, Business) and maximum size (1GB, 5GB, 10GB, 20GB, 30GB, 40GB, 50GB) of a SQL Azure database.  There are two ways to do this.

1. Check the database properties in the Azure portal https://windows.azure.com/
2. Run a T-SQL query against your database

I find the latter approach quite handy. Here's the query:

SELECT DATABASEPROPERTYEX('Database_Name', 'EDITION')

SELECT DATABASEPROPERTYEX('Database_Name', 'MaxSizeInBytes')
OR
SELECT CONVERT(BIGINT,DATABASEPROPERTYEX ( 'Database_Name' , 'MAXSIZEINBYTES'))/1024/1024/1024 AS 'MAXSIZE IN GB'

Remember that it needs to be executed on the database you want to check, not on the master database. Then you might ask why the database_name is required? I don't know! :)

Here's the full list of properties supported in this function: http://msdn.microsoft.com/en-us/library/ee621786.aspx