programmher
Programmer
I have a stored procedure that queries a table with over a million records. The table is indexed and does have a primary key. There are several necessary conditions within the procedure. The procedure takes several seconds to execute and return a result to a web form.
Is there anything else I can do to optimize the performance of this procedure to the results can be displayed faster?
My code is thus:
create procedure usp_PROCEDURENAME
@Client varchar(75) = NULL,
@salesman_lastname varchar(50) = NULL,
@ClientCity varchar(50) = NULL,
@ClientState varchar(4)= NULL,
@CSR_LastName varchar(50)= NULL
@CSR_EmpNum int= NULL
AS
Declare @SQLSTR varchar(2000)
select @sqlstr = 'SELECT * from tbl_ClientOrders WITH NOLOCK) where '
IF @Client IS NOT NULL
select @sqlstr = @sqlstr + 'Client like ''' + @Client + '%''' + ' and'
IF @salesman_Lastname IS NOT NULL
select @sqlstr = @sqlstr + ' salesman_lastname like ''' + @salesman_lastname + '%''' + ' and'
IF @ClientCity IS NOT NULL
select @sqlstr = @sqlstr + ' ClientCity like ''' + @ClientCity + '%''' + ' and'
IF @ClientState IS NOT NULL
select @sqlstr = @sqlstr + ' ClientState like ''' + @ClientState + '%''' + ' and'
IF @CSR_LastName IS NOT NULL
select @sqlstr = @sqlstr + ' CSR_lastname like ''' + @CSR_lastname + '%'''+ ' and'
IF @CSR_EmpNum IS NOT NULL
select @sqlstr = @sqlstr + ' CSR_EmpNum = @CSR_EmpNum''
IF right(@sqlstr,3) = 'and'
select @sqlstr = left(@sqlstr,datalength(@sqlstr)-3)
Is there anything else I can do to optimize the performance of this procedure to the results can be displayed faster?
My code is thus:
create procedure usp_PROCEDURENAME
@Client varchar(75) = NULL,
@salesman_lastname varchar(50) = NULL,
@ClientCity varchar(50) = NULL,
@ClientState varchar(4)= NULL,
@CSR_LastName varchar(50)= NULL
@CSR_EmpNum int= NULL
AS
Declare @SQLSTR varchar(2000)
select @sqlstr = 'SELECT * from tbl_ClientOrders WITH NOLOCK) where '
IF @Client IS NOT NULL
select @sqlstr = @sqlstr + 'Client like ''' + @Client + '%''' + ' and'
IF @salesman_Lastname IS NOT NULL
select @sqlstr = @sqlstr + ' salesman_lastname like ''' + @salesman_lastname + '%''' + ' and'
IF @ClientCity IS NOT NULL
select @sqlstr = @sqlstr + ' ClientCity like ''' + @ClientCity + '%''' + ' and'
IF @ClientState IS NOT NULL
select @sqlstr = @sqlstr + ' ClientState like ''' + @ClientState + '%''' + ' and'
IF @CSR_LastName IS NOT NULL
select @sqlstr = @sqlstr + ' CSR_lastname like ''' + @CSR_lastname + '%'''+ ' and'
IF @CSR_EmpNum IS NOT NULL
select @sqlstr = @sqlstr + ' CSR_EmpNum = @CSR_EmpNum''
IF right(@sqlstr,3) = 'and'
select @sqlstr = left(@sqlstr,datalength(@sqlstr)-3)