Click here to Skip to main content
15,913,773 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am trying to create function which can replace certain words with hyperlink in sql. When I call the function as query in sql, its takes a really long time to execute the query, more than 2-3min. I assumed this is because, the tag_libary table has around 600,000 records and iterating through large number, would consume a lot of processing time.
SQL
CREATE FUNCTION dbo.ReplaceTags(@body VARCHAR(MAX))
RETURNS VARCHAR(MAX)
AS
BEGIN

SELECT @body = REPLACE(@body,name,'<a href="/KB/answers/pagename.aspx"?tag='+name+'">'+name+'</a>')
FROM Tag_Library

RETURN @body
END

Hence, I am writing to seek some advice, if there is a way to make this sql function optimal or would it be better to change this function, into a insert trigger?

Please advice, if possible.
Posted
Comments
Tomas Takac 6-Nov-14 6:03am    
I don't see how the insert trigger would help here. Unless you are ok with the fact your inserts will take 2-3min to complete.
miss786 6-Nov-14 6:12am    
Thank you for your reply. I am slightly struggling to figure out a way, if there is way to execute the query under 1 min time, if possible. Please advice further.
Tomas Takac 6-Nov-14 8:12am    
600k replacements is a lot, that's the problem here I guess. You could catgorize the tags and inputs somehow so you don't have to do all the replacements each time, just a subset based on the category.
Tomas Takac 6-Nov-14 9:50am    
Do you need to do it on database level? Maybe application layer would be more suitable. BTW how do the tags look like? Can you post a sample of @body and Tag_Library?

1 solution

Since you have a lot of records so it may take time. Why dont you use a trigger instead of a function. It may help you
 
Share this answer
 
Comments
Tomas Takac 6-Nov-14 9:49am    
What about the insert then? That will then take 2-3mins. You are not solving the problem, just moving it around.
miss786 6-Nov-14 10:39am    
Thank you you all for your suggestion. Will the following procedure work, if I partition the tag_library data into smaller subset called "small_libraryTag". Then create a update trigger, to update the article field body with hyperlinks (i.e. call the above function). Is this new procedure plausible, to execute quickly.

Many thanks for your help and advice/
SITU.PATI 11-Nov-14 10:59am    
Do you have only 600k data to change or each time you give insert or update the data you have to change?
If you sub part it, then also it take the same time. Since there are a lot of record so it may take some time
miss786 12-Nov-14 7:23am    
Hi, thank you for reply. would it be possible to update the function, so it only looks for tags name after date September 2014?
Thank you for your advice.
SITU.PATI 19-Nov-14 10:35am    
Sure, Just put a select query while updating. It will do the thing

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900