У меня медленный запрос к нашей базе данных Microsoft SQL.И если я удаляю часть этого, это ускоряется, но я не понимаю, почему и планировщик запросов мне не помогает (есть кое-что, что я пропускаю).
Запрос, который медленен> 30 секунд ( queryplan ):
SELECT COUNT(*)
FROM [dbo].[CarImage] AS [t0]
LEFT OUTER JOIN [dbo].[Dismantled] AS [t1] ON [t1].[Id] = [t0].[DismantledId]
WHERE([t1].[FinishedReason] <> 0)
AND ([t1].[FinishedDate] < GETDATE() - 365)
AND (NOT(EXISTS
(
SELECT NULL AS [EMPTY]
FROM [dbo].[Part] AS [t2]
WHERE([t2].[Status] <> 4)
AND ([t2].[Status] <> 3)
AND ([t2].[DismantledId] = [t1].[Id])
)))
AND (NOT(EXISTS
(
SELECT NULL AS [EMPTY]
FROM [dbo].[Part] AS [t3]
WHERE([t3].[Status] = 3)
AND ([t3].[SoldDate] > GETDATE() - 365)
AND ([t3].[DismantledId] = [t1].[Id])
)))
AND ((NOT(EXISTS
(
SELECT NULL AS [EMPTY]
FROM [dbo].[Partner] AS [t4],
[dbo].[SelfPickSite] AS [t5]
WHERE([t4].[Id] = [t1].[PartnerId])
AND ([t5].[PartnerId] = [t4].[Id])
)))
OR (EXISTS
(
SELECT NULL AS [EMPTY]
FROM [dbo].[SelfPickCar] AS [t6]
WHERE [t6].[DismantledId] = [t1].[Id]
)));
Если я удаляю последнюю часть, это <1 сек (<a href="https://ufile.io/uk1yp3rt" rel="nofollow noreferrer"> queryplan ):
SELECT COUNT(*)
FROM [dbo].[CarImage] AS [t0]
LEFT OUTER JOIN [dbo].[Dismantled] AS [t1] ON [t1].[Id] = [t0].[DismantledId]
WHERE([t1].[FinishedReason] <> 0)
AND ([t1].[FinishedDate] < GETDATE() - 365)
AND (NOT(EXISTS
(
SELECT NULL AS [EMPTY]
FROM [dbo].[Part] AS [t2]
WHERE([t2].[Status] <> 4)
AND ([t2].[Status] <> 3)
AND ([t2].[DismantledId] = [t1].[Id])
)))
AND (NOT(EXISTS
(
SELECT NULL AS [EMPTY]
FROM [dbo].[Part] AS [t3]
WHERE([t3].[Status] = 3)
AND ([t3].[SoldDate] > GETDATE() - 365)
AND ([t3].[DismantledId] = [t1].[Id])
)))
AND ((NOT(EXISTS
(
SELECT NULL AS [EMPTY]
FROM [dbo].[Partner] AS [t4],
[dbo].[SelfPickSite] AS [t5]
WHERE([t4].[Id] = [t1].[PartnerId])
AND ([t5].[PartnerId] = [t4].[Id])
))));