2

我有这个 SQL 查询:

SELECT Start,
    ISNULL((SELECT Orders FROM [pp].dbo.VW_BEZETTING_RAW b2 WHERE b2.Start=b1.Start AND Afdeling = 'WP'),0) AS Werkplaats_ord,
    ISNULL((SELECT Tijd FROM [pp].dbo.VW_BEZETTING_RAW b2 WHERE b2.Start=b1.Start AND Afdeling = 'WP'),0) AS Werkplaats_tijd, 
    ISNULL((SELECT Orders FROM [pp].dbo.VW_BEZETTING_RAW b2 WHERE b2.Start=b1.Start AND Afdeling = 'MAG'),0) AS Magazijn_ord,
    ISNULL((SELECT Tijd FROM [pp].dbo.VW_BEZETTING_RAW b2 WHERE b2.Start=b1.Start AND Afdeling = 'MAG'),0) AS Magazijn_tijd, 
    ISNULL((SELECT Orders FROM [pp].dbo.VW_BEZETTING_RAW b2 WHERE b2.Start=b1.Start AND Afdeling = 'SERV'),0) AS Service_ord,
    ISNULL((SELECT Tijd FROM [pp].dbo.VW_BEZETTING_RAW b2 WHERE b2.Start=b1.Start AND Afdeling = 'SERV'),0) AS Service_tijd, 
    ISNULL((SELECT Orders FROM [pp].dbo.VW_BEZETTING_RAW b2 WHERE b2.Start=b1.Start AND Afdeling = 'CNC'),0) AS Draaibank_ord,
    ISNULL((SELECT Tijd FROM [pp].dbo.VW_BEZETTING_RAW b2 WHERE b2.Start=b1.Start AND Afdeling = 'CNC'),0) AS Draaibank_tijd, 
    ISNULL((SELECT Orders FROM [pp].dbo.VW_BEZETTING_RAW b2 WHERE b2.Start=b1.Start AND Afdeling = 'SECL'),0) AS Bougiekabels_ord,
    ISNULL((SELECT Tijd FROM [pp].dbo.VW_BEZETTING_RAW b2 WHERE b2.Start=b1.Start AND Afdeling = 'SECL'),0) AS Bougiekabels_tijd 
FROM [pp].dbo.VW_BEZETTING_RAW b1
GROUP BY Start

我可以解释我的查询是做什么的,但我认为这不相关,因为查询已经可以正常工作。我唯一的问题是处理时间;我必须等待 20-25 秒才能得到结果,我认为这有点长。

SQL Server 似乎在做的(我认为)是SELECT我的值 11 次[pp].dbo.VW_BEZETTING_RAW(我的每个子查询为 10 次,我的正常查询为 1 次)。这是一项繁重的任务,因为SELECT * FROM [pp].dbo.VW_BEZETTING_RAW处理时间大约为 2 秒。SELECT不是必需的,它应该足以引用一次并在每个子查询中使用结果。我测试了我的性能问题是否真的是因为子查询,它是:当我将查询减少到 2 个而不是 10 个子查询时,处理时间仅为 6-7 秒。

仅供参考,以下是结果的一部分[pp].dbo.VW_BEZETTING_RAW

    Start       Afdeling    Orders  Tijd
    2013-05-14  SERV        3       0
    2013-05-14  WP          17      0
    2013-05-15  MAG         1       0
    2013-05-15  SERV        3       0
    2013-05-15  WP          14      0
    2013-05-16  CNC         1       0
    2013-05-16  MAG         9       0
    2013-05-16  SERV        3       0
    2013-05-16  WP          22      0
    2013-05-17  MAG         19      0
    2013-05-17  WP          8       0
    2013-05-20  MAG         11      0

我的明确问题是:是否有可能只SELECT获得一次结果[pp].dbo.VW_BEZETTING_RAW并在每个子查询中使用此结果(而不是再次触发此SELECT查询 10 次),或者,如果不可能,是否有任何其他方法来减少处理我的查询时间?

4

2 回答 2

5

此查询应该可以解决您的问题(包括您是否可以有负订单/时间值)。

SELECT Start,
    ISNULL(MIN(CASE WHEN Afdeling = 'WP' THEN Orders END),0) AS Werkplaats_ord,
    ISNULL(MIN(CASE WHEN Afdeling = 'WP' THEN Tijd END),0) AS Werkplaats_tijd, 
    ISNULL(MIN(CASE WHEN Afdeling = 'MAG' THEN Orders END),0) AS Magazijn_ord,
    ISNULL(MIN(CASE WHEN Afdeling = 'MAG' THEN Tijd END),0) AS Magazijn_tijd, 
    ISNULL(MIN(CASE WHEN Afdeling = 'SERV' THEN Orders END),0) AS Service_ord,
    ISNULL(MIN(CASE WHEN Afdeling = 'SERV' THEN Tijd END),0) AS Service_tijd, 
    ISNULL(MIN(CASE WHEN Afdeling = 'CNC' THEN Orders END),0) AS Draaibank_ord,
    ISNULL(MIN(CASE WHEN Afdeling = 'CNC' THEN Tijd END),0) AS Draaibank_tijd, 
    ISNULL(MIN(CASE WHEN Afdeling = 'SECL' THEN Orders END),0) AS Bougiekabels_ord,
    ISNULL(MIN(CASE WHEN Afdeling = 'SECL' THEN Tijd END),0) AS Bougiekabels_tijd 
FROM [pp].dbo.VW_BEZETTING_RAW b1
GROUP BY Start
于 2013-05-14T10:18:33.023 回答
2

尝试这个 -

;WITH cte AS 
(
    SELECT 
          Orders
        , Tijd
        , Afdeling
        , Start 
    FROM dbo.VW_BEZETTING_RAW    
)
SELECT Start,
    ISNULL((SELECT Orders FROM cte b2 WHERE b2.Start=b1.Start AND Afdeling = 'WP'),0) AS Werkplaats_ord,
    ISNULL((SELECT Tijd FROM cte b2 WHERE b2.Start=b1.Start AND Afdeling = 'WP'),0) AS Werkplaats_tijd, 
    ISNULL((SELECT Orders FROM cte b2 WHERE b2.Start=b1.Start AND Afdeling = 'MAG'),0) AS Magazijn_ord,
    ISNULL((SELECT Tijd FROM cte b2 WHERE b2.Start=b1.Start AND Afdeling = 'MAG'),0) AS Magazijn_tijd, 
    ISNULL((SELECT Orders FROM cte b2 WHERE b2.Start=b1.Start AND Afdeling = 'SERV'),0) AS Service_ord,
    ISNULL((SELECT Tijd FROM cte b2 WHERE b2.Start=b1.Start AND Afdeling = 'SERV'),0) AS Service_tijd, 
    ISNULL((SELECT Orders FROM cte b2 WHERE b2.Start=b1.Start AND Afdeling = 'CNC'),0) AS Draaibank_ord,
    ISNULL((SELECT Tijd FROM cte b2 WHERE b2.Start=b1.Start AND Afdeling = 'CNC'),0) AS Draaibank_tijd, 
    ISNULL((SELECT Orders FROM cte b2 WHERE b2.Start=b1.Start AND Afdeling = 'SECL'),0) AS Bougiekabels_ord,
    ISNULL((SELECT Tijd FROM cte b2 WHERE b2.Start=b1.Start AND Afdeling = 'SECL'),0) AS Bougiekabels_tijd 
FROM cte b1
GROUP BY Start

或者试试这个 -

SELECT 
    Start,
    Werkplaats_ord = MIN(CASE WHEN Afdeling = 'WP' THEN Orders ELSE 0 END) ,
    Werkplaats_tijd = MIN(CASE WHEN Afdeling = 'WP' THEN Tijd ELSE 0 END) , 
    Magazijn_ord = MIN(CASE WHEN Afdeling = 'MAG' THEN Orders ELSE 0 END) ,
    Magazijn_tijd = MIN(CASE WHEN Afdeling = 'MAG' THEN Tijd ELSE 0 END) , 
    Service_ord = MIN(CASE WHEN Afdeling = 'SERV' THEN Orders ELSE 0 END) ,
    Service_tijd = MIN(CASE WHEN Afdeling = 'SERV' THEN Tijd ELSE 0 END) , 
    Draaibank_ord = MIN(CASE WHEN Afdeling = 'CNC' THEN Orders ELSE 0 END) ,
    Draaibank_tijd = MIN(CASE WHEN Afdeling = 'CNC' THEN Tijd ELSE 0 END) , 
    Bougiekabels_ord = MIN(CASE WHEN Afdeling = 'SECL' THEN Orders ELSE 0 END) ,
    Bougiekabels_tijd = MIN(CASE WHEN Afdeling = 'SECL' THEN Tijd ELSE 0 END)  
FROM dbo.VW_BEZETTING_RAW b1
GROUP BY Start
于 2013-05-14T10:21:15.347 回答