SQL Version: 2008 R2
Problem: I have created a View (udvEEOReportSummary) using a UNION ALL to join the records from two Views. View #1 udvEEOMcHughHours runs in about 25 seconds if ran by itself. View #2 udvEEOSubHours_UNPIVOT runs in about 1 second by itself. I run the Select statements located in udvEEOReportSummary relating to both View and they run separately in the same approximate time as it takes to run the related view. Now, when I run both Select Statements included the UNION ALL together or run the entire udvEEOReportSummary view it takes over 30 minutes.
I just don't understand what is happening. Any help would be appreciated. My complete code is below.
Regards,
...bob
Related Code:
ALTER VIEW [dbo].[udvEEOReportSummary]
AS
SELECT
-- GROUPING FIELDS
McHughCo
, MCUP.McHughCoName
, MCUP.McHughCoNum_CoName
--Speicific only to udEEOTracking
, FirstTierSubNum = NULL
, FirstTierSubName = 'N/A'
, SubKID = 'N/A'
, CompCode = 'N/A'
, CompCodeDesc = 'N/A'
--Speicific only to udEEOTracking
, MCUP.EmployeeNum
, MCUP.EmployeeName
, MCUP.EmployeeActiveYN
, MCUP.RptYear
, MCUP.RptYearMonth
, MCUP.RptDate
, MCUP.MostCurrentReportDate
, MCUP.RptCoID
, MCUP.RptCompanyName
, RptCompanyType = 'McHugh'
-- Gender is determined after UNPIVOT
-- Race Code info must be entered after UNPIVOT
, MCUP.JobNum
, MCUP.JobStatus
, MCUP.JobDeptNum
, MCUP.JobDeptDesc
, MCUP.JobNum_JobName
, MCUP.PMRoleName
, MCUP.AdminRoleName
, MCUP.CPCRoleName
, MCUP.PMNameOLD
, RTRIM(ISNULL(CAST(MCUP.JobZIP as NCHAR(12)),'') + ' ' + ISNULL(MCUP.JobLocalCode,'') + ' ' + ISNULL(MCUP.JobLocalCodeDesc,''))AS ResidencyCode
, Source = 'McHugh Payroll'
, CASE WHEN MCUP.RptHoursCat = 'HoursEEO' THEN 'EEO' ELSE '' END
+ CASE WHEN MCUP.RptHoursCat = 'HoursAppren' THEN 'App-Train' ELSE '' END
+ CASE WHEN MCUP.RptHoursCat = 'HoursResidLC' OR MCUP.RptHoursCat = 'HoursResidZIP' THEN 'Residency' ELSE '' END
AS RptCat
, CASE WHEN MCUP.RptHoursCat = 'HoursEEO' THEN (SELECT TOP 1 RptSubCat FROM budEEOReport_Matrix WHERE MCUP.RaceCode = RptSubCat) ELSE '' END
+ CASE WHEN MCUP.RptHoursCat = 'HoursAppren' THEN 'AP' ELSE '' END
+ CASE WHEN MCUP.RptHoursCat = 'HoursResidLC' THEN MCUP.JobLocalCode ELSE '' END
+ CASE WHEN MCUP.RptHoursCat = 'HoursResidZIP' THEN MCUP.JobZIP ELSE '' END
AS RptSubCat
, CASE WHEN MCUP.RptHoursCat = 'HoursEEO' THEN (SELECT TOP 1 RptSubCatDesc FROM budEEOReport_Matrix WHERE MCUP.RaceCode = RptSubCat) ELSE '' END
+ CASE WHEN MCUP.RptHoursCat = 'HoursAppren' THEN 'Apprentice' ELSE '' END
+ CASE WHEN MCUP.RptHoursCat = 'HoursResidLC' THEN MCUP.JobLocalCodeDesc ELSE '' END
+ CASE WHEN MCUP.RptHoursCat = 'HoursResidZIP' THEN MCUP.JobZIP ELSE '' END
AS RptSubCatDesc
, CASE WHEN MCUP.RptHoursCat = 'HoursEEO' THEN (SELECT TOP 1 EEOCategoryDesc FROM budEEOReport_Matrix WHERE MCUP.RaceCode = RptSubCat) ELSE '' END
+ CASE WHEN MCUP.RptHoursCat = 'HoursAppren' THEN 'AP' ELSE '' END
+ CASE WHEN MCUP.RptHoursCat = 'HoursResidLC' THEN MCUP.JobLocalCodeDesc ELSE '' END
+ CASE WHEN MCUP.RptHoursCat = 'HoursResidZIP' THEN MCUP.JobZIP ELSE '' END
AS RptEEOCatDesc
, CASE WHEN MCUP.RptHoursCat = 'HoursEEO' THEN (SELECT TOP 1 VPRaceCode FROM budEEOReport_Matrix WHERE MCUP.RaceCode = RptSubCat) ELSE '' END
+ CASE WHEN MCUP.RptHoursCat = 'HoursAppren' THEN 'N/A' ELSE '' END
+ CASE WHEN MCUP.RptHoursCat = 'HoursResidLC' THEN 'N/A' ELSE '' END
+ CASE WHEN MCUP.RptHoursCat = 'HoursResidZIP' THEN 'N/A' ELSE '' END
AS VPRaceCode
, MCUP.GenderFM
, CASE WHEN MCUP.GenderFM = 'M' THEN 'Male' ELSE 'Female' END AS Gender
-- PLACEHOLDER FIELD FOR COLUMN VALUES
, MCUP.RptHoursCat
-- AGGREGATED FIELD
, MCUP.Hours
FROM udvEEOMcHughHours_UNPIVOT AS MCUP
UNION ALL
SELECT
SubUP.McHughCo
, SubUP.McHughCoName
, SubUP.McHughCoNum_CoName
--Speicific only to udEEOTracking
, SubUP.FirstTierSubNum
, SubUP.FirstTierSubName
, SubUP.SubKID
, SubUP.CompCode
, SubUP.CompCodeDesc
--Specific only to udvEEOMcHughPRHours
, EmployeeNum = NULL
, EmployeeName = 'N/A'
, EmployeeActiveYN = 'N/A'
, SubUP.RptYear
, SubUP.RptYearMonth
, SubUP.RptDate
, SubUP.MostCurrentReportDate
, SubUP.RptCoID
, SubUP.RptCompanyName
, SubUP.RptCompanyType
-- Gender is determined after UNPIVOT
-- Race Code info must be entered after UNPIVOT
, SubUP.JobNum
, SubUP.JobStatus
, SubUP.JobDeptNum
, SubUP.JobDeptDesc
, SubUP.JobNum_JobName
, SubUP.PMRoleName
, SubUP.AdminRoleName
, SubUP.CPCRoleName
, SubUP.PMNameOLD
, LTRIM(ISNULL(PRLI.Description,'') + ' Sub ZIP Reporting' ) AS ResidencyCode
, Source = 'EEO Sub Tracking'
-- ADD EEO & UNPIVOTED REPORTING CATEGORIES:
, (SELECT TOP 1 RptCat FROM budEEOReport_Matrix AS Matrix WHERE SubUP.EEOGroup = Matrix.RptItemDesc)
AS RptCat
, (SELECT TOP 1 RptSubCat FROM budEEOReport_Matrix AS Matrix WHERE SubUP.EEOGroup = Matrix.RptItemDesc)
AS RptSubCat
, CASE WHEN SubUP.EEOGroup = 'CityHours' AND SubUP.JobLocalCode IS NOT NULL THEN 'Residency-Local Code'
WHEN SubUP.EEOGroup = 'CityHours' AND SubUP.JobZIPYN IS NOT NULL THEN 'Residency-ZIP'
ELSE '' END
AS RptSubCatDesc
, CASE WHEN SubUP.EEOGroup <> 'CityHours' THEN (SELECT TOP 1 RptSubCatDesc FROM budEEOReport_Matrix AS Matrix WHERE SubUP.EEOGroup = Matrix.RptItemDesc)
WHEN SubUP.EEOGroup = 'CityHours' AND SubUP.JobLocalCode IS NOT NULL THEN SubUP.JobLocalCode
WHEN SubUP.EEOGroup = 'CityHours' AND SubUP.JobZIPYN IS NOT NULL THEN 'Sub ZIP Reporting'
ELSE '' END
AS RptEEOCatDesc
, (SELECT TOP 1 VPRaceCode FROM budEEOReport_Matrix AS Matrix WHERE SubUP.EEOGroup = Matrix.RptItemDesc)
AS VPRaceCode
, (SELECT TOP 1 GenderMF FROM budEEOReport_Matrix AS Matrix WHERE SubUP.EEOGroup = Matrix.RptItemDesc)
AS GenderFM
, (SELECT TOP 1 Gender FROM budEEOReport_Matrix AS Matrix WHERE SubUP.EEOGroup = Matrix.RptItemDesc)
AS Gender
, RptHoursCat = 'SubHours'
, SubUP.Hours
FROM udvEEOSubHours_UNPIVOT AS SubUP
LEFT JOIN PRLI
ON SubUP.McHughCo = PRLI.PRCo
AND SubUP.JobLocalCode = PRLI.LocalCode
GO
Bob Sutor