I have a stored procedure that has some problems with slow performance.
The procedure has 2 parameters
@firstname varchar(100)
@lastname varchar(100)
These parameters can have values like a%, that is wildcard searches.
The strange thing is that if one of the parameters has the value %, and
the other one a%, the performance is very bad.
If i subsistute the variables with exactly the same values hardcoded in
the where-clause, the query is very fast.
If both variables has some characters prepending the percent sign, the
performance is better.
SELECT distinct u.user_id, u.username, u.status_id
FROM statusnames sn, statuses s, users u, users_persons up, persons p,
users_roles ur
WHERE p.lastname like @lastname
AND p.firstname like @firstname
AND s.status_id = u.status_id
AND sn.statusname_i d = s.statusname_id
AND u.user_id = up.user_id
AND up.person_id = p.person_id
What makes SQL server behave so differently with variables and
hardcoded values?
The procedure has 2 parameters
@firstname varchar(100)
@lastname varchar(100)
These parameters can have values like a%, that is wildcard searches.
The strange thing is that if one of the parameters has the value %, and
the other one a%, the performance is very bad.
If i subsistute the variables with exactly the same values hardcoded in
the where-clause, the query is very fast.
If both variables has some characters prepending the percent sign, the
performance is better.
SELECT distinct u.user_id, u.username, u.status_id
FROM statusnames sn, statuses s, users u, users_persons up, persons p,
users_roles ur
WHERE p.lastname like @lastname
AND p.firstname like @firstname
AND s.status_id = u.status_id
AND sn.statusname_i d = s.statusname_id
AND u.user_id = up.user_id
AND up.person_id = p.person_id
What makes SQL server behave so differently with variables and
hardcoded values?
Comment