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这里统计了将近两百亿记录,显示的是毫秒数。其实最重要的是做查询优化。
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.
怀么要判断一个记录是否存在,是用select count()好,还是用select top 1好?
判断一条记录是否存在跟这个没关系吧.应该是select * from table where (条件吧)
自然数据量大了,速度会变慢。
合理地建立索引是好的做法。
要不然我很多地方判断记录是否存在都是用count是不是等于0来判断的,不是很惨~
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这里统计了将近两百亿记录,显示的是毫秒数。其实最重要的是做查询优化。
when you add a topic ,then the object you cache plus .
in this way you can get the topic count very quickly.