I am in the process of re-writing a stored procedure that obtains the row count and max Id
of all tables in a database. The database has close to 500 tables and over a billion entries, so the old procedure is way too slow to continue to use.
Here is the old procedure:
DECLARE @TableRowCounts TABLE ([TableName] VARCHAR(128), [RowCount] BIGINT, [MaxId] BIGINT) ;
INSERT INTO @TableRowCounts ([TableName], [RowCount], [MaxId])
EXEC sp_MSforeachtable 'SELECT ''?'' [TableName], COUNT(*) [RowCount], MAX(Id) [MaxId] FROM ?' ;
SELECT [TableName], [RowCount], [MaxId]
FROM @TableRowCounts
ORDER BY [TableName]
This would give a result something like the following:
TableName | RowCount | MaxId
-------------------------------
TableA | 12345678 | 12345688
TableB | 90123456 | 90123466
TableC | 78901234 | 78901244
I can't quite say how long it takes to run because I have yet to actually observe it complete at the current database's size.
Here is a work-in-progress new query:
SELECT
o.NAME,
i.rowcnt
FROM sysindexes AS i
INNER JOIN sysobjects AS o ON i.id = o.id
--INNER JOIN sys.tables AS t ON t.[object_id] = o.id ???
--INNER JOIN sys.schemas AS s on t.[schema_id] = s.[schema_id] ???
--INNER JOIN sys.columns AS c on t.[object_id] = c.[object_id] ???
WHERE i.indid < 2 AND OBJECTPROPERTY(o.id, 'IsMSShipped') = 0
ORDER BY o.NAME
My idea was to use sys.schemas
and sys.columns
so that I can use MAX(Id)
inside of my SELECT
, but I am currently stuck on how to fully incorporate this functionality. If there is another, better way to do this, I am open to suggestions.
I do need both the row count and MAX(Id)
. My dataset should not contain any missing Ids, and this will help show that one is missing at a glance. The data is being cached from an external source, and no Ids should be missing, so if the row count is not equal to MAX(Id)
, the client consuming the database can see this and take the necessary actions to fill in the missing rows. The client also uses the row count and MAX(Id)
for other tasks, such as comparing the external source's current Id to the database's max id. If the external source's current Id is greater than the database's MAX(Id)
, there is work to be done.