sql 小问题,速度给分!

Small__Wolf 2009-03-11 10:19:50
declare @datetemp table(classid int)--表变量
insert into @datetemp select distinct(enterprisecolumnclass.classid)
from enterprisecolumnclass,enterprisecolumn
where status=0 and enterpriseid=137 and enterprisecolumn.id=enterprisecolumnclass.enterprisecolumnid

declare @infocount int
set @infocount=0
select @infocount=@infocount+count(ProductTemp.id) from ProductTemp,ProductTemp_class,@datetemp t
where ProductTemp.name<>'' and ProductTemp.name is not null and ProductTemp_class.classid=t.classid and ProductTemp.id=ProductTemp_class.ProductTempid
and datediff(d,getdate(),ProductTemp.createtime)=0

select @infocount

这条sql用@infocount=@infocount+count(ProductTemp.id) 这种方式查询需要2秒左右
直接用infocount=count(ProductTemp.id) 效率就高多了,但是这样满足不了我的逻辑,有什么方式可以把@infocount值累加起来,效率还高?

...全文
89 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
等不到来世 2009-03-11
  • 打赏
  • 举报
回复
再加个变量中转一下。
declare @infocount int,@cnt int
select @infocount=0,@cnt=0
select @cnt=count(id) from table1
set @infocount=@infocount+@cnt
select @cnt=count(id) from table2
set @infocount=@infocount+@cnt
select @infocount
Small__Wolf 2009-03-11
  • 打赏
  • 举报
回复
declare @infocount int
set @infocount=0
select @infocount=@infocount+count(id) from table1
select @infocount=@infocount+count(id) from table2
select @infocount=@infocount+count(id) from table3
select @infocount=@infocount+count(id) from table4
select @infocount=@infocount+count(id) from table5
select @infocount


其实就是统计多个表中的一个数,把这个数加到一个 @infocount中,表可能非常多!

ChinaJiaBing 2009-03-11
  • 打赏
  • 举报
回复

declare @datetemp table(classid int)--表变量
insert into @datetemp select distinct(enterprisecolumnclass.classid)
from enterprisecolumnclass,enterprisecolumn
where status=0 and enterpriseid=137 and enterprisecolumn.id=enterprisecolumnclass.enterprisecolumnid

declare @infocount int
set @infocount=0
select @infocount=count(ProductTemp.id) from ProductTemp,ProductTemp_class,@datetemp t
where ProductTemp.name <>'' and ProductTemp.name is not null and ProductTemp_class.classid=t.classid and ProductTemp.id=ProductTemp_class.ProductTempid
and datediff(d,getdate(),ProductTemp.createtime)=0
select @infocount

sdhdy 2009-03-11
  • 打赏
  • 举报
回复
count(ProductTemp.id) 不就是表里id 的个数吗?为什么还要@infocount=@infocount+count(ProductTemp.id)???
arrow_gx 2009-03-11
  • 打赏
  • 举报
回复
select @infocount =sum(count(ProductTemp.id)) from ProductTemp,ProductTemp_class,@datetemp t 
where ProductTemp.name <>'' and ProductTemp.name is not null and ProductTemp_class.classid=t.classid and ProductTemp.id=ProductTemp_class.ProductTempid
and datediff(d,getdate(),ProductTemp.createtime)=0

select @infocount
dawugui 2009-03-11
  • 打赏
  • 举报
回复
请给出表结构,测试数据,具体算法和结果,谢谢。
sdhdy 2009-03-11
  • 打赏
  • 举报
回复
--不太明白你说的。??
sum(count(ProductTemp.id))

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧