验证列是否具有空值
哪种 SQL 可以更快地验证特定列是否具有空值,为什么?
1) SELECT * FROM TABLE1 WHERE COL1 IS NULL
执行此查询,然后检查是否能够读取任何记录。 如果是,则有空值。
2) SELECT COUNT(COL1) FROM TABLE1 WHERE COL1 IS NULL
读取返回的计数以确定是否有任何空记录
使用Oracle10g 和SQLServer2005。
Which SQL would be faster to validate if a particular column has a null value or not, why?
1) SELECT * FROM TABLE1 WHERE COL1 IS NULL
Execute this query and then check if you are able to read any records. If yes there are null values.
2) SELECT COUNT(COL1) FROM TABLE1 WHERE COL1 IS NULL
Read the count which is returned to determine if there are any null records
Working with Oracle10g and SQLServer2005.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Count(columnName) 永远不会计算 NULL 值,当您指定列名时,count 会跳过 NULL,而当您使用 *
运行
时,会计算 NULL。
Count(columnName) will NEVER count NULL values, count skips NULLS when you specify a column name and does count NULLS when you use *
run this
I would use exists instead since it is a boolean operation and will stop at the first occurance of NULL
基于 kquinn 的答案,在 Oracle 中这将是
然而,这种说法具有误导性。 它必须读取所有行,直到找到缺少列值的行。 然后它可以停止并返回该行。
如果没有这样的行,它将读取整个表。
仅指定 COL1 不会产生太大影响,至少在 Oracle 上是这样,(常规 B 树)索引不能用于查找 NULL 值。
如果您有兴趣稍后识别该行,您可能仍然需要选择更多列(例如主键值)。
Building on kquinn's answer, in Oracle that would be
That statement is misleading, however. It has to read all rows until it finds one with the missing column value. Then it can stop and return that row.
If there is no such row, it will read the whole table.
Specifying only COL1 will not have too much impact, at least on Oracle, where (regular B-Tree) indices cannot be used to find NULL values.
You may want to select more columns anyway (such as the primary key value) if you are interested in identifiying the row later.
我不了解 Oracle,但对于 SQL Server,这个选项可能是最快的:
这样 DBMS 在给你答案之前只需要读取一行; 其他选项必须读取所有非空行。 而且我指定了
COL1
而不是*
,因此可以使用COL1
上的索引来满足查询,从而使查询更快仍然。I don't know about Oracle, but for SQL Server this option is probably going to be fastest of all:
That way the DBMS only has to read a single row before giving you your answer; the other options have to read all non-null rows. And I've specified
COL1
instead of*
, so it might be possible to satisfy the query with an index onCOL1
, making the query faster still.多种解决方案(列包含一些 NULL | 列全部为 NULL * 测试单列 | 用表格结果测试多列)
如果需要测试多列,可以使用以下方法:
首先,测试 NULL并对它们进行计数:
产生 NULL 的计数:
如果结果为 0,则没有 NULL。
第二,让我们计算非 NULL 值:
...但是因为我们在这里计算非 NULL 值,所以可以简化为:
任一结果:
如果结果为 0,则该列完全是由 NULL 组成。
最后,如果您只需要检查特定列,那么 TOP 1 会更快,因为它应该在第一次点击时停止。 然后,您可以选择使用 count(*) 给出布尔型结果:
0 = 没有 NULL,1 = 至少有一个 NULL
0 = 它们都是 NULL,1 = 至少有一个非 NULL
I希望这可以帮助。
Multiple Solutions (Column Contains Some NULLs | Column is All NULLs * Test Single Column | Test Multiple Columns with Tabular Results)
If you need to test multiple columns, you could use the following:
First, test for NULLs and count them:
Yields a count of NULLs:
Where the result is 0, there are no NULLs.
Second, let's count the non-NULLs:
...But because we're counting non-NULLs here, this can be simplified to:
Either one yields:
Where the result is 0, the column is entirely made up of NULLs.
Lastly, if you only need to check a specific column, then TOP 1 is quicker because it should stop at the first hit. You can then optionally use count(*) to give a boolean-style result:
0 = There are no NULLs, 1 = There is at least one NULL
0 = They are all NULL, 1 = There is at least one non-NULL
I hope this helps.