Solved

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

Posted on 2009-07-10
5
225 Views
Last Modified: 2012-05-07

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
Comment
Question by:Robberbaron (robr)
5 Comments
 
LVL 32

Author Comment

by:Robberbaron (robr)
ID: 24821171
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
 
LVL 31

Assisted Solution

by:RiteshShah
RiteshShah earned 150 total points
ID: 24821247
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
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 300 total points
ID: 24821668
0
 
LVL 23

Assisted Solution

by:Racim BOUDJAKDJI
Racim BOUDJAKDJI earned 50 total points
ID: 24836581
Try...

WHERE (col1 = ISNULL(@col1, col1))  AND
(col2 = ISNULL(@col2, col2) ) AND...
0
 
LVL 32

Author Closing Comment

by:Robberbaron (robr)
ID: 31601974
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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
GeoClustering  and AOG 25 41
Duplicated data in GROUP_CONCAT 2 14
sql query 5 37
Trigger C# code inside the SQL Server 6 24
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

679 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