SqlServer快速獲得表總記錄數(大資料量)
阿新 • • 發佈:2019-01-11
--第1種 執行全表掃描才能獲得行數
ALTER PROCEDURE [dbo].[sp_RowCount]
@table NVARCHAR(100)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @tb TABLE(name SYSNAME,[RowCount] NVARCHAR(4000),c NVARCHAR(4000),d NVARCHAR(4000),e NVARCHAR(4000),f NVARCHAR(4000))
INSERT INTO @tb EXEC sp_spaceused @table
SELECT TOP 1 [RowCount] FROM @tb
END
SELECT count(*) FROM BUS_tb_UserGradePrice
--第2種 執行掃描全表id不為空的,獲得行數select count(userid) from BUS_tb_UserGradePrice where userid is not NULL
--第3種 直接從系統表中查詢表的總記錄數(特別適合大資料)SELECT rows FROM sysindexes WHERE id = OBJECT_ID('dbo.BUS_tb_UserGradePrice') AND indid < 2
**其中“dbo.BUS_tb_UserGradePrice ”為需要查詢的表名
ALTER PROCEDURE [dbo].[sp_RowCount]
@table NVARCHAR(100)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @tb TABLE(name SYSNAME,[RowCount] NVARCHAR(4000),c NVARCHAR(4000),d NVARCHAR(4000),e NVARCHAR(4000),f NVARCHAR(4000))
INSERT INTO @tb EXEC sp_spaceused @table
SELECT TOP 1 [RowCount] FROM @tb
END