检查空字符串时 COALESCE 与 IS NOT NULL 的性能

发布于 2024-09-27 07:53:46 字数 361 浏览 0 评论 0原文

我在互联网上找到的一些文章将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

棒棒糖 2024-10-04 07:53:46

这个版本稍微更具控制性,并且允许(可能)使用索引

SELECT * FROM mytable WHERE mycolumn IS NOT NULL AND mycolumn <> '';

它可以简化为

SELECT * FROM mytable WHERE mycolumn <> '';

我说“稍微”和“可能”的原因是非相等谓词很可能意味着您最终会进行完整扫描。

This version is slightly more sargable and allows an index to be (potentially) used

SELECT * FROM mytable WHERE mycolumn IS NOT NULL AND mycolumn <> '';

It can be simplified to

SELECT * FROM mytable WHERE mycolumn <> '';

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.

杯别 2024-10-04 07:53:46

您还可以考虑使用ANSI_NULL ON 设置 。这将隐式过滤掉您发出搜索参数的列上的空值。这样做可以简化您的查询,我不确定它是否会使查询速度更快。从理论上讲,它应该是这样,因为需要评估的过滤器参数较少,并且在 where 子句中没有使用任何函数来实现完整的索引选择性。例如,您可以像这样重构您的查询:

SET ANSI_NULLS ON;
SELECT * FROM mytable WHERE NOT mycolumn = '';

我希望这有帮助。

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:

SET ANSI_NULLS ON;
SELECT * FROM mytable WHERE NOT mycolumn = '';

I hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文