How to optimize this query in SQL Server, as it is consuming more time to execute
SELECT
A.RegTimePartnerID AS OrganiationID,
P.PartnerName AS OrganiationName,
PUIDCount AS IndividualsTrained_count,
EventCount AS TrainingActivities_count,
SalesCount AS Salestraining_Count,
TechCount AS Technicaltraining_count,
CASE WHEN OnDemandCount = 0 THEN 100
ELSE (Onlinecount*100/OnDemandCount)
END AS LivevsOndemand
FROM
dbo.Partner P
INNER JOIN
(SELECT RegTimePartnerID, COUNT(distinct UniqueID) AS PUIDCount
FROM dbo.DenormTrainingActivity(NOLOCK)
GROUP BY RegTimePartnerID) A
ON P.PartnerID = A.RegTimePartnerID AND P.PartnerID <> 0
INNER JOIN
(SELECT RegTimePartnerID, COUNT(distinct EventID) AS EventCount
FROM dbo.DenormTrainingActivity(NOLOCK)
GROUP BY RegTimePartnerID) B
ON A.RegTimePartnerID = B.RegTimePartnerID
INNER JOIN
(SELECT RegTimePartnerID, COUNT(distinct EventID) AS SalesCount
FROM dbo.DenormTrainingActivity(NOLOCK)
WHERE CourseTypeid = 10
GROUP BY RegTimePartnerID) C
ON A.RegTimePartnerID = C.RegTimePartnerID
INNER JOIN
(SELECT RegTimePartnerID, COUNT(distinct EventID) As TechCount
FROM dbo.DenormTrainingActivity(NOLOCK)
WHERE CourseTypeid = 20
GROUP BY RegTimePartnerID) D
ON A.RegTimePartnerID = D.RegTimePartnerID 开发者_开发百科
INNER JOIN
(SELECT RegTimePartnerID ,COUNT(distinct EventID) AS Onlinecount
FROM dbo.DenormTrainingActivity(NOLOCK)
WHERE EventCategory IN ('Live-ILT', 'Live-Online')
GROUP BY RegTimePartnerID) E
ON A.RegTimePartnerID = E.RegTimePartnerID
INNER JOIN
(SELECT RegTimePartnerID, COUNT(distinct EventID) AS OnDemandCount
FROM dbo.DenormTrainingActivity(NOLOCK)
WHERE EventCategory NOT IN ('Live-ILT', 'Live-Online')
GROUP BY RegTimePartnerID) F
ON A.RegTimePartnerID = F.RegTimePartnerID
What about something like (I'm sure there are typos):
SELECT
A.RegTimePartnerID AS OrganiationID,
P.PartnerName AS OrganiationName,
(
SELECT COUNT(DISTINCT dta.[UniqueID])
FROM dbo.[DenormTraningActivity](NOLOCK) AS dta
WHERE dta.[RegTimePartnerID] = P.[PartnerID]
) AS PUIDCount,
(
SELECT COUNT(DISTINCT dta.[EventID])
FROM dbo.[DenormTraningActivity](NOLOCK) AS dta
WHERE dta.[RegTimePartnerID] = P.[PartnerID]
) AS EventCount,
(
SELECT COUNT(DISTINCT dta.[EventID])
FROM dbo.[DenormTrainingActivity](NOLOCK) AS dta
WHERE dta.[CourseTypeid] = 10
AND dta.[RegTimePartnerId] = P.[PartnerID]
) AS SalesCount,
(
SELECT COUNT(DISTINCT dta.[EventID])
FROM dbo.[DenormTrainingActivity](NOLOCK) AS dta
WHERE dta.[CourseTypeid] = 20
AND dta.[RegTimePartnerID] = P.[PartnerID]
) AS TechCount,
CASE
WHEN (
SELECT COUNT(DISTINCT dta.[EventID])
FROM dbo.[DenormTrainingActivity](NOLOCK) AS dta
WHERE dta.[EventCategory] IN ('Live-ILT', 'Live-Online')
AND dta.[RegTimePartnerID] = P.[PartnerID]
) = 0 THEN 100
ELSE (
SELECT COUNT(DISTINCT dta.[EventID])
FROM dbo.[DenormTrainingActivity](NOLOCK) AS dta
WHERE dta.[EventCategory] IN ('Live-ILT', 'Live-Online')
AND dta.[RegTimePartnerID] = P.[PartnerID]
)*100/
(
SELECT COUNT(DISTINCT dta.[EventID])
FROM dbo.[DenormTrainingActivity](NOLOCK) AS dta
WHERE dta.[EventCategory] NOT IN ('Live-ILT', 'Live-Online')
AND dta.[RegTimePartnerID] = P.[PartnerID]
)
END AS LivevsOndemand
FROM
dbo.Partner P
WHERE P.[PartnerID] <> 0
I was thinking that maybe the nested subqueries would be less costly than the INNER JOIN/COUNT/GROUPBY. Probably not though since each subquery would have to be evaluated for each record in the Partner table.
精彩评论