开发者社区> 问答> 正文

当查询包含不同的排序方式和内部联接时,如何避免在Union All中使用TempTable?

我想做的是始终将数量为0的产品发送到已排序列表的末尾而又不丢失当前的排序(如我在以下问题中所描述的如何将零数量产品发送到PagedList 的末尾?)

所以我有一个Sorted temptable(按用户选择的字母顺序,价格或更新的产品排序):

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     |  
+------------+---------------+

PS:我的产品表,我必须与tmptable内连接以查找产品数量。

```js

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

到目前为止,我尝试过的是:(延迟运行,并且出现性能问题,我几乎有3万种产品)

```js
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

有什么方法可以避免在此查询中创建TempTable?在不创建其他三个tempTable的情况下,将第一个临时表的0个数量的产品发送到数据列表的末尾,而不会丢失基于Identity ID的当前排序。我的查询出现性能问题。

我不得不再说一遍,该临时表具有标识插入ID,并且它是基于排序的排序类型,用户将其传递给存储过程。谢谢你们 :)

展开
收起
心有灵_夕 2019-12-22 13:58:34 682 0
0 条回答
写回答
取消 提交回答
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
RowKey与索引设计:技巧与案例分析 立即下载
Phoenix 全局索引原理与实践 立即下载
事务、全局索引、透明分布式 立即下载