We help IT Professionals succeed at work.
Get Started

SELECT COUNT DISTINCT query very slow

2,395 Views
Last Modified: 2021-04-21
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

Comment
Watch Question
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 9 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE