以下两种写法哪种更有效率?

ruhong 2014-05-07 10:51:07

select sum(次数) as 次数,月份 from
(select cast(datepart(month,考核时间) as nvarchar(2))+'月' as 月份,次数 from 绩效管理_考核列表
where 被考核人=50 and datepart(year,考核时间)=datepart(year,getdate())) as abc group by 月份



select cast(datepart(month,考核时间) as nvarchar(2))+'月' as 月份,次数 into #abc from 绩效管理_考核列表
where 被考核人=50 and datepart(year,考核时间)=datepart(year,getdate())

select sum(次数) as 次数,月份 from #abc b group by 月份

...全文
234 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
spiritofdragon 2014-05-08
  • 打赏
  • 举报
回复
引用 3 楼 shinger126 的回复:
第二种方法会带来额外的开销,写进临时表再汇总,如果汇总时不需要再关联其他的表是完全没必要的。第一种写法改进一下 select cast(month(考核时间) as nvarchar(2))+'月' as 月份,sum(次数) as 次数 from 绩效管理_考核列表 where 被考核人=50 and year(考核时间)=year(getdate()) group by cast(month(考核时间) as nvarchar(2))+'月'
group by int 肯定比 group by string 快。因为他只查1年的 所以完全可以只 group by month(考核时间) 。 其他不变,语法没问题。
shinger126 2014-05-08
  • 打赏
  • 举报
回复
第二种方法会带来额外的开销,写进临时表再汇总,如果汇总时不需要再关联其他的表是完全没必要的。第一种写法改进一下 select cast(month(考核时间) as nvarchar(2))+'月' as 月份,sum(次数) as 次数 from 绩效管理_考核列表 where 被考核人=50 and year(考核时间)=year(getdate()) group by cast(month(考核时间) as nvarchar(2))+'月'
ruhong 2014-05-08
  • 打赏
  • 举报
回复
我看不太懂, 有一个百分比啊。 哪里可以看到具体的时间呢?
shinger126 2014-05-08
  • 打赏
  • 举报
回复
引用 4 楼 spiritofdragon 的回复:
[quote=引用 3 楼 shinger126 的回复:] 第二种方法会带来额外的开销,写进临时表再汇总,如果汇总时不需要再关联其他的表是完全没必要的。第一种写法改进一下 select cast(month(考核时间) as nvarchar(2))+'月' as 月份,sum(次数) as 次数 from 绩效管理_考核列表 where 被考核人=50 and year(考核时间)=year(getdate()) group by cast(month(考核时间) as nvarchar(2))+'月'
group by int 肯定比 group by string 快。因为他只查1年的 所以完全可以只 group by month(考核时间) 。 其他不变,语法没问题。[/quote] 执行效率不大的,group by后面的字段是什么类型对执行计划影响不大,微乎其微
ruhong 2014-05-07
  • 打赏
  • 举报
回复




请教我如何看效率。

Neo_whl 2014-05-07
  • 打赏
  • 举报
回复
选中代码,ctrl+L显示执行计划,贴出计划内容

34,593

社区成员

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

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