Я не понимаю, почему этот запрос медленный - PullRequest
0 голосов
/ 17 апреля 2019

У меня медленный запрос к нашей базе данных 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])
))));

1 Ответ

0 голосов
/ 17 апреля 2019

ИЛИ - убийца производительности; Пожалуйста, попробуйте этот запрос и дайте мне результат

  SELECT COUNT(*)
    FROM (
        SELECT 1
        FROM [dbo].[CarImage] AS [t0]
        LEFT 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])
                            )
                        )
                    )

                UNION ALL

                SELECT 1
                FROM [dbo].[CarImage] AS [t0]
                LEFT JOIN [dbo].[Dismantled] AS [t1] ON [t1].[Id] = [t0].[DismantledId]
                WHERE (
                        EXISTS (
                            SELECT NULL AS [EMPTY]
                            FROM [dbo].[SelfPickCar] AS [t6]
                            WHERE [t6].[DismantledId] = [t1].[Id]
                            )
                        )
                )
        ) X

ps: если у вас есть проблемы с производительностью, запустите этот код SET STATISTICS IO ON, а затем выполните запрос и опубликуйте результат

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...