sql - datetime变量与datetime变量的字符串表示形式

Sof*_*eek 4 sql string datetime date

当搜索参数恰好是带有日期的varchar数据类型时,我有一个查询需要很长时间才能响应.但是,如果我将varchar转换为datetime变量,则查询运行正常.例如:

这需要太长时间.

select count(id)
  from names
 where updateddate > '1/5/2010'
Run Code Online (Sandbox Code Playgroud)

这很好.

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

select count(id)
  from names
 where updateddate > @dateparam
Run Code Online (Sandbox Code Playgroud)

一个人运行正常但另一个不运行的原因是什么?

Joh*_*ers 5

因为在varchar的情况下,它必须转换为日期.这需要时间,并且可能妨碍索引的有效使用.

使用正确的类型几乎总是最好的.