SQL - 日期时间变量与日期时间变量的字符串表示之间的区别

4

当搜索参数为varchar数据类型和日期时,我的查询响应时间过长。然而,如果我将varchar转换为datetime变量,查询就可以正常运行。例如:

这需要太长时间。

select count(id)
  from names
 where updateddate > '1/5/2010'

这个运行良好。

declare @dateparam datetime
    set @dateparam = convert(datetime, '1/5/2010',102)

select count(id)
  from names
 where updateddate > @dateparam

“为什么一个运行良好而另一个不行?”

3
现在你知道为什么数据库系统提供诸如DATETIME之类的类型了,因为它可以使计算更有效率。 - Jonathan Leffler
1个回答

5

因为在varchar的情况下,需要将其转换为日期。这需要时间,并且可能会阻止索引的有效使用。

最好始终使用正确的类型。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接