テーブルからの最初のクエリには約 40 秒かかり、80,000 を超える行が作成されます。サイト、シーケンス、合計、および任意の OS バージョンごとに Windows 7 アプリケーションの数を取得したいと考えています。
これらのサブクエリは機能しますが、もちろんプロセスが大幅に遅くなります。実行に 3.5 時間かかりました。
これを行うより効率的な方法はありますか?
出力:
SoftwareName Sequence Site Win7/site Win7Installs/seq TotWin7apps TotalInstalls
Adobe Acrobat 1 BKN 1 5 626 7854
AutoCAD LT 1 BKN 1 1 3 15
Adobe Acrobat 1 CTW 4 5 626 7854
Adobe Captivate 1 CTW 1 1 8 60
クエリ:
WITH PCapps AS (
SELECT DISTINCT
Computer,
Sequence,
Site,
SoftwareName,
OS
FROM table
)
SELECT DISTINCT
SoftwareName,
Sequence,
Site,
(SELECT COUNT(p1.SoftwareName) FROM PCapps p1 WHERE p1.SoftwareName = pc.Softwarename AND OS = 'Windows 7 Enterprise' AND p1.Site = pc.Site) as 'Win7/site',
(SELECT COUNT(p1.SoftwareName) FROM PCapps p1 WHERE p1.SoftwareName = pc.Softwarename AND OS = 'Windows 7 Enterprise' AND p1.Sequence = pc.Sequence) as 'Win7Installs/seq',
(SELECT COUNT(p2.SoftwareName) FROM PCapps p2 WHERE p2.SoftwareName = pc.Softwarename AND OS = 'Windows 7 Enterprise') as TotWin7apps,
(SELECT COUNT(p3.SoftwareName) FROM PCapps p3 WHERE p3.SoftwareName = pc.SoftwareName) as TotalInstalls
FROM PCapps pc
** アップデート:
@Jason Carter のヒントを使用して、いくつかの #temp テーブルを作成し、それらを結合しました。結果は 1 分未満で非常に高速に実行されます (最初のクエリよりも長くはありません)。#temp テーブルを作成するための最初のヒントに従っていたため、この方法は @JasonCarter のソリューションとは少し異なります。それぞれが COUNT() を含むいくつかの #temp テーブルを作成しました。
SELECT DISTINCT
Computer,
Sequence,
Site,
SoftwareName,
OS
INTO #PCapps
FROM TABLE
SELECT
SoftwareName,
Site,
COUNT(SoftwareName) AS [SiteInstalls]
INTO #SiteInstalls
FROM #PCapps
WHERE OS = 'Windows 7 Enterprise'
GROUP BY Site, SoftwareName
SELECT
SoftwareName,
Sequence,
COUNT(SoftwareName) AS [SeqInstalls]
INTO #SeqInstalls
FROM #PCapps
WHERE OS = 'Windows 7 Enterprise'
GROUP BY Sequence, SoftwareName
SELECT
SoftwareName,
COUNT(SoftwareName) AS [Win7Installs]
INTO #Win7Installs
FROM #PCapps
WHERE OS = 'Windows 7 Enterprise'
GROUP BY SoftwareName
SELECT
SoftwareName,
COUNT(SoftwareName) AS [AppInstalls]
INTO #AppInstalls
FROM #PCapps
GROUP BY SoftwareName
SELECT
pc.SoftwareName,
pc.Sequence,
pc.Site,
sit7.SiteInstalls,
seq7.SeqInstalls,
w7.Win7Installs,
ai.AppInstalls
FROM #PCapps pc
LEFT OUTER JOIN #SiteInstalls sit7 ON sit7.SoftwareName = pc.SoftwareName AND sit7.Site = pc.Site
LEFT OUTER JOIN #SeqInstalls seq7 ON seq7.SoftwareName = pc.SoftwareName AND seq7.Sequence = pc.Sequence
LEFT OUTER JOIN #Win7Installs w7 ON w7.SoftwareName = pc.SoftwareName
LEFT OUTER JOIN #AppInstalls ai ON ai.Softwarename = pc.SoftwareName
DROP TABLE #PCapps
DROP TABLE #SiteInstalls
DROP TABLE #SeqInstalls
DROP TABLE #Win7Installs
DROP TABLE #AppInstalls