jobs テーブルには 100 万件のレコードがあります。次のクエリの実行時間は 30 秒です。クエリは洗練されておらず、条件に一致するレコードの数を返します。最適化を手伝ってください。
DECLARE @MaxIdValue varchar(100)
DECLARE @SerchFilter varchar(100) = 'test'
DECLARE @TotalRowCount int = ( SELECT
Count(*) AS [RowCount]
FROM
[Jobs]
INNER JOIN [Sites] ON [Jobs].[SiteId] = [Sites].[Id]
INNER JOIN [JobStatusHistory] ON [Jobs].[JobStatusHistoryId] = [JobStatusHistory].[Id]
INNER JOIN [JobStatuses] ON [JobStatusHistory].[StatusId] = [JobStatuses].[Id]
INNER JOIN [JobTypes] ON [Jobs].[JobTypeId] = [JobTypes].[Id]
LEFT JOIN [Customers] ON [Jobs].[CustomerId] = [Customers].[Id]
LEFT JOIN [Priorities] ON [Jobs].[PriorityId] = [Priorities].[Id]
LEFT JOIN [TaskTypes] ON [Jobs].[TaskTypeId] = [TaskTypes].[Id]
LEFT JOIN [Areas] ON [Sites].[AreaId] = [Areas].[Id]
WHERE
(
[Sites].[Active]=1
AND
(
[Jobs].[OwnedByCompanyId]=18
OR
[Jobs].[OwnedByCompanyId] = 0)
AND
[Jobs].[Active]=1
)
AND
(
[Customers].[Name] LIKE '%'+@SerchFilter+'%'
OR
[Sites].[Name] LIKE '%'+@SerchFilter+'%'
OR
[Sites].[Address1] LIKE '%'+@SerchFilter+'%'
OR
[Sites].[Postcode] LIKE '%'+@SerchFilter+'%'
OR
[Sites].[UPRN] LIKE '%'+@SerchFilter+'%'
OR
[Sites].[Contact] LIKE '%'+@SerchFilter+'%'
OR
[Sites].[Telephone] LIKE '%'+@SerchFilter+'%'
OR
[Sites].[Work] LIKE '%'+@SerchFilter+'%'
OR
[Sites].[MobileNumber] LIKE '%'+@SerchFilter+'%'
OR
[Areas].[Description] LIKE '%'+@SerchFilter+'%'
OR
[Jobs].[Id] LIKE '%'+@SerchFilter+'%'
OR
[Jobs].[OrderNumber] LIKE '%'+@SerchFilter+'%'
OR
[Jobs].[Description_PlainText] LIKE '%'+@SerchFilter+'%'
OR
[Jobs].[JobNumber] LIKE '%'+@SerchFilter+'%'
OR
[JobTypes].[Description] LIKE '%'+@SerchFilter+'%'
OR
[TaskTypes].[Description] LIKE '%'+@SerchFilter+'%'
OR
[Priorities].[Description] LIKE '%'+@SerchFilter+'%'
)
)