最近我遇到了一个问题,我们有多个并发客户端请求导致db中的性能问题。我尝试了测试场景,结果发现,当我运行SELECT查询(相同查询)6到7次(越来越差)时,它会降低性能并且执行会花费很多时间。不过我试过这个
SELECT TOP (100) COUNT(DISTINCT([Doc_Number])) AS "Expression"
FROM (
SELECT *
FROM "dbo"."Dummy_Table" "table_alias"
WHERE ((CAST("table_alias"."ID" AS NVARCHAR)) NOT IN
(
SELECT "PrimaryKey" AS ExceptionKey
FROM dbo.exceptions inner_exceptionStatus
LEFT JOIN dbo.Workflow inner_workflowStates ON
(inner_exceptionStatus."Status"= inner_workflowStates."UUID" AND
inner_exceptionStatus."UUID"= 'CA1662D6-73A2-4692-A765-E7E3EDB66062')
WHERE ("inner_workflowStates"."RemoveFromRecordSet" = 1 AND
"inner_workflowStates"."IsDeleted" = 0) AND
("inner_exceptionStatus"."IsArchived" IS NULL OR
"inner_exceptionStatus"."IsArchived" = 0)))) wrapperQuery
单独运行时的查询大约需要1秒的执行时间。但是如果我们并行运行它,对于每个查询,它会花费大量时间导致超时。 我唯一困扰的是SELECT查询应该是非阻塞的,即使使用共享锁,也需要轻松相处。
我不确定查询中是否有任何错误会导致情况恶化。
非常感谢任何帮助!!
答案 0 :(得分:2)
试试这种方式
SELECT Count(DISTINCT( [Doc_Number] )) AS Expression
FROM dbo.Dummy_Table table_alias
WHERE NOT EXISTS (SELECT 1
FROM dbo.exceptions inner_exceptionStatus
INNER JOIN dbo.Workflow inner_workflowStates
ON ( inner_exceptionStatus.Status = inner_workflowStates.UUID
AND inner_exceptionStatus.UUID = 'CA1662D6-73A2-4692-A765-E7E3EDB66062' )
WHERE inner_workflowStates.RemoveFromRecordSet = 1
AND inner_workflowStates.IsDeleted = 0
AND ( inner_exceptionStatus.IsArchived IS NULL
OR inner_exceptionStatus.IsArchived = 0 )
AND table_alias.ID = PrimaryKey)
做了几个改变。
NOT IN
更改为NOT EXISTS
"table_alias"."ID"
中的转换,因为它会避免使用"table_alias"."ID"
列中的任何索引。如果确实需要转换,则添加它。 Top (100)
,因为没有Group By
它将返回单个记录作为结果。 如果查询运行缓慢,您需要发布execution plan
并确保statistics
是最新的
答案 1 :(得分:0)
您可以像这样简单地查询您的查询:
SELECT COUNT(DISTINCT(Doc_Number)) AS Expression
FROM dbo.Dummy_Table dmy
WHERE not exists
(
SELECT *
FROM dbo.exceptions ies
INNER JOIN dbo.Workflow iws ON ies.Status= iws.UUID AND ies.UUID= 'CA1662D6-73A2-4692-A765-E7E3EDB66062'
WHERE iws.RemoveFromRecordSet = 1 AND iws.IsDeleted = 0 AND (ies.IsArchived IS NULL OR ies.IsArchived = 0)
and dmy.ID=PrimaryKey
)
就像prdp说:
我补充说:
您可以使用INNER JOIN,因为您在测试RemoveFromRecordSet = 1的位置然后删除空值。
将where utils引号,括号和parenthèses删除到where子句