积极答复者
count(*)函数难题?

问题
-
程序中需要使用count(*)来检索表,但是速度很慢,可以自己新建函数来获得count(*)同样的结果,
如:
create function dbo.row_count (@table_name sysname)
-- @table_name we want to get count
returns bigint
as
begin
declare @nn bigint -- number of rows
if @table_name is not null
begin
select @nn = sum( p.rows )
from sys.partitions p
left join sys.allocation_units a on p.partition_id = a.container_id
where
p.index_id in(0,1)
and p.rows is not null
and a.type = 1
and p.object_id = object_id(@table_name)
end
return (@nn)
end
go
函数速度蛮快,
但是要实现如:select count(*) from t1 where st<10000这样的功能,有什么办法嘛?