Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 230
  • Last Modified:

Any way to improve the performance of SQL query with optional parameters ?


This query returns correct data, just appears to be a bit slower than hoped (about 0.9s in testing on development box)
I will be calling it up to 150 times in succession from my app, using different DocId's
The idea is to determine docrevision with highest priority, including testing a requested docrevision to see what it's priority would be.

ProjectId is alway available. usually only one of DocId and DocNo will be supplied.
I need the RevStyles as a LIKE operation, but am looking at removing the option for LIKE testing of DocId

Query is in a stored proc. Currently SQLS2000. 2005 is available.
declare @ProjectId varchar(8)
   declare @DocNo int
   declare @DocId varchar(50)
   declare @TestRev varchar(5)
   
   set @ProjectId = '07074'
   set @DocId = 'Q1251-ME-BWSE-1000'
   set @TestRev = 'D'
   
   SELECT  rtrim(Proj_DocRev.RevisionId) AS RevId, Proj_RevStyles.Priority as Priority
   FROM dbo.Proj_Documents  , dbo.Proj_DocRev , dbo.Proj_Codes , dbo.Proj_RevStyles 
 
   WHERE proj_documents.Projectno =  @ProjectId 
        AND ((@DocNo IS NULL) OR (Proj_Documents.DocNo = @DocNo))
        AND ((@DocId IS NULL) OR (Proj_Documents.DocId LIKE @DocId))
        AND Proj_Documents.DocNo = Proj_DocRev.DocNo
        AND Proj_Documents.ProjectNo = Proj_Codes.ProjectNo 
        AND Proj_Codes.RevisionStyle = Proj_Revstyles.Id
        AND Proj_DocRev.RevisionId LIKE Proj_RevStyles.Tag
	UNION 
		 SELECT rtrim(@TestRev) as RevId, Proj_RevStyles.Priority as Priority
		   FROM dbo.Proj_Documents  , dbo.Proj_DocRev , dbo.Proj_Codes , dbo.Proj_RevStyles 
		   WHERE Proj_Codes.ProjectNo =  @ProjectId
				AND Proj_Codes.RevisionStyle = Proj_Revstyles.Id
				AND ((@TestRev IS NOT NULL) AND (@TestRev LIKE Proj_Revstyles.Tag))
		
   ORDER BY Priority DESC,RevId DESC;

Open in new window

0
Robberbaron (robr)
Asked:
Robberbaron (robr)
3 Solutions
 
Robberbaron (robr)Author Commented:
just realised i had too many tables in second query.  Removing them makes a significant improvement by itself.

   UNION                
       SELECT rtrim(@TestRev) as RevId, Proj_RevStyles.Priority as Priority
                   FROM dbo.Proj_Codes , dbo.Proj_RevStyles 
                   WHERE Proj_Codes.ProjectNo =  @ProjectId
                                AND Proj_Codes.RevisionStyle = Proj_Revstyles.Id
                                AND ((@TestRev IS NOT NULL) AND (@TestRev LIKE Proj_Revstyles.Tag))
                

Open in new window

0
 
RiteshShahCommented:
btw, can try removing "RTrim" function? it may give you good boost up, rather you can use something like this:

set @TestRev = rtrim('D')

there may be few more reason of low speed, like not having appropriate index, not having updated states but this is just a first look.
0
 
Éric MoreauSenior .Net ConsultantCommented:
0
 
Racim BOUDJAKDJICommented:
Try...

WHERE (col1 = ISNULL(@col1, col1))  AND
(col2 = ISNULL(@col2, col2) ) AND...
0
 
Robberbaron (robr)Author Commented:
none made any major improvement though the query may well be impacted by the parameter sniffing issue so have impleneted that workaround and added an index to assist with another process.
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now