<tfoot id='j5pWD'></tfoot>

    <small id='j5pWD'></small><noframes id='j5pWD'>

    1. <i id='j5pWD'><tr id='j5pWD'><dt id='j5pWD'><q id='j5pWD'><span id='j5pWD'><b id='j5pWD'><form id='j5pWD'><ins id='j5pWD'></ins><ul id='j5pWD'></ul><sub id='j5pWD'></sub></form><legend id='j5pWD'></legend><bdo id='j5pWD'><pre id='j5pWD'><center id='j5pWD'></center></pre></bdo></b><th id='j5pWD'></th></span></q></dt></tr></i><div id='j5pWD'><tfoot id='j5pWD'></tfoot><dl id='j5pWD'><fieldset id='j5pWD'></fieldset></dl></div>
      <legend id='j5pWD'><style id='j5pWD'><dir id='j5pWD'><q id='j5pWD'></q></dir></style></legend>
        <bdo id='j5pWD'></bdo><ul id='j5pWD'></ul>

      1. 不存在与不存在

        时间:2023-07-18

      2. <legend id='uFJIB'><style id='uFJIB'><dir id='uFJIB'><q id='uFJIB'></q></dir></style></legend>

          <tfoot id='uFJIB'></tfoot>

          • <i id='uFJIB'><tr id='uFJIB'><dt id='uFJIB'><q id='uFJIB'><span id='uFJIB'><b id='uFJIB'><form id='uFJIB'><ins id='uFJIB'></ins><ul id='uFJIB'></ul><sub id='uFJIB'></sub></form><legend id='uFJIB'></legend><bdo id='uFJIB'><pre id='uFJIB'><center id='uFJIB'></center></pre></bdo></b><th id='uFJIB'></th></span></q></dt></tr></i><div id='uFJIB'><tfoot id='uFJIB'></tfoot><dl id='uFJIB'><fieldset id='uFJIB'></fieldset></dl></div>
              <tbody id='uFJIB'></tbody>

            • <small id='uFJIB'></small><noframes id='uFJIB'>

                  <bdo id='uFJIB'></bdo><ul id='uFJIB'></ul>
                  本文介绍了不存在与不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这些查询中哪个更快?

                  不存在:

                  SELECT ProductID, ProductNameFROM Northwind..Products p不存在的地方(选择 1FROM Northwind..[订单详情] od其中 p.ProductId = od.ProductId)

                  或不在:

                  SELECT ProductID, ProductNameFROM Northwind..Products pp.ProductID 不在 (选择产品 ID从北风..[订单详情])

                  查询执行计划说他们都做同样的事情.如果是这样,推荐的形式是什么?

                  这是基于 NorthWind 数据库.

                  刚刚找到这篇有用的文章:.在示例中,逻辑读取的数量从大约 400 次增加到 500,000 次.

                  此外,单个 NULL 可以将行数减少到零这一事实使得基数估计变得非常困难.如果 SQL Server 假设会发生这种情况,但实际上数据中没有 NULL 行,则执行计划的其余部分可能会更糟,如果这只是较大查询的一部分,.只需将 Sales.SalesOrderDetail.ProductID = <correlated_product_id> 上的先前单个相关索引查找转换为每个外行的两个查找即可.另一个位于 WHERE Sales.SalesOrderDetail.ProductID IS NULL 上.

                  由于这是在反半连接下,如果该连接返回任何行,则不会发生第二次查找.但是,如果 Sales.SalesOrderDetail 不包含任何 NULL ProductIDs,它将使所需的查找操作次数增加一倍.

                  Which of these queries is the faster?

                  NOT EXISTS:

                  SELECT ProductID, ProductName 
                  FROM Northwind..Products p
                  WHERE NOT EXISTS (
                      SELECT 1 
                      FROM Northwind..[Order Details] od 
                      WHERE p.ProductId = od.ProductId)
                  

                  Or NOT IN:

                  SELECT ProductID, ProductName 
                  FROM Northwind..Products p
                  WHERE p.ProductID NOT IN (
                      SELECT ProductID 
                      FROM Northwind..[Order Details])
                  

                  The query execution plan says they both do the same thing. If that is the case, which is the recommended form?

                  This is based on the NorthWind database.

                  [Edit]

                  Just found this helpful article: http://weblogs.sqlteam.com/mladenp/archive/2007/05/18/60210.aspx

                  I think I'll stick with NOT EXISTS.

                  解决方案

                  I always default to NOT EXISTS.

                  The execution plans may be the same at the moment but if either column is altered in the future to allow NULLs the NOT IN version will need to do more work (even if no NULLs are actually present in the data) and the semantics of NOT IN if NULLs are present are unlikely to be the ones you want anyway.

                  When neither Products.ProductID or [Order Details].ProductID allow NULLs the NOT IN will be treated identically to the following query.

                  SELECT ProductID,
                         ProductName
                  FROM   Products p
                  WHERE  NOT EXISTS (SELECT *
                                     FROM   [Order Details] od
                                     WHERE  p.ProductId = od.ProductId) 
                  

                  The exact plan may vary but for my example data I get the following.

                  A reasonably common misconception seems to be that correlated sub queries are always "bad" compared to joins. They certainly can be when they force a nested loops plan (sub query evaluated row by row) but this plan includes an anti semi join logical operator. Anti semi joins are not restricted to nested loops but can use hash or merge (as in this example) joins too.

                  /*Not valid syntax but better reflects the plan*/ 
                  SELECT p.ProductID,
                         p.ProductName
                  FROM   Products p
                         LEFT ANTI SEMI JOIN [Order Details] od
                           ON p.ProductId = od.ProductId 
                  

                  If [Order Details].ProductID is NULL-able the query then becomes

                  SELECT ProductID,
                         ProductName
                  FROM   Products p
                  WHERE  NOT EXISTS (SELECT *
                                     FROM   [Order Details] od
                                     WHERE  p.ProductId = od.ProductId)
                         AND NOT EXISTS (SELECT *
                                         FROM   [Order Details]
                                         WHERE  ProductId IS NULL) 
                  

                  The reason for this is that the correct semantics if [Order Details] contains any NULL ProductIds is to return no results. See the extra anti semi join and row count spool to verify this that is added to the plan.

                  If Products.ProductID is also changed to become NULL-able the query then becomes

                  SELECT ProductID,
                         ProductName
                  FROM   Products p
                  WHERE  NOT EXISTS (SELECT *
                                     FROM   [Order Details] od
                                     WHERE  p.ProductId = od.ProductId)
                         AND NOT EXISTS (SELECT *
                                         FROM   [Order Details]
                                         WHERE  ProductId IS NULL)
                         AND NOT EXISTS (SELECT *
                                         FROM   (SELECT TOP 1 *
                                                 FROM   [Order Details]) S
                                         WHERE  p.ProductID IS NULL) 
                  

                  The reason for that one is because a NULL Products.ProductId should not be returned in the results except if the NOT IN sub query were to return no results at all (i.e. the [Order Details] table is empty). In which case it should. In the plan for my sample data this is implemented by adding another anti semi join as below.

                  The effect of this is shown in the blog post already linked by Buckley. In the example there the number of logical reads increase from around 400 to 500,000.

                  Additionally the fact that a single NULL can reduce the row count to zero makes cardinality estimation very difficult. If SQL Server assumes that this will happen but in fact there were no NULL rows in the data the rest of the execution plan may be catastrophically worse, if this is just part of a larger query, with inappropriate nested loops causing repeated execution of an expensive sub tree for example.

                  This is not the only possible execution plan for a NOT IN on a NULL-able column however. This article shows another one for a query against the AdventureWorks2008 database.

                  For the NOT IN on a NOT NULL column or the NOT EXISTS against either a nullable or non nullable column it gives the following plan.

                  When the column changes to NULL-able the NOT IN plan now looks like

                  It adds an extra inner join operator to the plan. This apparatus is explained here. It is all there to convert the previous single correlated index seek on Sales.SalesOrderDetail.ProductID = <correlated_product_id> to two seeks per outer row. The additional one is on WHERE Sales.SalesOrderDetail.ProductID IS NULL.

                  As this is under an anti semi join if that one returns any rows the second seek will not occur. However if Sales.SalesOrderDetail does not contain any NULL ProductIDs it will double the number of seek operations required.

                  这篇关于不存在与不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:用于连接 Oracle 中多行列值的 SQL 查询 下一篇:在 SQL 中转置列和行的简单方法?

                  相关文章

                    <small id='RTs3C'></small><noframes id='RTs3C'>

                  1. <i id='RTs3C'><tr id='RTs3C'><dt id='RTs3C'><q id='RTs3C'><span id='RTs3C'><b id='RTs3C'><form id='RTs3C'><ins id='RTs3C'></ins><ul id='RTs3C'></ul><sub id='RTs3C'></sub></form><legend id='RTs3C'></legend><bdo id='RTs3C'><pre id='RTs3C'><center id='RTs3C'></center></pre></bdo></b><th id='RTs3C'></th></span></q></dt></tr></i><div id='RTs3C'><tfoot id='RTs3C'></tfoot><dl id='RTs3C'><fieldset id='RTs3C'></fieldset></dl></div>

                    1. <tfoot id='RTs3C'></tfoot><legend id='RTs3C'><style id='RTs3C'><dir id='RTs3C'><q id='RTs3C'></q></dir></style></legend>
                        <bdo id='RTs3C'></bdo><ul id='RTs3C'></ul>