代码之家  ›  专栏  ›  技术社区  ›  tgandrews

IsNull Vs为空

  •  19
  • tgandrews  · 技术社区  · 16 年前

    我注意到在工作中有许多查询,因此在表单中使用了限制:

    isnull(name,'') <> ''
    

    人们这样做有什么特别的原因,而不是越简单

    name is not null
    

    它是遗留问题还是性能问题?

    10 回复  |  直到 11 年前
        1
  •  35
  •   Martin Smith    11 年前
    where isNull(name,“)<>''
    < /代码> 
    
    

    等于

    where name is not null and name<gt;“”
    < /代码> 
    
    

    反过来相当于

    where name <> ''
    < /代码> 
    
    

    (if name IS NULL that final expression would evaluate to unknown and the row not returned)

    The use of the ISNULL pattern will result in a scan and is less efficient as can be seen in the below test.

    select ca.[名称],
    [数]
    [类型],
    [低],
    [高],
    [地位]
    进入测试表
    来自[master]。[dbo]。[spt_values]
    交叉应用(选择[名称]
    联合所有
    选择’
    联合所有
    选择空)CA
    
    
    在dbo.testtable(name)上创建非聚集索引ix_testtable
    
    去
    
    
    从测试表中选择名称,其中IsNull(名称“”)<>“”
    
    从名称不为空且名称为<>'的测试表中选择名称。
    /*可以简化为仅在名称<>“”处*/
    < /代码> 
    
    

    这应该给你你所需要的执行计划。

    enter image description here

    反过来相当于

    where name <> ''
    

    (如果名字IS NULL最后一个表达式的值将为Unknown,但未返回行)

    使用的ISNULL模式将导致扫描,并且效率较低,如下面的测试所示。

    SELECT ca.[name],
           [number],
           [type],
           [low],
           [high],
           [status]
    INTO   TestTable
    FROM   [master].[dbo].[spt_values]
           CROSS APPLY (SELECT [name]
                        UNION ALL
                        SELECT ''
                        UNION ALL
                        SELECT NULL) ca 
    
    
    CREATE NONCLUSTERED INDEX IX_TestTable ON dbo.TestTable(name)
    
    GO
    
    
    SELECT name FROM TestTable WHERE isnull(name,'') <> ''
    
    SELECT name FROM TestTable WHERE name is not null and name <> ''
    /*Can be simplified to just WHERE name <> '' */
    

    它应该给你你需要的执行计划。

    enter image description here

        2
  •  13
  •   Justin Niessner    16 年前
    is not null
    

    isnull(name, '') <> name
    

    检查空字符串和空字符串。

        3
  •  2
  •   Donnie    16 年前

    isnull(name,'') <> :name 是 (name is null or name <> :name) (假设 :name 从不包含空字符串,因此为什么这样的速记会很糟糕)。

    就性能而言,这取决于。 or 语句 where 子句的性能可能非常差。但是,列上的函数会影响索引的使用。和往常一样:简介。

        4
  •  1
  •   kemiller2002    16 年前
    isnull(name,'') <> name
    

    Well I can see them using this because this way if the name doesn't match or is null it returns as a failed comparison. This really means: name is null 或 name <> name

    这个在哪 name is not null 只需检查名称是否为空。

        5
  •  1
  •   HLGEM    16 年前

    他们的意思不一样。

    name is not null 
    

    这将检查名称字段为空的记录

    isnull(name,'') <> name  
    

    This one changes the value of null fields to the empty string so they can be used in a comparision. In SQL Server (but not in Oracle I think), if a value is null and it is used to compare equlaity or inequality it will not be considered becasue null means I don't know the value and thus is not an actual value. So if you want to make sure the null records are considered when doing the comparision, you need ISNULL or COALESCE(which is the ASCII STANDARD term to use as ISNULL doen't work in all databases).

    你应该看到的是

    isnull(a.name,'') <> b.name  
    

    a.名称<gt;b.名称

    然后您将了解为什么需要IsNull来获得正确的结果。

        6
  •  1
  •   JC Ford    16 年前

    我显然误解了你的问题。所以让我先回答我的第一个问题,然后试试这个问题:

    isnull(name,'') <> ''
    

    是一个误入歧途的捷径

    name is not null and name <> ''
    
        7
  •  1
  •   Jay    16 年前

    Others have pointed out the functional difference. As to the performance issue, in Postgres I've found that -- oh, I should mention that Postgres has a function "coalesce" that is the equivalent of the "isnull" found in some other SQL dialects -- but in Postgres, saying

    where coalesce(foobar,'')=''
    

    明显快于

    where foobar is null or foobar=''
    

    而且,说起来可能会快得惊人。

     where foobar>''
    

    结束

    where foobar!=''
    

    大于测试可以使用索引,从而跳过所有空白,而不相等的测试必须进行完整的文件读取。(假设您在字段上有一个索引,并且没有使用其他索引。)

        8
  •  0
  •   Madhivanan    16 年前

    另外,如果要使用该列的索引,请使用

    name is not null and name <> '' 
    
        9
  •  0
  •   A-K    16 年前

    这两个查询不同。例如,我没有中间名,这是一个已知事实,可以存储为

    MiddleName=''
    

    但是,如果我们不知道某人的中间名,我们可以存储空值。 因此,isnull(中间名,“)是指“没有已知中间名的人”。

        10
  •  0
  •   onedaywhen    16 年前

    它处理空字符串和 NULL . 虽然能用一句话来做很好, isnull 是专有语法。我会用可移植的标准SQL编写这个

    NULLIF(name, '') IS NOT NULL
    
    推荐文章