troubleshooting Question

SELECT COUNT DISTINCT query very slow

Avatar of Xandolph75
Xandolph75Flag for Austria asked on
Microsoft SQL Server 2005Microsoft SQL Server 2008SQL
9 Comments1 Solution2396 ViewsLast Modified:
Hello!
In my MS-SQL 2005 Express database I have a table "Asphalt" and a number of tables with information for certain asphalttests. These tables (e.g. AV_Hohlraumgehalt,...) have a column "FID_asphalt" that references the ID of the asphalt that was tested. This column accepts NULL-values.
What I want to do is to get the number of all the tests for each asphalt.
The attached query worked very well, as long as the number of entries in the test-tables was small. Now I have between 500 and 1000 entries in each test-table, and the query takes ~45 seconds to complete.
Is there any way to speed up the query? It mustn't take longer then 1-2 seconds to get the result, which I use in a C# Win-Forms program.
Please help.
Thanks a lot!
Xandolph
SELECT	Asphalt.ID_asphalt,
		COUNT(DISTINCT AV_Hohlraumgehalt.ID_AV_hohlraumgehalt) AS AnzahlHohlraum, 
        COUNT(DISTINCT AV_Raumdichte.ID_AV_raumdichte) AS AnzahlRaumdichte,
		COUNT(DISTINCT AV_Triax.ID_AV_triax) AS AnzahlTriax, 
        COUNT(DISTINCT AV_UTST.ID_AV_UTST) AS AnzahlUTST,
		COUNT(DISTINCT AV_TSRST.ID_AV_TSRST) AS AnzahlTSRST, 
        COUNT(DISTINCT AV_Ermuedung4PBB.ID_AV_ermuedung_4PBB) AS AnzahlFAT4PBB, 
		COUNT(DISTINCT AV_Steifigkeit.ID_AV_steifigkeit) AS AnzahlSteifigkeit, 
		COUNT(DISTINCT AV_ErmuedungDTCPR.ID_AV_ermuedung_DTCPR) AS AnzahlFATDTCPR
FROM    Asphalt LEFT OUTER JOIN
                      AV_ErmuedungDTCPR ON Asphalt.ID_asphalt = AV_ErmuedungDTCPR.FID_asphalt LEFT OUTER JOIN
                      AV_Steifigkeit ON Asphalt.ID_asphalt = AV_Steifigkeit.FID_asphalt LEFT OUTER JOIN
                      AV_Ermuedung4PBB ON Asphalt.ID_asphalt = AV_Ermuedung4PBB.FID_asphalt LEFT OUTER JOIN
                      AV_UTST ON Asphalt.ID_asphalt = AV_UTST.FID_asphalt LEFT OUTER JOIN
                      AV_TSRST ON Asphalt.ID_asphalt = AV_TSRST.FID_asphalt LEFT OUTER JOIN
                      AV_Triax ON Asphalt.ID_asphalt = AV_Triax.FID_asphalt LEFT OUTER JOIN
                      AV_Raumdichte ON Asphalt.ID_asphalt = AV_Raumdichte.FID_asphalt LEFT OUTER JOIN
                      AV_Hohlraumgehalt ON Asphalt.ID_asphalt = AV_Hohlraumgehalt.FID_asphalt
GROUP BY Asphalt.ID_asphalt
ORDER BY Asphalt.ID_asphalt

Open in new window

ASKER CERTIFIED SOLUTION
Pavel Celba
All in One

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 9 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 9 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros