[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

TSQL Optimisation of code in SQL 2008

Posted on 2011-10-13
4
Medium Priority
?
182 Views
Last Modified: 2013-03-05
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  
0
Comment
Question by:Davesm
  • 2
  • 2
4 Comments
 
LVL 21

Accepted Solution

by:
JestersGrind earned 2000 total points
ID: 36969386
Here is an article from Microsoft about query tuning.  

http://msdn.microsoft.com/en-us/library/ms176005.aspx

If you have specific examples or questions, you can ask them here and experts will be able to help you more.

Greg

0
 

Author Comment

by:Davesm
ID: 36981217
Hi greg

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


,WorkHours_CAPSTART_CAPCOMP = mis.dbo.mis_Workhours((select min(a1.ApplicationstatusChange_Date) 
                                         from nas..ApplicationstatusChange a1 with(nolock) 
                                         where a1.ApplicationStatusChange_New_Status in (2) and a1.application_id = a.application_id),
                                         (select min(a1.ApplicationstatusChange_Date) 
                                         from nas..ApplicationstatusChange a1 with(nolock) 
                                         where a1.ApplicationStatusChange_New_Status in (3) and a1.application_id = a.application_id)) 
                                         
       ,WorkHours_CAPCOMP_VALCOMP = mis.dbo.mis_Workhours((select min(a1.ApplicationstatusChange_Date) 
                                         from nas..ApplicationstatusChange a1 with(nolock) 
                                         where a1.ApplicationStatusChange_New_Status in (3) and a1.application_id = a.application_id),
                                         (select min(a1.ApplicationstatusChange_Date) 
                                         from nas..ApplicationstatusChange a1 with(nolock) 
                                         where a1.ApplicationStatusChange_New_Status in (17) and a1.application_id = a.application_id)) 
                                         
       ,WorkHours_VALCOMP_AIP = mis.dbo.mis_Workhours((select min(a1.ApplicationstatusChange_Date) 
                                         from nas..ApplicationstatusChange a1 with(nolock) 
                                         where a1.ApplicationStatusChange_New_Status in (17) and a1.application_id = a.application_id),
                                         (select min(a1.ApplicationstatusChange_Date) 
                                         from nas..ApplicationstatusChange a1 with(nolock) 
                                         where a1.ApplicationStatusChange_New_Status in (24,26,124) and a1.application_id = a.application_id))
     
      ,WorkHours_AIP_VALREQ = mis.dbo.mis_Workhours((select min(a1.ApplicationstatusChange_Date) 
                                         from nas..ApplicationstatusChange a1 with(nolock) 
                                         where a1.ApplicationStatusChange_New_Status in (24,26,124) and a1.application_id = a.application_id),
                                         (select min(a1.ApplicationstatusChange_Date) 
                                         from nas..ApplicationstatusChange a1 with(nolock) 
                                         where a1.ApplicationStatusChange_New_Status in (36) and a1.application_id = a.application_id))                                 
                                         
       ,WorkHours_VALREQ_VALRECD = mis.dbo.mis_Workhours((select min(a1.ApplicationstatusChange_Date) 
                                         from nas..ApplicationstatusChange a1 with(nolock) 
                                         where a1.ApplicationStatusChange_New_Status in (36) and a1.application_id = a.application_id),
                                         (select min(a1.ApplicationstatusChange_Date) 
                                         from nas..ApplicationstatusChange a1 with(nolock) 
                                         where a1.ApplicationStatusChange_New_Status in (37) and a1.application_id = a.application_id))
        
        ,WorkHours_VALRECD_FINDECS = mis.dbo.mis_Workhours((select min(a1.ApplicationstatusChange_Date) 
                                         from nas..ApplicationstatusChange a1 with(nolock) 
                                         where a1.ApplicationStatusChange_New_Status in (37) and a1.application_id = a.application_id),
                                         (select min(a1.ApplicationstatusChange_Date) 
                                         from nas..ApplicationstatusChange a1 with(nolock) 
                                         where a1.ApplicationStatusChange_New_Status in (47,48,117,125) and a1.application_id = a.application_id)) 
                                         
       ,WorkHours_FINDESC_CUSTACCFINDECS = mis.dbo.mis_Workhours((select min(a1.ApplicationstatusChange_Date) 
                                         from nas..ApplicationstatusChange a1 with(nolock) 
                                         where a1.ApplicationStatusChange_New_Status in (47,48,117,125) and a1.application_id = a.application_id),
                                         (select min(a1.ApplicationstatusChange_Date) 
                                         from nas..ApplicationstatusChange a1 with(nolock) 
                                         where a1.ApplicationStatusChange_New_Status in (127) and a1.application_id = a.application_id))
                                         
       ,WorkHours_CUSTACCFINDECS_ElECTINST = mis.dbo.mis_Workhours((select min(a1.ApplicationstatusChange_Date) 
                                         from nas..ApplicationstatusChange a1 with(nolock) 
                                         where a1.ApplicationStatusChange_New_Status in (127) and a1.application_id = a.application_id),
                                         (select min(a1.ApplicationstatusChange_Date) 
                                         from nas..ApplicationstatusChange a1 with(nolock) 
                                         where a1.ApplicationStatusChange_New_Status in (301,54) and a1.application_id = a.application_id))                                  
                                                               
       ,WorkHours_ELECINST_BONDREG = mis.dbo.mis_Workhours((select min(a1.ApplicationstatusChange_Date) 
                                         from nas..ApplicationstatusChange a1 with(nolock) 
                                         where a1.ApplicationStatusChange_New_Status in (301,54) and a1.application_id = a.application_id),
                                         (select min(a1.ApplicationstatusChange_Date) 
                                         from nas..ApplicationstatusChange a1 with(nolock) 
                                         where a1.ApplicationStatusChange_New_Status in (114,316) and a1.application_id = a.application_id))
       
       ,WorkHours_BONDREG_PAYOUT = mis.dbo.mis_Workhours((select min(a1.ApplicationstatusChange_Date) 
                                         from nas..ApplicationstatusChange a1 with(nolock) 
                                         where a1.ApplicationStatusChange_New_Status in (114,316) and a1.application_id = a.application_id),
                                         (select min(Payout_EntryDate) 
                                         from nas..Payout p with(nolock) 
                                         where p.application_id = a.application_id))

Open in new window

0
 
LVL 21

Expert Comment

by:JestersGrind
ID: 36982116
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


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

Open in new window

0
 

Author Comment

by:Davesm
ID: 37007277
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
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question