效率比较高sql语句。

IFEXISTS(SELECTnameFROMdbo.sysobjectsWHEREid=Object_id(N'[dbo].[row_count]'))
  DROPFUNCTION[dbo].[row_count]
  GO
  CREATEFUNCTIONdbo.row_count(@table_namesysname)
  --@table_namewewanttogetcount
  RETURNSbigint
  AS
  BEGIN
  DECLARE@nnbigint--numberofrows
  IF@table_nameISNOTNULL
  BEGIN
  SELECT@nn=sum(p.rows)
  FROMsys.partitionsp
  LEFTJOINsys.allocation_unitsaONp.partition_id=a.container_id
  WHERE
  p.index_idin(0,1)
  andp.rowsisnotnull
  anda.type=1
  andp.object_id=object_id(@table_name)
  END
  RETURN(@nn)
  END

  GO

 比count(*)效率高很多。

 

posted @ 2010-09-26 13:33  依然乐观  阅读(432)  评论(0编辑  收藏  举报