验证列是否具有空值

发布于 2024-07-17 08:27:10 字数 256 浏览 3 评论 0原文

哪种 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 技术交流群。

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

发布评论

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

评论(4

梦过后 2024-07-24 08:27:10

Count(columnName) 永远不会计算 NULL 值,当您指定列名时,count 会跳过 NULL,而当您使用 *

运行

CREATE TABLE testnulls (ID INT)
INSERT INTO testnulls VALUES (1)
INSERT INTO testnulls VALUES (2)
INSERT INTO testnulls VALUES (null)

SELECT count(*) FROM testnulls WHERE ID IS NULL --1

SELECT count(ID) FROM testnulls WHERE ID IS NULL --0

时,会计算 NULL。

IF EXISTS (SELECT 1 FROM testnulls WHERE ID IS NULL)
PRINT 'YES'
ELSE
PRINT 'NO'

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

CREATE TABLE testnulls (ID INT)
INSERT INTO testnulls VALUES (1)
INSERT INTO testnulls VALUES (2)
INSERT INTO testnulls VALUES (null)

SELECT count(*) FROM testnulls WHERE ID IS NULL --1

SELECT count(ID) FROM testnulls WHERE ID IS NULL --0

I would use exists instead since it is a boolean operation and will stop at the first occurance of NULL

IF EXISTS (SELECT 1 FROM testnulls WHERE ID IS NULL)
PRINT 'YES'
ELSE
PRINT 'NO'
冰火雁神 2024-07-24 08:27:10

基于 kquinn 的答案,在 Oracle 中这将是

SELECT COL1 FROM TABLE1 WHERE COL1 IS NULL AND ROWNUM = 1;

这样,DBMS 在给出答案之前只需读取一行;

然而,这种说法具有误导性。 它必须读取所有行,直到找到缺少列值的行。 然后它可以停止并返回该行。

如果没有这样的行,它将读取整个表。

因此可以使用 COL1 上的索引来满足查询,从而使查询速度更快。

仅指定 COL1 不会产生太大影响,至少在 Oracle 上是这样,(常规 B 树)索引不能用于查找 NULL 值。

如果您有兴趣稍后识别该行,您可能仍然需要选择更多列(例如主键值)。

Building on kquinn's answer, in Oracle that would be

SELECT COL1 FROM TABLE1 WHERE COL1 IS NULL AND ROWNUM = 1;

That way the DBMS only has to read a single row before giving you your answer;

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.

so it might be possible to satisfy the query with an index on COL1, making the query faster still.

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.

別甾虛僞 2024-07-24 08:27:10

我不了解 Oracle,但对于 SQL Server,这个选项可能是最快的:

SELECT TOP 1 COL1 FROM TABLE1 WHERE COL1 IS NULL;

这样 DBMS 在给你答案之前只需要读取一行; 其他选项必须读取所有非空行。 而且我指定了 COL1 而不是 *,因此可以使用 COL1 上的索引来满足查询,从而使查询更快仍然。

I don't know about Oracle, but for SQL Server this option is probably going to be fastest of all:

SELECT TOP 1 COL1 FROM TABLE1 WHERE COL1 IS NULL;

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 on COL1, making the query faster still.

貪欢 2024-07-24 08:27:10

多种解决方案(列包含一些 NULL | 列全部为 NULL * 测试单列 | 用表格结果测试多列)

如果需要测试多列,可以使用以下方法:

Column_1 Column_2 Column_3
-------- -------- --------
1        2        NULL
1        NULL     NULL
5        6        NULL

首先,测试 NULL并对它们进行计数:

select 
    sum(case when Column_1 is null then 1 else 0 end) as Column_1, 
    sum(case when Column_2 is null then 1 else 0 end) as Column_2, 
    sum(case when Column_3 is null then 1 else 0 end) as Column_3,
from TestTable 

产生 NULL 的计数:

Column_1  Column_2  Column_3
0         1         3

如果结果为 0,则没有 NULL。

第二,让我们计算非 NULL 值:

select 
    sum(case when Column_1 is null then 0 else 1 end) as Column_1, 
    sum(case when Column_2 is null then 0 else 1 end) as Column_2, 
    sum(case when Column_3 is null then 0 else 1 end) as Column_3,
from TestTable

...但是因为我们在这里计算非 NULL 值,所以可以简化为:

select 
    count(Column_1) as Column_1, 
    count(Column_2) as Column_2, 
    count(Column_3) as Column_3,
from TestTable

任一结果:

Column_1  Column_2  Column_3
3         2         0

如果结果为 0,则该列完全是由 NULL 组成。

最后,如果您只需要检查特定列,那么 TOP 1 会更快,因为它应该在第一次点击时停止。 然后,您可以选择使用 count(*) 给出布尔型结果:

select count(*) from (select top 1 'There is at least one NULL' AS note from TestTable where Column_3 is NULL) a

0 = 没有 NULL,1 = 至少有一个 NULL

select count(*) from (select top 1 'There is at least one non-NULL' AS note from TestTable where Column_3 is not NULL) a

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:

Column_1 Column_2 Column_3
-------- -------- --------
1        2        NULL
1        NULL     NULL
5        6        NULL

First, test for NULLs and count them:

select 
    sum(case when Column_1 is null then 1 else 0 end) as Column_1, 
    sum(case when Column_2 is null then 1 else 0 end) as Column_2, 
    sum(case when Column_3 is null then 1 else 0 end) as Column_3,
from TestTable 

Yields a count of NULLs:

Column_1  Column_2  Column_3
0         1         3

Where the result is 0, there are no NULLs.

Second, let's count the non-NULLs:

select 
    sum(case when Column_1 is null then 0 else 1 end) as Column_1, 
    sum(case when Column_2 is null then 0 else 1 end) as Column_2, 
    sum(case when Column_3 is null then 0 else 1 end) as Column_3,
from TestTable

...But because we're counting non-NULLs here, this can be simplified to:

select 
    count(Column_1) as Column_1, 
    count(Column_2) as Column_2, 
    count(Column_3) as Column_3,
from TestTable

Either one yields:

Column_1  Column_2  Column_3
3         2         0

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:

select count(*) from (select top 1 'There is at least one NULL' AS note from TestTable where Column_3 is NULL) a

0 = There are no NULLs, 1 = There is at least one NULL

select count(*) from (select top 1 'There is at least one non-NULL' AS note from TestTable where Column_3 is not NULL) a

0 = They are all NULL, 1 = There is at least one non-NULL

I hope this helps.

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