Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Really surprising to me... The optimizer takes advantage of parallelism to make the subqueries very efficient. I populated a dummy table with 120K records and tried the below. The third query is only slightly more efficient than the first (yours), but at the cost of more complexity. I would leave yours as-is. There probably is a better solution, but yours looks good enough to me. How long does yours take to process the 80K rows?</p> <pre><code>WITH PCapps2 AS ( SELECT DISTINCT Computer, Sequence, Site, SoftwareName, OS FROM pcapps ) SELECT DISTINCT SoftwareName, Sequence, Site, (SELECT COUNT(p1.SoftwareName) FROM PCapps2 p1 WHERE p1.SoftwareName = pc.Softwarename AND OS = 'Windows 7 Enterprise' AND p1.Site = pc.Site) as 'Win7/site', (SELECT COUNT(p1.SoftwareName) FROM PCapps2 p1 WHERE p1.SoftwareName = pc.Softwarename AND OS = 'Windows 7 Enterprise' AND p1.Sequence = pc.Sequence) as 'Win7Installs/seq', (SELECT COUNT(p2.SoftwareName) FROM PCapps2 p2 WHERE p2.SoftwareName = pc.Softwarename AND OS = 'Windows 7 Enterprise') as TotWin7apps, (SELECT COUNT(p3.SoftwareName) FROM PCapps2 p3 WHERE p3.SoftwareName = pc.SoftwareName) as TotalInstalls FROM PCapps2 pc ;WITH PCapps2 AS ( SELECT DISTINCT Computer, Sequence, Site, SoftwareName, OS FROM pcapps ) SELECT DISTINCT SoftwareName, Sequence, Site, COUNT(case when os = 'Windows 7 Enterprise' then 1 end) over (partition by pc.Softwarename,pc.Site) as 'Win7/site', COUNT(case when os = 'Windows 7 Enterprise' then 1 end) over (partition by pc.Softwarename,pc.Sequence) as 'Win7Installs/seq', COUNT(case when os = 'Windows 7 Enterprise' then 1 end) over (partition by pc.Softwarename) as TotWin7apps, COUNT(*) over (partition by pc.Softwarename) as TotalInstalls FROM PCapps2 pc ;WITH PCapps2 AS ( SELECT DISTINCT Computer, Sequence, Site, SoftwareName, OS FROM pcapps ), a as ( select softwarename, site, COUNT(*) as 'Win7/site' from pcapps2 where os = 'Windows 7 Enterprise' group by softwarename, site ), b as ( select softwarename, sequence, COUNT(*) as 'Win7Installs/seq' from pcapps2 where os = 'Windows 7 Enterprise' group by softwarename, sequence ), c as ( select softwarename, COUNT(case when os = 'Windows 7 Enterprise' then 1 end) as TotWin7apps, COUNT(*) as TotalInstalls from pcapps2 group by softwarename ), d as ( select distinct softwarename, sequence, site from pcapps2 ) select d.*, isnull(a.[Win7/site], 0) as [Win7/site], isnull(b.[Win7Installs/seq],0) as [Win7Installs/seq] , isnull(c.TotWin7apps,0) as TotWin7apps, isnull(c.TotalInstalls, 0) as TotalInstalls from d left join a on d.softwarename = a.softwarename and d.site = a.site left join b on d.softwarename = b.softwarename and d.sequence = b.sequence left join c on d.softwarename = c.softwarename </code></pre> <p>First query: Table 'pcapps'. Scan count 15, logical reads 6630, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.</p> <p>Second query: Table 'pcapps'. Scan count 3, logical reads 1326, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. Table 'Worktable'. Scan count 18, logical reads 1983591, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.</p> <p>Third query: Table 'pcapps'. Scan count 12, logical reads 5304, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload