Hello Professionals,
My code in CTE below is taking more than 5 min to return 500 records. Is there any better way to write this piece of code. Str before a column name indicates its a varchar field and int indicates integer field.
I need to insert this output to a fact table.Even after 15 minutes of execution, not even one record got inserted in to fact table. There are records that matches the filters below (its not like there are no records and so inserting zero records)
Thanks, in advance for your time.
USE Database01
GO
;WITH Apple as
(
Select A.DATE_TIME,A.StrID,A.StrUID,A.StrFLW,A.StrREAS,B.IntPID,D.intDID,isNULL (C.intCID,0) as intCID
FROM Database02.dbo.Table1 A WITH (NOLOCK)
JOIN DimTable2 D WITH (NOLOCK)
ON D.dtDate = cast(A.DATE_TIME as DATE )
LEFT JOIN DimTable3 C WITH (NOLOCK)
ON ( C.Struid = A.StrID OR C.Struid =A.Struid )
JOIN DimTable4 B WITH (NOLOCK)
ON A.StrPART = B.StrPart
GROUP BY A.DATE_TIME,A.StrID,A.StrUID,A.StrFLW,A.StrREAS,B.IntPID,D.intDID,isNULL (C.intCID,0) as intCID
)
Insert into FactTable(DATE_TIME,StrID,StrUID,StrFLW,StrREAS,IntPID,intDID,intCID)
Select A.DATE_TIME,A.StrID,A.StrUID,A.StrFLW,A.StrREAS,A.IntPID,A.intDID,A.intCID
FROM Apple A
LEFT OUTER JOIN FactTable F
ON A.DATE_TIME = F.DATE_TIME AND
A.StrVID = F.StrVID AND
Z.intCID = F.IntCID AND
Z.intDID = F.IntDID AND
Z.IntPID = F.IntPID
WHERE F.IntDID IS NULL