【sql server】日期作为筛选条件的一个小优化

稻庄 2018-08-23 09:36:01
select * from view
-----1s-----where date1 >= '2018-08-01'
-----20s----where date1 >= (select firstdate from table where id=1)
如上,直接与一个字符串相比,执行为1s;与table表中查询出来的firstdate相比,则耗时20s(table只有2行数据)。
请教大神分析问题出在哪?出在view这个视图脚本里?
...全文
754 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
二月十六 2018-08-23
  • 打赏
  • 举报
回复
执行view里边的select语句,看执行计划
吉普赛的歌 2018-08-23
  • 打赏
  • 举报
回复
发一下慢SQL的执行计划吧
吉普赛的歌 2018-08-23
  • 打赏
  • 举报
回复
贴了执行计划再说吧
稻庄 2018-08-23
  • 打赏
  • 举报
回复
引用 1 楼 yenange 的回复:
发一下慢SQL的执行计划吧
引用 2 楼 sinat_28984567 的回复:
执行view里边的select语句,看执行计划
应该这样描述: where date1 >= (select firstdate from table where id=1) 和 where date1 >= (select firstdate='2018-08-01' from table where id=1) 两种写法耗时区别很大。
稻庄 2018-08-23
  • 打赏
  • 举报
回复
引用 2 楼 sinat_28984567 的回复:
执行view里边的select语句,看执行计划
应该这样描述: where date1 >= (select firstdate from table where id=1) 和 where date1 >= (select firstdate='2018-08-01' from table where id=1) 两种写法耗时区别很大。

27,582

社区成员

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

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