Fast COUNT

  • This technique shows how get count from MSSQL fast, if you have lot of rows.
  • I is significantly faster than select count(*) …
  • But in some cases result couldn't be exact.
  • The code bellow is code for creating stored procedure, which returns row count of table, which name was passed as a procedure parameter
CREATE PROCEDURE [dbo].[table_Count] 
@tableName nvarchar(255),
@ID BIGINT OUTPUT
AS
BEGIN
 
    SELECT @ID=rowcnt
    FROM sysindexes
    WHERE indid IN (1,0)
        AND OBJECTPROPERTY(id, 'IsUserTable') = 1
        AND OBJECT_NAME(id) = @tableName
 
END
programming/mssql/fastcount.txt · Last modified: 2018-06-21 19:48 (external edit)
CC Attribution-Noncommercial-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0