Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

query performance to increase

$
0
0

hi i'm using cte's as defined and took a temp table to take the columns from temp table.now,when i'm executing this logic of sp..its taking 7min for 3lakhs records. can any one pls help me how to improve my query performance..

the code which i'm using is:

create temtable as

(

cols

);

WITH Datematrix(AllocationDate)/*cte*/ As ( SELECT @StartDate AS AllocationDate UNION ALL SELECT DATEADD(D,1,AllocationDate) AS AllocationDate FROM Datematrix WHERE AllocationDate<@EndDate ) , /*cte*/

Allocation (Division,DivisionID,ResourceName,ResourceEmailID,ResourceID,Project ,ProjectID,Scope,ScopeID,WorkItem,TaskStartDate,TaskEndDate ,ProgramID ,Program,PortfolioID ,Portfolio,StatusID,Status,TaskID,EstimateHrs,ScopeEstimateHrs) AS ( SELECT DIV.Division ,RES.DivisionID ,RES.ResourceName ,ResourceEmailID = STUFF(( SELECT COALESCE( ', ' + CONVERT(VARCHAR,RES.Email1), '') FROM dbo.TasksResource TSKRES WITH(NOLOCK) LEFT OUTER JOIN dbo.tb_Resource RES WITH(NOLOCK) ON RES.UID = TSKRES.ResourceID WHERE TSKRES.TaskID = TSK.TaskID FOR XML PATH('')), 1, 1, '') ,RES.UID ResourceID ,PRJ.Project + ' (' + CONVERT(VARCHAR(15),PRJ.StartDate,101) +' - ' + CONVERT(VARCHAR(15),PRJ.EndDate,101) + ')' as Project ,PRJ.UID ProjectID ,SCP.Title Scope ,SCP.ScopeID ,TSK.Title WorkItem ,TSK.StartDate TaskStartDate ,TSK.EndDate TaskEndDate ,PRJ.ProgramID ,PR.Program ,PR.PortfolioID ,PF.Portfolio ,TSK.StatusID ,ST.Status ,TSK.TaskID ,TSK.EstimateHrs ,(isnull(SCP.EstimateARCH,0) + isnull(SCP.EstimateBA,0) + isnull(SCP.EstimateDev,0) + isnull(SCP.EstimatePM,0) + isnull(SCP.EstimateQA,0) + isnull(SCP.EstimateRM,0)) as ScopeEstimateHrs --SCP.EstimateARCH + SCP.EstimateBA +SCP.EstimateDev +SCP.EstimatePM +SCP.EstimateQA +SCP.EstimateRM as ScopeEstimateHrs FROM Tasks TSK WITH(NOLOCK) INNER JOIN dbo.Scope SCP WITH(NOLOCK) ON TSK.ScopeID = SCP.ScopeID INNER JOIN dbo.tb_Project PRJ WITH(NOLOCK)ON TSK.ProjectID = PRJ.UID INNER JOIN dbo.tb_Program PR WITH(NOLOCK) ON PR.UID=PRJ.ProgramID INNER JOIN dbo.tb_Portfolio PF WITH(NOLOCK)ON PF.UID=PR.PortfolioID LEFT OUTER JOIN dbo.TasksResource TSKRES WITH(NOLOCK)ON TSKRES.TaskID = TSK.TaskID LEFT OUTER JOIN dbo.tb_Resource RES WITH(NOLOCK) ON RES.UID = TSKRES.ResourceID LEFT JOIN dbo.tb_Division DIV WITH(NOLOCK) ON RES.DivisionID = DIV.UID LEFT JOIN dbo.tb_Status ST WITH(NOLOCK) ON TSK.StatusID=ST.UID /*relating with the high level work items */ WHERE (PRJ.UID = @Project OR @Project = -1) AND (PRJ.ProgramID = @Program OR @Program = -1) AND (PRJ.PortfolioID =@Portfolio OR @Portfolio = -1) ) ,/*columns used in 2 cte's are taken in below maindata*/ MainData (AllocationDate,Division,DivisionID,ResourceName,ResourceEmailID,ResourceID,Project,ProjectID ,Scope,ScopeID,WorkItem,TaskStartDate,TaskEndDate ,ProgramID ,Program,PortfolioID ,Portfolio,StatusID,Status,TaskID,EstimateHrs,ScopeEstimateHrs,Allocated) AS ( SELECT Datematrix.* ,Allocation.* ,CASE WHEN ISDATE(TaskStartDate)=1 THEN 1 ELSE 0 END AS Allocated FROM Datematrix FULL OUTER JOIN Allocation ON ( Datematrix.AllocationDate>= Allocation.TaskStartDate AND Datematrix.AllocationDate<=Allocation.TaskEndDate )

INSERT INTO #TempTable SELECT * FROM MainData OPTION (MAXRECURSION 0);

this way the code goes...

please help.. i need my query to be tuned..!!

thanks in advance..





lucky


Viewing all articles
Browse latest Browse all 23857

Trending Articles