Optimizing stored procedure

Hi,

This is my stored procedure,

Is there any way to avoid temptable to improve performance

CREATE procedure [dbo].[sp_GetMALReportDetails]

(@CategoryID INT = null, @StatusID INT = null, @UserTypeId int = null,

@UserName varchar(100)= null, @assignedTo varchar(200)=null, @priorityID int =null,

@queueId int =null)

AS

BEGIN

select distinct

     a.ID as [ID], p.Name as [Priority], c.Name as [Category],  q.Name as [Queue], a.AssignedTo as [AssignedTo],

     b.Prefix, b.CI,

     a.Name as [AppName],s.Name as [AppStatus], CONVERT(varchar(255), null) as [PrimaryAppOwner],

     ac.Comment as [LastComment], ct.Name as [CommentType], CONVERT(varchar(255), null) as [DivManager]

into #Results_tbl

from Application a(nolock)

inner join Queue q (nolock)

     On a.QueueID = q.ID

inner join Category c (nolock)

     on c.ID = a.CategoryID

left join ApplicationExtended b

     on a.ID = b.ApplicationID

inner join Status s (nolock)

     on s.ID = a.StatusID

left join ApplicationDateType adt (nolock)

     on a.ID = adt.ApplicationID

left join DateType dt (nolock)

     on adt.DateTypeID = dt.ID

left join Priority p (nolock)

     on a.PriorityID = p.ID    

left join ApplicationComment ac(nolock)

     on ac.ApplicationID = a.ID

left join CommentType ct (nolock)

     on ac.CommentTypeID = ct.ID

where a.StatusID = coalesce(@StatusID, a.StatusID)

and a.CategoryID = coalesce(@CategoryID, a.CategoryID)

--and a.AssignedTo = coalesce(@assignedTo, a.AssignedTo)

and a.PriorityID = coalesce(@priorityID, a.PriorityID)

and a.QueueID = coalesce(@queueId, a.QueueID)

if (@assignedTo is not null)

begin

     delete from #Results_tbl where [AssignedTo] != @assignedTo or [AssignedTo] is null

end

-- updating App Owner

update r set r.[PrimaryAppOwner] = aut.UserName

from #Results_tbl r (nolock)

left join ApplicationUserType aut (nolock)

     on r.[ID] = aut.ApplicationID

left join UserType ut (nolock)

     on ut.ID = aut.UserTypeID

where ut.Name = 'Primary App Owner'

-- updating Div Manager

update r set r.[DivManager] = aut.UserName

from #Results_tbl r (nolock)

left join ApplicationUserType aut (nolock)

     on r.[ID] = aut.ApplicationID

left join UserType ut (nolock)

     on ut.ID = aut.UserTypeID

where ut.Name = 'Div Manager'

if (@UserTypeId is not null)

begin

     if(@UserTypeId = 1)

     begin

           select *

           from #Results_tbl

           where [PrimaryAppOwner] = coalesce(@UserName,[PrimaryAppOwner])

           return

     end

     else if(@UserTypeId = 4)

     begin

           select *

           from #Results_tbl

           where [DivManager] = coalesce(@UserName,[DivManager])

           return

     end

end

select * from #Results_tbl

END

G


March 5th, 2014 9:45am

without knowing your exact requirement we cant suggest much. Can you put some sample data for us and explain what you're trying to achieve with procedure above?
Free Windows Admin Tool Kit Click here and download it now
March 5th, 2014 10:02am

Why do you think temp table is causing a performance issue here? Please provide execution plan.

March 5th, 2014 10:05am

Hi,

 I am a new joinee, I didn't get any environment, Just my teamlead has given me this storedProcedure

How could you optimize

I don't know about tables and indexes

Free Windows Admin Tool Kit Click here and download it now
March 5th, 2014 10:13am

Please show us the execution plan of the query.

http://technet.microsoft.com/en-us/library/ms189562.aspx

March 5th, 2014 10:46am

1. create table "#Results_tbl" using CREATE TABLE command instead of using SELECT..INTO..FROM

2. insert data into "#Results_tbl"

3. create index(s) on "#Results_tbl"

Free Windows Admin Tool Kit Click here and download it now
March 5th, 2014 10:55am

Hi Koteswararao,

It is commended to ask the question relatd to Stored Procedures on the Transact-SQL fourm for dedicated support.

Thanks.

March 6th, 2014 9:27am

Duplicate thread.

See Optimizing the stored procedure.

Free Windows Admin Tool Kit Click here and download it now
March 6th, 2014 12:43pm

The following blog is on optimization:

Query and Stored Procedure Optimization

March 15th, 2014 2:08am

It's not the temptable that
lower the performance. The bad performance is caused by join so
many tables in the stored procedure. If these tables
are rarely update, you can create an indexed view(then all these
tables' data will be stored in your db, like real table), and then
get data from this view into you temptable in the stored procedure.

CREATE VIEW view_XXXXXXXXXXX WITH SCHEMABINDING AS 
BEGIN
select distinct

     a.ID as [ID], p.Name as [Priority], c.Name as [Category],   q.Name as [Queue], a.AssignedTo as [AssignedTo], 

     b.Prefix, b.CI,

     a.Name as [AppName],s.Name as [AppStatus], CONVERT(varchar(255), null) as [PrimaryAppOwner],

     ac.Comment as [LastComment], ct.Name as [CommentType], CONVERT(varchar(255), null) as [DivManager]


from Application a(nolock)

inner join Queue q (nolock)

     On a.QueueID = q.ID

inner join Category c (nolock)

     on c.ID = a.CategoryID

left join ApplicationExtended b

     on a.ID = b.ApplicationID

inner join Status s (nolock)

     on s.ID = a.StatusID

left join ApplicationDateType adt (nolock)

     on a.ID = adt.ApplicationID

left join DateType dt (nolock)

     on adt.DateTypeID = dt.ID

left join Priority p (nolock)

     on a.PriorityID = p.ID     

left join ApplicationComment ac(nolock)

     on ac.ApplicationID = a.ID

left join CommentType ct (nolock)

     on ac.CommentTypeID = ct.ID
END
GO
CREATE UNIQUE CLUSTERED INDEX  IDX_view_XXXXXXXXXXX
ON view_XXXXXXXXXXX (StatusID,CategoryID,PriorityID,QueueID)




Free Windows Admin Tool Kit Click here and download it now
March 15th, 2014 6:18am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics