x

Query performance

Hi

I have a stored procedure which queries in 5 secs.

Recently i have added a UDF in select

select col1,col2,col3,UDF_function(col1),col4 from table inner join table 1 ..........11 tables are joined here.

after adding udf_function() my query execution time is taking minimum 15 mins which is horrible. Kindly help me to how to improve the performance.

the function returns the values of a column as comma seperated string. it means i am looping through the records in the function.

The reason why i am using UDF is becos of the below table structure and requirement

Table1

p.k 	
col1    col2    col3
1       x       z
2       v       q
3       n       w
4       m       e

Table2

f.k 
1       a
1       b
1       c
2       a
2       d

Output

col1    col2
1       a,b,c
2       a,d
3
4

This is mainly to avoid duplicates

Thanks in advance Usha

more ▼

asked Nov 03 '09 at 08:54 AM in Default

Usha gravatar image

Usha
110 7 7 8

Can you provide us with some more details please (table strucutre, sample data)?
Nov 03 '09 at 09:23 AM John Sansom
(comments are locked)
10|1200 characters needed characters left

6 answers: sort voted first

You could try creating an indexed view based off the UDF for pre-aggregation. Go to BOL and search on Creating Indexed Views

Based on your new information I would say you could create an indexed view utilizing the UDF for every table.

The views would basically be

Create View vTabx 
With Schemabinding
As
Select col1, UDF(col2)
From Tabx

You would then have persisted data and could join this into your query.

Assuming the views created column one as the FK and column two as the comma separated list you could do something similar to the following:

Select Tab0.col1, vTab1UDF.col2, vTab2UDF.col2,... From Tab0 Inner Join vTab1UDF On vTab1UDF.col1 = Tab0.col1 Inner Join vTab2UDF On vTab2UDF.col1 = Tab0.col1 ... 
more ▼

answered Nov 03 '09 at 10:45 AM

Blackhawk-17 gravatar image

Blackhawk-17
11.8k 28 30 35

Thank u. The example you have given is very clear. Now my query execution is taking 3 mins for 270 records which is better when compared to 15 mins .
Nov 05 '09 at 02:59 AM Usha
(comments are locked)
10|1200 characters needed characters left

You probably want to read this blog post http://sqlblog.com/blogs/adam_machanic/archive/2009/05/31/grouped-string-concatenation-the-winner-is.aspx

It is all about how to return grouped strings. There is an attached file which contains the winning scripts. Look at them to see what can be done with this matter.

//Peso

more ▼

answered Nov 03 '09 at 09:38 AM

Peso gravatar image

Peso
1.6k 5 6 8

You're only linking to that because you won! ;)
Nov 03 '09 at 09:44 AM Melvyn Harbour 1 ♦♦
(comments are locked)
10|1200 characters needed characters left

I suspect your problem is that you're falling into Jeff Moden's RBAR (Row By Agonising Row). What you're attempting to do should be possible with an appropriate aggregate.

more ▼

answered Nov 03 '09 at 08:58 AM

Melvyn Harbour 1 gravatar image

Melvyn Harbour 1 ♦♦
1.4k 18 20 22

(comments are locked)
10|1200 characters needed characters left

Also, bear in mind that calling any sort of scalar UDF on a row-by-row basis can dramatically reduce performance.

more ▼

answered Nov 03 '09 at 09:42 AM

Matt Whitfield gravatar image

Matt Whitfield ♦♦
29.4k 61 65 87

(comments are locked)
10|1200 characters needed characters left

Here is another link with exact matching samples
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=81254

more ▼

answered Nov 08 '09 at 04:06 AM

Peso gravatar image

Peso
1.6k 5 6 8

nice set of scripts and test results in that link without having to download a scripts file
Nov 09 '09 at 11:23 PM KenJ
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x242
x27

asked: Nov 03 '09 at 08:54 AM

Seen: 1075 times

Last Updated: Nov 04 '09 at 08:12 AM