工作表中有 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+'%'
)
)