这样的语句怎们能优化一下,现在发现数据量大的时候很慢

Oo云 2011-03-25 01:21:26

select case when jvolum.jiayoujiaci is null then j.jjiayou else j.jjiayou - jvolum.jiayoujiaci*2 end as '加油架次',
j.jyue as '月份', j.jgongjin as '加油量(吨)' from (select
datepart(mm,BSTDK) as jyue,
count(BSTKD) as jjiayou,
sum(KWMENG)/1000 as jgongjin
from
jiayoudan
where datepart(YYYY,BSTDK) = @year
group by datepart(mm,BSTDK)) AS j
left join (select
datepart(mm,BSTDK) as yue,
count(BSTKD) as jiayoujiaci
from jiayoudan
where VOLUM = 0 and datepart(YYYY,BSTDK) = @year
group by datepart(mm,BSTDK)) AS jvolum on j.jyue = jvolum.yue
order by '月份'
...全文
57 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
--小F-- 2011-03-25
  • 打赏
  • 举报
回复
然后在没有函数的连接部分加索引
--小F-- 2011-03-25
  • 打赏
  • 举报
回复
把你第一个表里面的嵌套部分拿出来写在一个临时表里面

select
datepart(mm,BSTDK) as jyue,
count(BSTKD) as jjiayou,
sum(KWMENG)/1000 as jgongjin
from
jiayoudan
where datepart(YYYY,BSTDK) = @year
group by datepart(mm,BSTDK)

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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