我对我们的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秒(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])
))));
答案 0 :(得分:0)
OR是性能杀手; 请尝试此查询并给我结果
Any
ps:如果您遇到性能问题,请运行以下代码 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
,然后运行查询并发布结果