Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Slow Stored Procedure Response

Status
Not open for further replies.

byrne1

Programmer
Aug 7, 2001
415
US
When I execute the following SQL command from within the Query Analyzer it takes one second to return 84 rows:

SELECT * FROM LOANS WHERE SHORT_NAME LIKE 'BYRNE%'

When I execute this same command through a stored procedure from within the Query Analyzer it takes 1 1/2 minutes to return the same 84 rows. Can someone help me understand why this happens and what I can do to speed up my stored procedure? Thanks in advance for your help.

QUERY ANALYZER COMMAND:
sp_GetLoansName byrne


STORED PROCEDURE:
CREATE PROCEDURE sp_GetLoansName
@ShortName varchar(20)
as
select * from loans where short_name like left(@ShortName,len(@shortname)) + '%'
 
When you execute the query Query Analyzer, the criteria has been resolved - LIKE 'BYRNE%'.

In the stored procedure SQL must reevaluate the function for every row in the table - LIKE (@ShortName,len(@shortname)) + '%'.

Change the SP as folows and let me know if it helps.

CREATE PROCEDURE sp_GetLoansName
@ShortName varchar(20)
as
Set @shortname=left(@ShortName,len(@shortname)) + '%'
select * from loans where short_name like @ShortName Terry

The reason why worry kills more people than work is that more people worry than work. - Robert Frost
 
YES!!!!

That worked! Thank you very much for your help! You have relieved me of a lot of stress! Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top