私はこのクエリを持っています...
SELECT Distinct([TargetAttributeID]) FROM
(SELECT distinct att1.intAttributeID as [TargetAttributeID]
FROM AST_tblAttributes att1
INNER JOIN
AST_lnkProfileDemandAttributes pda
ON pda.intAttributeID=att1.intAttributeID AND pda.intProfileID = @intProfileID
union all
SELECT distinct ca2.intAttributeID as [TargetAttributeID] FROM
AST_lnkCapturePolicyAttributes ca2
INNER JOIN
AST_lnkEmployeeCapture ec2 ON ec2.intAdminCaptureID = ca2.intAdminCaptureID AND ec2.intTeamID = 57
WHERE ec2.dteCreatedDate >= @cutoffdate) x
2つの内部の区別は、それぞれ32行と10,000行を見ています。このクエリは5行を返し、1秒以内に実行されます。
次に、このクエリの結果を同様の件名として使用するとIN
...
SELECT attx.intAttributeID,attx.txtAttributeName,attx.txtAttributeLabel,attx.txtType,attx.txtEntity FROM
AST_tblAttributes attx WHERE attx.intAttributeID
IN
(SELECT Distinct([TargetAttributeID]) FROM
(SELECT Distinct att1.intAttributeID as [TargetAttributeID]
FROM AST_tblAttributes att1
INNER JOIN
AST_lnkProfileDemandAttributes pda
ON pda.intAttributeID=att1.intAttributeID AND pda.intProfileID = @intProfileID
union all
SELECT Distinct ca2.intAttributeID as [TargetAttributeID] FROM
AST_lnkCapturePolicyAttributes ca2
INNER JOIN
AST_lnkEmployeeCapture ec2 ON ec2.intAdminCaptureID = ca2.intAdminCaptureID AND ec2.intTeamID = 57
WHERE ec2.dteCreatedDate >= @cutoffdate) x)
それから3分以上かかります!クエリの結果を取得してIN
「手動」で実行すると、非常に迅速に返されます。
しかし、2つの内側を削除するとDISTINCTS
...。
SELECT attx.intAttributeID,attx.txtAttributeName,attx.txtAttributeLabel,attx.txtType,attx.txtEntity FROM
AST_tblAttributes attx WHERE attx.intAttributeID
IN
(SELECT Distinct([TargetAttributeID]) FROM
(SELECT att1.intAttributeID as [TargetAttributeID]
FROM AST_tblAttributes att1
INNER JOIN
AST_lnkProfileDemandAttributes pda
ON pda.intAttributeID=att1.intAttributeID AND pda.intProfileID = @intProfileID
union all
SELECT ca2.intAttributeID as [TargetAttributeID] FROM
AST_lnkCapturePolicyAttributes ca2
INNER JOIN
AST_lnkEmployeeCapture ec2 ON ec2.intAdminCaptureID = ca2.intAdminCaptureID AND ec2.intTeamID = 57
WHERE ec2.dteCreatedDate >= @cutoffdate) x)
..それから1秒以内に戻ってきます。
SQL Serverは何を考えていますか?2つのサブクエリを実行し、その結果をのサブジェクトとして使用できることを理解できませんかIN
。相関サブクエリと同じくらい遅いようですが、相関していません!!!
Show Estimate Executionプランには、それぞれ100%のコストで3つのクラスター化インデックススキャンがあります。(実行計画はこちら)
DISTINCTS
内部がこのクエリを非常に遅くする理由を誰かに教えてもらえますか(ただし、 IN
...のサブジェクトとして使用された場合のみ)?
アップデート
申し訳ありませんが、これらの実行計画を立てるのに時間がかかりました...