当查询包含不同的 order by 和 inner join 时,如何避免 Union All 中的 TempTable?

How To Avoid TempTable in Union All when queries contain DIFFERENT order by and inner join?

我想做的是始终将数量为 0 的产品发送到已排序的温度的末尾Table而不丢失当前排序(如我在下面的问题How to send Zero Qty Products to the end of a PagedList<Products>?)

我有一个 Sorted temptable 已填充(它按用户选择的内容排序,如字母、价格或较新的产品,排序基于身份 ID):

CREATE TABLE #DisplayOrderTmp 
(
    [Id] int IDENTITY (1, 1) NOT NULL,
    [ProductId] int NOT NULL
)

sorted #DisplayOrderTmp :
+------------+---------------+  
|     id     |  ProductId    |  
+------------+---------------+  
|     1      |     66873     |  // Qty is 0
|     2      |     70735     |  // Qty is not 0
|     3      |     17121     |  // Qty is not 0
|     4      |     48512     |  // Qty is not 0
|     5      |     51213     |  // Qty is 0
+------------+---------------+

我想将此数据传递到网页,但在此之前我需要将数量为零的产品发送到此列表的末尾而不丢失当前排序方式)

我返回的数据应该是这样的(排序没有改变,只是 0 数量的产品按顺序排到了列表末尾):

CREATE TABLE #DisplayOrderTmp4 
(
    [Id] int IDENTITY (1, 1) NOT NULL,
    [ProductId] int NOT NULL
)
+------------+---------------+  
|     id     |  ProductId    |  
+------------+---------------+  
|     1      |     70735     |  
|     2      |     17121     |  
|     3      |     48512     | 
|     4      |     66873     |   
|     5      |     51213     |  
+------------+---------------+ 

P.S:这是我的产品Table,我必须在内部加入 tmptable 才能找到产品数量。

    Product Table is like this :
    +------------+---------------+------------------+  
    |     id     |   stockqty    | DisableBuyButton |
    +------------+---------------+------------------+  
    |   17121    |      1        |         0        | 
    |   48512    |      27       |         0        |     
    |   51213    |      0        |         1        |
    |   66873    |      0        |         1        |
    |   70735    |      11       |         0        |
    +------------+---------------+------------------+

到目前为止我尝试过的是:(它工作 有延迟并且有性能问题我几乎有 30k 产品

INSERT INTO #DisplayOrderTmp2 ([ProductId])
        SELECT p2.ProductId
        FROM  #DisplayOrderTmp p2  with (NOLOCK) // it's already sorted table
        INNER JOIN Product prd with (NOLOCK) 
        ON p2.ProductId=prd.Id  
        and prd.DisableBuyButton=0     // to find product with qty more than 0
        group by p2.ProductId  order by min(p2.Id) // to save current ordering

INSERT INTO #DisplayOrderTmp3 ([ProductId])     
        SELECT p2.ProductId     
        FROM  #DisplayOrderTmp p2  with (NOLOCK) //it's already sorted table    
        INNER JOIN Product prd with (NOLOCK)
        ON p2.ProductId=prd.Id  
        and prd.DisableBuyButton=1    // to find product with qty equal to 0
        group by p2.ProductId  order by min(p2.Id) // to save current ordering


        INSERT INTO #DisplayOrderTmp4 ([ProductId]) // finally Union All this two data
          SELECT p2.ProductId FROM  
          #DisplayOrderTmp2 p2  with (NOLOCK)  // More than 0 qty products with saved ordering

        UNION ALL 

        SELECT p2.ProductId   FROM 
        #DisplayOrderTmp3 p2  with (NOLOCK)  //  0 qty products with saved ordering

Is there any way To Avoid creating TempTable in this query? send 0 quantity products of first temptable to the end of data-list without creating three other tempTable , without loosing current ordering based by Identity ID. My query has performance problem.

我不得不再说一遍,temptable 有一个身份插入 ID 列,它是基于用户传递给存储过程的排序类型进行排序的。 谢谢大家:)

您可以使用 ORDER BY 而无需使用 UNION ALL:

SELECT p2.ProductId
FROM #DisplayOrderTmp p2
JOIN Product prd
  ON p2.ProductId=prd.Id  
ORDER BY prd.DisableBuyButton, p2.id;

DisableBuyButton = 0 - qnt > 0 DisableBuyButton = 1 - qnt = 0

看来它只需要在 order by 中添加一些额外的东西。

可以使用IIFCASE来优先排序

SELECT tmp.ProductId
FROM #DisplayOrderTmp tmp
JOIN Product prd
  ON prd.Id = tmp.ProductId
 AND prd.DisableBuyButton IN (0,1)
ORDER BY IIF(prd.DisableBuyButton=0,1,2), tmp.id;

确保临时 table 有一个以 Id 作为前导列的索引或主键。这将有助于避免排序计划中的排序运算符:

CREATE TABLE #DisplayOrderTmp
(
    [Id] int NOT NULL,
    [ProductId] int NOT NULL
    ,PRIMARY KEY CLUSTERED(Id)
);

使用该索引,假设 ProductID 是 Product table 主键,您应该能够使用 UNION ALL 查询以合理的效率获得结果而无需额外的临时 tables :

WITH products AS (
    SELECT p2.Id, p2.ProductId, prd.stockqty, 1 AS seq
    FROM #DisplayOrderTmp p2
    JOIN Product prd
          ON p2.ProductId=prd.Id
    WHERE prd.stockqty > 0
    UNION ALL
    SELECT p2.Id, p2.ProductId, prd.stockqty, 2 AS seq
    FROM #DisplayOrderTmp p2
    JOIN Product prd
          ON p2.ProductId=prd.Id
    WHERE prd.stockqty = 0
)
SELECT ProductId
FROM products
ORDER BY seq, Id;

您在评论中提到您最终想要一个分页结果。这可以在 T-SQL 中通过将 OFFSETFETCH 添加到 ORDER BY 子句来完成,如下所示。但是,请注意,对大型结果集的分页会随着查询结果的深入而逐渐变慢。

WITH products AS (
    SELECT p2.Id, p2.ProductId, prd.stockqty, 1 AS seq
    FROM #DisplayOrderTmp p2
    JOIN Product prd
          ON p2.ProductId=prd.Id
    WHERE prd.stockqty > 0
    UNION ALL
    SELECT p2.Id, p2.ProductId, prd.stockqty, 2 AS seq
    FROM #DisplayOrderTmp p2
    JOIN Product prd
          ON p2.ProductId=prd.Id
    WHERE prd.stockqty = 0
)
SELECT ProductId
FROM products
ORDER BY seq, Id
OFFSET @PageSize * (@PageNumber - 1) ROWS
FETCH NEXT @PageSize ROWS ONLY;