Created following script to load user accounts. Used a parameter string for username in initial testing in management studio. Odd thing is after I changed from parameter string to string literal the query slowed down by 20 seconds. Isn't it the other way usually for parameter sniffing? I've tried DBCC FREEPROCCACHE and creating a stored procedure with setting some local vars, but that didn't speed up the query. Any suggestions?
DECLARE @accntRep VARCHAR(50)
SET @accntRep = 'someUserName'
SELECT accntRep,transBalance FROM companyView AS cv
OUTER APPLY
(SELECT SUM(CASE WHEN pastdue > 0 THEN balance ELSE 0 END) AS pastDueBalance,
SUM(balance) AS transBalance FROM pastDueView WHERE compID = cv.compID
) AS balance
WHERE
-- accntRep = @accntRep
accntRep = 'someUserName'
GROUP BY accntRep,transBalance