一个论坛,要计算贴子数量,用select count(id)
当贴子数量比较多时,是不是会很耗时?有没有其它比交好的方法?

解决方案 »

  1.   

    count还是比较快的。
    自然数据量大了,速度会变慢。
    合理地建立索引是好的做法。
      

  2.   

    应该比较快的
    要不然我很多地方判断记录是否存在都是用count是不是等于0来判断的,不是很惨~
      

  3.   

    select count(*) from table并不会查询数据库,并不像你想象的将数据库数一遍。print datepart(ms,getdate())
    select top 1 a.id from sysobjects a cross join sysobjects b cross join sysobjects c
    print datepart(ms,getdate())
    select count(*) from sysobjects a cross join sysobjects b cross join sysobjects c
    print datepart(ms,getdate())
    select count(*) from sysobjects a cross join sysobjects b cross join sysobjects c
      where a.id>-b.id and b.id>-c.id
    print datepart(ms,getdate())
    go这里统计了将近两百亿记录,显示的是毫秒数。其实最重要的是做查询优化。
      

  4.   

    sorry,第一句话写错了。我的意思说count并不很占时间,占时间的是你经常需要动态修改的查询条件。
      

  5.   

    cache a object to count to the result's count,
    when you add a topic ,then the object you cache plus .
    in this way you can get the topic count very quickly.
      

  6.   

    怀么要判断一个记录是否存在,是用select count()好,还是用select top 1好?
      

  7.   

    判断一条记录是否存在跟这个没关系吧.应该是select * from table where (条件吧)