I have a small database with 7 tables with 6 reference table
I need to do search on the base of key word.
So I wrote a procedure as follows:
ALTER PROCEDURE [dbo].[GetByKeyWord] @Keyword NVARCHAR(60) = '',
@PageNumber BIGINT = 1,
@PageSize BIGINT = 100 AS BEGIN
SET nocount ON;
DECLARE @Query NVARCHAR(2000) = '';
DECLARE @q1 NVARCHAR(100) = '';
DECLARE @q2 NVARCHAR(500) = '';
DECLARE @pgsize NVARCHAR(40) = '';
SET @Query ='; WITH CTE AS
( Select [dbo].[CTable].CaseId ,[dbo].[CTable].LoanAmount,[dbo].[CTable].CommDate,[dbo].[CTable].LastSubmissionDate,[dbo].[CTable].Aging,[dbo].[CTable].BankersORCreditorsCity,[dbo].[CTable].BusinessNature,[dbo].[CT].CreditorName,
[dbo].[DT].DebtorName,[dbo].[IT].IPName,[dbo].[ST].SectorName,[dbo].[AT].CatgoryName,[dbo].[AT].CategoryStart , ROW_NUMBER() OVER (ORDER BY [dbo].[CTable].DebtorId ) as rowNo
from [dbo].[CTable]
LEFT JOIN [dbo].[DT] ON [dbo].[DT].DebtorId = [dbo].[CTable].DebtorId
LEFT JOIN [dbo].[RT] ON [dbo].[RT].RPId = [dbo].[CTable].RPId
LEFT JOIN [dbo].[ST] ON [dbo].[ST].SectorId = [dbo].[CTable].SectorId
LEFT JOIN [dbo].[IT] ON [dbo].[IT].IPId = [dbo].[CTable].IpId
LEFT JOIN [dbo].[AT] ON [dbo].[AT].CategoryId = [dbo].[CTable].AgingCategoryId
LEft JOIN [dbo].[CT] ON [dbo].[CT].CreditorId = [dbo].[CTable].CreditorId
Where ( [dbo].[CTable].IsDeleted IS NULL OR [dbo].[CTable].IsDeleted = 0) '
IF (@Keyword != ''
AND @Keyword IS NOT NULL) BEGIN
SELECT @q1 = TRIM(@Keyword) ;
SELECT @q2 = @q2 +' AND( [dbo].[CTable].BusinessNature LIKE '''+ '%'+@q1 +'%'+ '''' ;
SELECT @q2 = @q2 +' OR [dbo].[DT].DebtorName LIKE '''+ '%'+@q1 +'%'+ '''' ;
SELECT @q2 = @q2 +' OR [dbo].[CT].CreditorName LIKE '''+ '%'+@q1 +'%'+ '''' ;
SELECT @q2 = @q2 +' OR LoanAmount LIKE '''+ '%'+@q1 +'%'+ '''' ;
SELECT @q2 = @q2 +' OR [dbo].[ST].SectorName LIKE '''+ '%'+@q1 +'%'+ '''' ;
SELECT @q2 = @q2 +' OR [dbo].[IT].IPName LIKE '''+ '%'+@q1 +'%'+ ''')' ;
END
SELECT @q1 = @PageNumber ;
SELECT @pgsize = @PageSize ;
-- select @q2 = @q2 +' And [dbo].[CTable].LastSubmissionDate = '+@q1 ;
SELECT @q2 = @q2 +' )
SELECT *
FROM CTE
WHERE rowNo > (' + @pgsize + ' * (' + @q1 + ' - 1 ) )
AND rowNo <= ( ' + @pgsize +' * '+ @q1 + ') '
SET @Query = @Query + @q2 --select @Query
EXEC (@Query) ;
END
In the CTable
there are just 974
records still its taking around 25
seconds
in query execution
What can be done to improve the performance in query execution?
What are the alternates available?