我有这个 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 次),或者,如果不可能,是否有任何其他方法来减少处理我的查询时间?