我有一个SQL查询,大约需要1分钟才能获得75,000条记录。
我想知道是否有办法通过更改任何内容来加快速度。
SELECT c.CampaignID,
c.Description,
c.Active,
c.CampaignTypeID,
CASE c.CampaignTypeID
WHEN 1 THEN 'TEST1'
WHEN 2 THEN 'TEST2'
WHEN 3 THEN 'TEST3'
WHEN 5 THEN 'TEST4'
WHEN 6 THEN 'TEST5'
END AS CampaignType,
CASE c.Active
WHEN 1 THEN 'TEST6'
ELSE 'TEST7'
END AS style,
CASE c.Active
WHEN 1 THEN 'Deactivate'
ELSE 'Activate'
END AS stat,
REPLACE( REPLACE(c.Description,'\\','\\\\'),'''','\\') AS LinkDescription,
@CompanyID AS CompanyID
FROM Campaign c WITH(nolock)
LEFT OUTER JOIN UserPermissions
ON up.CampaignID=c.CampaignID
AND up.UserID = @UserID
WHERE c.CompanyID = @CompanyID
and c.IsCampaignHidden = 0
AND (@IsAdmin = 0
and case when (@IsAdmin = 0) then up.UserID else 0 end is not null
and case when (@IsAdmin = 0) then up.[View] else 1 end = 1
感谢任何帮助。
答案 0 :(得分:1)
这令人困惑......
您的条件AND (@IsAdmin = 0
后面跟着另一个条件case when (@IsAdmin = 0)
,您已经定义了@IsAdmin = 0
,我认为以下case when
不是必需的,因为它始终转向{ {1}}因为您已经定义了TRUE
。您必须清理代码,删除不需要的行,因为这会导致额外的执行时间,特别是在@IsAdmin = 0
条件下。
您应避免在WHERE
中使用多个CASE WHEN
条件,因为这会导致执行时间过长。
WHERE
我认为你不需要 WHERE c.CompanyID = @CompanyID
and c.IsCampaignHidden = 0
AND (@IsAdmin = 0
and case when (@IsAdmin = 0) then up.UserID else 0 end is not null
and case when (@IsAdmin = 0) then up.[View] else 1 end = 1
答案 1 :(得分:0)
尝试删除
REPLACE( REPLACE(c.Description,'\\','\\\\'),'''','\\') AS LinkDescription
看看它运行了多长时间;如果它更快,找到一种不同的方式去做正在做的事情。