4 dual core cpus del 2950, 64gig memory, 64bit Win2003. 24gig in use by sql.
140 ms with my changes
if @IsSuperUser = 1
begin
INSERT INTO @sec (SecurityKey, CanView)
SELECT DISTINCT S.SecurityKey,1 as [CanView]
FROM dbo.Security AS S LEFT OUTER JOIN
dbo.UserRoles AS R ON S.ObjectId = R.RoleID
WHERE @IsSuperUser = 1 Group By s.SecurityKey
end
else
begin
INSERT INTO @sec (SecurityKey, CanView)
SELECT DISTINCT S.SecurityKey, IsNull(MAX(Convert(int,S.[View])),0) as [CanView]
FROM dbo.Security AS S LEFT OUTER JOIN
dbo.UserRoles AS R ON S.ObjectId = R.RoleIDWHERE (S.[View] = 1) AND
( (
(R.EffectiveDate IS NULL) AND (R.ExpiryDate >= GETDATE()) OR
(R.EffectiveDate IS NULL) AND (R.ExpiryDate IS NULL) OR
(R.EffectiveDate <= GETDATE()) AND (R.ExpiryDate IS NULL) OR
(R.EffectiveDate <= GETDATE()) AND (R.ExpiryDate >= GETDATE()))
AND R.UserId = @UserId AND @UserId > 0)
Group By s.SecurityKey
union
SELECT DISTINCT S.SecurityKey, IsNull(MAX(Convert(int,S.[View])),0) as [CanView]
FROM dbo.Security AS S LEFT OUTER JOIN
dbo.UserRoles AS R ON S.ObjectId = R.RoleIDWHERE (S.[View] = 1)
AND ( (@UserId = -1 AND S.ObjectId = -3)
OR (S.ObjectId = -1) )
Group By s.SecurityKey
end
VERSUS Original logic from vendor code
18547 ms
INSERT INTO @sec (SecurityKey, CanView)
SELECT DISTINCT S.SecurityKey,CASE WHEN @IsSuperUser = 1 THEN 1 ELSE IsNull(MAX(Convert(int,S.[View])),0)END as [CanView]
FROM dbo.Security AS S LEFT OUTER JOIN
dbo.UserRoles AS R ON S.ObjectId = R.RoleID
WHERE (@IsSuperUser = 1) OR
(
(S.[View] = 1)
AND
(
(
(R.EffectiveDate IS NULL) AND (R.ExpiryDate >= GETDATE()) OR
(R.EffectiveDate IS NULL) AND (R.ExpiryDate IS NULL) OR
(R.EffectiveDate <= GETDATE()) AND (R.ExpiryDate IS NULL) OR
(R.EffectiveDate <= GETDATE()) AND (R.ExpiryDate >= GETDATE()))
AND
R.UserId = @UserId AND @UserId > 0
)
OR
(
(@UserId = -1 AND S.ObjectId = -3)
OR
(S.ObjectId = -1)
)
)
Group By s.SecurityKey
The issue is the nested OR statements. They ALL run and all add to the time and execution of the sql. This was a prior version, I can’t wait to see their performance tuned one in their next release.