Dateadd 如何影响 SQL 查询的性能? [英] How Does Dateadd Impact the Performance of a SQL Query?

查看:63
本文介绍了Dateadd 如何影响 SQL 查询的性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我正在加入一个数字表以在子查询中的两个日期之间执行一些操作,如下所示:

Say for instance I'm joining on a number table to perform some operation between two dates in a subquery, like so:

select n
      ,(select avg(col1)
          from table1
         where timestamp between dateadd(minute, 15*n, @ArbitraryDate) 
                             and dateadd(minute, 15*(n+1), @ArbitraryDate))
  from numbers
 where n < 1200

如果我比使用 dateadd 函数通过连接 varchars 构造日期,查询的性能会更好吗?

Would the query perform better if I, say, constructed the date from concatenating varchars than using the dateadd function?

推荐答案

使用 DATEADD 以日期时间格式保存数据最有可能更快

Keeping data in the datetime format using DATEADD is most likely to be quicker

检查这个问题:最有效的方法在 SQL Server 中从日期+时间获取日期?

接受的答案(不是我!)通过字符串转换演示了 DATEADD.多年前我看过另一个显示相同的内容

The accepted answer (not me!) demonstrates DATEADD over string conversions. I've seen another too many years ago that showed the same

这篇关于Dateadd 如何影响 SQL 查询的性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆