检查空字符串时 COALESCE 与 IS NOT NULL 的性能
我在互联网上找到的一些文章将 ISNULL 与 COALESCE 进行了比较,所以我认为我的问题有点不同。
我想知道哪个在性能方面更好?
SELECT * FROM mytable WHERE mycolumn IS NOT NULL AND mycolumn <> '';
或者
SELECT * FROM mytable WHERE COALESCE(mycolumn,'') <> '';
除了性能之外,我在决定时还应该考虑其他问题吗?
编辑:
我正在使用 Teradata。
Some articles I found on the internet compared ISNULL with COALESCE, so I think my question is a little different.
I'm wondering which is better in terms of performance?
SELECT * FROM mytable WHERE mycolumn IS NOT NULL AND mycolumn <> '';
Or
SELECT * FROM mytable WHERE COALESCE(mycolumn,'') <> '';
Other than performance, are there any other concerns I should consider when deciding?
EDIT:
I'm using Teradata.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个版本稍微更具控制性,并且允许(可能)使用索引
它可以简化为
我说“稍微”和“可能”的原因是非相等谓词很可能意味着您最终会进行完整扫描。
This version is slightly more sargable and allows an index to be (potentially) used
It can be simplified to
The reason why I say "slightly" and "potentially" is that the non equality predicate may well mean you end up with a full scan anyway.
您还可以考虑使用ANSI_NULL ON 设置 。这将隐式过滤掉您发出搜索参数的列上的空值。这样做可以简化您的查询,我不确定它是否会使查询速度更快。从理论上讲,它应该是这样,因为需要评估的过滤器参数较少,并且在 where 子句中没有使用任何函数来实现完整的索引选择性。例如,您可以像这样重构您的查询:
我希望这有帮助。
You might also consider using the ANSI_NULL ON setting. This will implicitly filter out null values on a column where you issue a search argument. Doing this simplifies your query, i'm not sure if it makes it faster. Logically in theory it should though, since less filter arguments need to be evaluated and no functions are being used in where clause which should enable full index selectivity. As an example you could re-factor your query like this:
I hope this helps.