Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2008
»
T-SQL (SS2K8)
»
Function in Joining column degrades...
20 posts, Page 1 of 2
1
2
»»
Function in Joining column degrades performance in 2008
Rate Topic
Display Mode
Topic Options
Author
Message
aravind-305595
aravind-305595
Posted Tuesday, June 29, 2010 11:29 PM
SSC Rookie
Group: General Forum Members
Last Login: Tuesday, April 24, 2012 10:07 AM
Points: 37,
Visits: 114
Hi,
I have an existing code in sql server 2000, which after migrating to sql server 2008 the performance has become poor.
The query inside the code have a select statement which uses joins inside it. The joining statement has a function call on one of the joining column. The query returns the results faster in 2000 environment but it takes several minutes in 2008 to return the results.
Query Format:
select col1, col2, col3 .....
from #tmp t inner join table1
on table1.col1 = t.col1
left join table2 on table1.col1 = table2.col2
left join table3 on dbo.func_logic(table1.col1) = table3.col1
where table1.col4 <> 5 -- some business logic
Does 2008 has some kind of settings that woule enable the performance of the query faster?
Note: When I looked at the query plan of 2008, the place where we have the function call goes for the Lazy pool.
In the query plan of 2000, I dont see anything like that.
Post #945210
spaghettidba
spaghettidba
Posted Wednesday, June 30, 2010 1:50 AM
SSCarpal Tunnel
Group: General Forum Members
Last Login: Thursday, May 16, 2013 8:29 AM
Points: 4,804,
Visits: 8,067
To get rid of the lazy pool you should alter the function WITH SCHEMABINDING.
As a side note, using functions in join conditions is not a good idea at all.
Hope this helps
Gianluca
Get your two-cent-answer quickly
The Spaghetti DBA
Post #945265
Jeff Moden
Jeff Moden
Posted Wednesday, June 30, 2010 6:32 AM
SSC-Dedicated
Group: General Forum Members
Last Login: Today @ 11:05 AM
Points: 32,894,
Visits: 26,775
aravind-305595 (6/29/2010)
Hi,
I have an existing code in sql server 2000, which after migrating to sql server 2008 the performance has become poor.
The query inside the code have a select statement which uses joins inside it. The joining statement has a function call on one of the joining column. The query returns the results faster in 2000 environment but it takes several minutes in 2008 to return the results.
Query Format:
select col1, col2, col3 .....
from #tmp t inner join table1
on table1.col1 = t.col1
left join table2 on table1.col1 = table2.col2
left join table3 on dbo.func_logic(table1.col1) = table3.col1
where table1.col4 <> 5 -- some business logic
Does 2008 has some kind of settings that woule enable the performance of the query faster?
Note: When I looked at the query plan of 2008, the place where we have the function call goes for the Lazy pool.
In the query plan of 2000, I dont see anything like that.
I agree with Gianluca. Using a SCALAR function as part of a join is a guarantee of slow code. See if you can rewrite the function to be an Inline Table Valued Function which returns more than one row at a time and join the function as if it were a table.
--Jeff Moden
"
RBAR
is pronounced "ree-bar" and is a "Modenism" for "
R
ow-
B
y-
A
gonizing-
R
ow".
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #945412
Grant Fritchey
Grant Fritchey
Posted Wednesday, June 30, 2010 6:33 AM
SSChampion
Group: General Forum Members
Last Login: Today @ 10:57 AM
Points: 13,371,
Visits: 25,149
Is that a single statement table valued function or a multi-statement table valued function? If multi-statement, you were just lucky that it ran well in 2000. These types of functions don't have statistics and work off a basis of one row, which can result in pretty radically poor execution plans.
However, if it's the former, you may just need to restructure the query. The optimizer is very different between 2008 and 2000. Almost every query I've worked with came out better in 2008, but I've heard of a few that didnt'.
----------------------------------------------------
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
The Scary DBA
Author of:
SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans
Product Evangelist for
Red Gate Software
Post #945414
Jeff Moden
Jeff Moden
Posted Wednesday, June 30, 2010 6:35 AM
SSC-Dedicated
Group: General Forum Members
Last Login: Today @ 11:05 AM
Points: 32,894,
Visits: 26,775
Heh... I finally beat Grant to the punch for a change.
--Jeff Moden
"
RBAR
is pronounced "ree-bar" and is a "Modenism" for "
R
ow-
B
y-
A
gonizing-
R
ow".
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #945418
Grant Fritchey
Grant Fritchey
Posted Wednesday, June 30, 2010 7:12 AM
SSChampion
Group: General Forum Members
Last Login: Today @ 10:57 AM
Points: 13,371,
Visits: 25,149
Jeff Moden (6/30/2010)
Heh... I finally beat Grant to the punch for a change.
Oh right, Mr. Umpty-gazillion points, you "finally" beat me to the punch.
----------------------------------------------------
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
The Scary DBA
Author of:
SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans
Product Evangelist for
Red Gate Software
Post #945446
aravind-305595
aravind-305595
Posted Friday, July 09, 2010 1:01 AM
SSC Rookie
Group: General Forum Members
Last Login: Tuesday, April 24, 2012 10:07 AM
Points: 37,
Visits: 114
Hi, Thanks all. I thought to re-write that piece of code with suggestion given in this forum. Now I face another issue with the joins, my code looks like,
update table1 set col1 = s.col1
from table1 w inner join [SQLServer2].[database1].[dbo].[table1] s on w.col2 + ':' + w.col3 = s.col2 and w.col4 = s.col3
where w.col1 is null
and w.col2 is not null.
I am using two concatenated columns the join and the joining table is from a linked server. The table1 has around 2 million records.
The same query works in 2 minutes in SQL 2000 but it takes around 18 minutes in SQL 2008.
I dont know why the joins degrade the performance for these kind of queries in SQL 2008. Please suggest some best idea to overcome this issue. Again if I have to re-write his code, I am not sure how many more will come with this kind of scenario in the joins.
Post #949757
HowardW
HowardW
Posted Friday, July 09, 2010 2:48 AM
Ten Centuries
Group: General Forum Members
Last Login: Today @ 11:53 AM
Points: 1,035,
Visits: 7,670
You're never going to get great performance with this type of query, but it's worth checking that at least collation compatible is set to true in the properties for the linked server so it can avoid returning the entire table to the local server first.
Are both servers 2008?
Post #949788
Grant Fritchey
Grant Fritchey
Posted Friday, July 09, 2010 6:00 AM
SSChampion
Group: General Forum Members
Last Login: Today @ 10:57 AM
Points: 13,371,
Visits: 25,149
aravind-305595 (7/9/2010)
Hi, Thanks all. I thought to re-write that piece of code with suggestion given in this forum. Now I face another issue with the joins, my code looks like,
update table1 set col1 = s.col1
from table1 w inner join [SQLServer2].[database1].[dbo].[table1] s on w.col2 + ':' + w.col3 = s.col2 and w.col4 = s.col3
where w.col1 is null
and w.col2 is not null.
I am using two concatenated columns the join and the joining table is from a linked server. The table1 has around 2 million records.
The same query works in 2 minutes in SQL 2000 but it takes around 18 minutes in SQL 2008.
I dont know why the joins degrade the performance for these kind of queries in SQL 2008. Please suggest some best idea to overcome this issue. Again if I have to re-write his code, I am not sure how many more will come with this kind of scenario in the joins.
The concatenation of two columns is going to kill performance. You won't be able to use indexes. Better to have a calculated column that you can index for this.
Also, are you hopping servers? If so, you're going to get smacked with the fact that it has to bring all the data back from the second server for processing on the first one. As written it won't filter the data on the second server. Instead you would need to use OPENQUERY and pass parameters to filter the data on the second server before it retrieves it.
----------------------------------------------------
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
The Scary DBA
Author of:
SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans
Product Evangelist for
Red Gate Software
Post #949875
aravind-305595
aravind-305595
Posted Tuesday, July 13, 2010 3:11 AM
SSC Rookie
Group: General Forum Members
Last Login: Tuesday, April 24, 2012 10:07 AM
Points: 37,
Visits: 114
Yes both are 2008 servers
Post #951225
« Prev Topic
|
Next Topic »
20 posts, Page 1 of 2
1
2
»»
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.