This is a test senario, made with temporary tables to illustrate the problem. Pretend table @userdata has index on userid and table @users has index on id
Why is the first select unable to use index, I assumed it would perform better in 1 subselect than in 2 subselects?
Version - Microsoft SQL Server 2008 R2 (RTM) Compatibility level - SQL Server 2000.
-- test tables
DECLARE @userdata TABLE(info VARCHAR(50), userid INT)
DECLARE @users TABLE(id INT, username VARCHAR(20), superuser BIT)
-- test data
INSERT @users VALUES(1, 'superuser', 1)
INSERT @users VALUES(2, 'testuser1', 0)
INSERT @users VALUES(3, 'testuser2', 0)
INSERT @userdata VALUES('secret information', 1)
INSERT @userdata VALUES('testuser1''s data', 2)
INSERT @userdata VALUES('testuser2''s data', 3)
INSERT @userdata VALUES('testuser2''s data',3)
DECLARE @username VARCHAR(50)
SET @username = 'superuser'
--SET @username = 'testuser1'
--The superuser can read all data
--The testusers can only read own data
-- This sql can't use indexes and is very slow
SELECT *
FROM @userdata d
WHERE EXISTS
(SELECT 1 FROM @users u
WHERE u.username = @username AND u.superuser = 1 OR
u.id = d.userid AND u.username = @username)
-- This sql uses indexes and performs well
SELECT *
FROM @userdata d
WHERE EXISTS
(SELECT 1 FROM @users u
WHERE u.username = @username AND u.superuser = 1)
OR EXISTS (SELECT 1 FROM @users u
WHERE u.ID = d.userid
AND u.username = @username)