我正在尝试像这样优化查询:
SELECT id, type, parent_id
FROM [dbo].[v5] with(nolock)
WHERE
(
USER_ID() = 1
OR
(
v5.type IN (1,2,3)
and
EXISTS
(
SELECT obj_id FROM dbo.access with(nolock)
where access.obj_id = id
AND obj_id.[u_name] in
(
select SUSER_SNAME() union all
select [name] COLLATE database_default from dbo.u_role
)
)
)
)
它工作25秒。当我评论这一行时:
--USER_ID() = 1
-- OR
它工作5秒。如何优化此查询?如何避免在 where 子句中使用 USER_ID()?