Davesm
asked on
TSQL Optimisation of code in SQL 2008
Hi Does anyone know of software or of sites that teach u to optimise code to get best performances either CTE's or optimising the From Clause etc
Is there anyone at expert exchange that could show or go through each step
Is there anyone at expert exchange that could show or go through each step
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You could do all of your aggregation up front and put that in a temp table. Then pivot the temp table to make the join easier. I'm not sure if this will be faster or not. I don't have any way to test it. Please let me know how it works out.
Greg
Greg
CREATE TABLE #ApplicationStatus(Application_ID INTEGER, ApplicationStatusChange_Date DATETIME, ApplicationStatusChange VARCHAR(10))
INSERT INTO #ApplicationStatus
SELECT
Application_ID,
MIN(ApplicationstatusChange_Date),
CASE ApplicationStatusChange_New_Status
WHEN ApplicationStatusChange_New_Status = 2 THEN 'CAPSTART'
WHEN ApplicationStatusChange_New_Status = 3 THEN 'CAPCOMP'
WHEN ApplicationStatusChange_New_Status = 17 THEN 'VALCOMP'
WHEN ApplicationStatusChange_New_Status IN(24,26,124) THEN 'AIP'
WHEN ApplicationStatusChange_New_Status = 36 THEN 'VALREQ'
WHEN ApplicationStatusChange_New_Status = 37 THEN 'VALRECD'
WHEN ApplicationStatusChange_New_Status IN (47,48,117,125) THEN 'FINDECS'
WHEN ApplicationStatusChange_New_Status = 127 THEN 'CUSTACCFINDECS'
WHEN ApplicationStatusChange_New_Status IN(301,54) THEN 'ElECTINST'
WHEN ApplicationStatusChange_New_Status IN(114,316) THEN 'BONDREG'
END
FROM
YourTable
GROUP BY
CASE ApplicationStatusChange_New_Status
WHEN ApplicationStatusChange_New_Status = 2 THEN 'CAPSTART'
WHEN ApplicationStatusChange_New_Status = 3 THEN 'CAPCOMP'
WHEN ApplicationStatusChange_New_Status = 17 THEN 'VALCOMP'
WHEN ApplicationStatusChange_New_Status IN(24,26,124) THEN 'AIP'
WHEN ApplicationStatusChange_New_Status = 36 THEN 'VALREQ'
WHEN ApplicationStatusChange_New_Status = 37 THEN 'VALRECD'
WHEN ApplicationStatusChange_New_Status IN (47,48,117,125) THEN 'FINDECS'
WHEN ApplicationStatusChange_New_Status = 127 THEN 'CUSTACCFINDECS'
WHEN ApplicationStatusChange_New_Status IN(301,54) THEN 'ElECTINST'
WHEN ApplicationStatusChange_New_Status IN(114,316) THEN 'BONDREG'
END
INSERT INTO #ApplicationStatus
SELECT
Application_ID,
MIN(Payout_EntryDate),
'PAYOUT'
FROM
YourTable
GROUP BY
Application_ID
SELECT
WorkHours_CAPSTART_CAPCOMP = mis.dbo.mis_Workhours((b.CAPSTART, b.CAPCOMP)
,WorkHours_CAPCOMP_VALCOMP = mis.dbo.mis_Workhours(b.CAPCOMP, b.VALCOMP)
,WorkHours_VALCOMP_AIP = mis.dbo.mis_Workhours(b.VALCOMP, b.AIP)
,WorkHours_AIP_VALREQ = mis.dbo.mis_Workhours(b.AIP, b.VALREQ)
,WorkHours_VALREQ_VALRECD = mis.dbo.mis_Workhours(b.VALREQ, b.VALRECD)
,WorkHours_VALRECD_FINDECS = mis.dbo.mis_Workhours(b.VALRECD, b.FINDECS)
,WorkHours_FINDESC_CUSTACCFINDECS = mis.dbo.mis_Workhours(b.FINDESC, b.CUSTACCFINDECS)
,WorkHours_CUSTACCFINDECS_ElECTINST = mis.dbo.mis_Workhours(b.CUSTACCFINDECS, b.ElECTINST)
,WorkHours_ELECINST_BONDREG = mis.dbo.mis_Workhours(b.ElECTINST, b.BONDREG)
,WorkHours_BONDREG_PAYOUT = mis.dbo.mis_Workhours(b.BONDREG, b.PAYOUT)
FROM
YourTable a INNER JOIN
(
SELECT Application_ID, [CAPSTART],[CAPCOMP],[VALCOMP],[AIP],[VALREQ],[VALRECD],[FINDECS], [CUSTACCFINDECS], [ElECTINST], [BONDREG], [PAYOUT]
FROM #ApplicationStatus
PIVOT (MIN (ApplicationstatusChange_Date)
FOR ApplicationStatusChange
IN ([CAPSTART],[CAPCOMP],[VALCOMP],[AIP],[VALREQ],[VALRECD],[FINDECS], [CUSTACCFINDECS], [ElECTINST], [BONDREG], [PAYOUT])) AS [Status]
) b ON a.Application_ID = b.Application_ID
ASKER
Hi I cant get this to run
Please can you walk me throught this query i can get the #ApplicationStatus
part to work but when i try run the second part it does not work
Please can you walk me throught this query i can get the #ApplicationStatus
part to work but when i try run the second part it does not work
ASKER
The attached is code i am using to get workdays betwen statuses. This code is slowing down the query badly is the another way i can code this or prehaps put it in a temp table.
Please can you give me some solutions
Open in new window