测试 T-SQL 中的不等式 [英] Testing for inequality in T-SQL

查看:29
本文介绍了测试 T-SQL 中的不等式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在 WHERE 子句中遇到了这个:

I've just come across this in a WHERE clause:

AND NOT (t.id = @id)

这与以下内容相比如何:

How does this compare with:

AND t.id != @id

或与:

AND t.id <> @id

我总是自己写后者,但显然其他人的想法不同.一个会比另一个表现更好吗?我知道使用 <>!= 会破坏使用我可能拥有的索引的任何希望,但肯定上面的第一种方法会遇到同样的问题吗?

I'd always write the latter myself, but clearly someone else thinks differently. Is one going to perform any better than the other? I know that using <> or != is going to bust any hopes for using an index that I might have had, but surely the first approach above will suffer the same problem?

推荐答案

这3个会得到完全一样的执行计划

These 3 will get the same exact execution plan

declare @id varchar(40)
select @id = '172-32-1176'

select * from authors
where au_id <> @id

select * from authors
where au_id != @id

select * from authors
where not (au_id = @id)

这当然也取决于索引本身的选择性.我总是使用 au_id <> @id 自己

It will also depend on the selectivity of the index itself of course. I always use au_id <> @id myself

这篇关于测试 T-SQL 中的不等式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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