I am trying to optimize the performance of a stored procedure. I have to search two columns with LIKE operator. Here is the lookup table
CREATE TABLE [dbo].[PrincipalDiagnosis](
[PrincipalDiagnosisID] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
[Code] [varchar](10) NULL,
[PrincipalDiagnosisDescription] [varchar](3500) NULL,
)
I have created two independent NONCLUSTERED indexes on [Code] and [PrincipalDiagnosisDescription] columns. Here is the Query of the stored procedure:
SELECT RTRIM(LTRIM(Item)) AS SearchItem
INTO #SearchItems
FROM dbo.SplitString(@SearchQuery, ' ')
WHERE RTRIM(LTRIM(Item))<>''
SELECT TOP (100) PrincipalDiagnosisID,
ISNULL(Code +' '+ PrincipalDiagnosisDescription, '') AS Info
FROM dbo.PrincipalDiagnosis
WHERE EXISTS (
SELECT 1
FROM #SearchItems
WHERE Code LIKE '%'+SearchItem+'%'
OR PrincipalDiagnosisDescription LIKE '%'+SearchItem+'%'
)
ORDER BY 2
EXISTS part is taking so much time. In Execution Plan there is table scan of #SearchItems. I am using SQL server 2014 Enterprise edition. I want to optimize this query as much as I can. I will appreciate your valuable suggestions. Thanks! Muhammad