Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Function in Joining column degrades performance in 2008 Expand / Collapse
Author
Message
Posted Tuesday, June 29, 2010 11:29 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, August 12, 2014 7:30 AM
Points: 37, Visits: 119
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
Posted Wednesday, June 30, 2010 1:50 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 8:38 AM
Points: 4,655, Visits: 11,125
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


--
Gianluca Sartori

How to post T-SQL questions
spaghettidba.com
@spaghettidba
Post #945265
Posted Wednesday, June 30, 2010 6:32 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 7:27 AM
Points: 35,770, Visits: 32,437
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 "Row-By-Agonizing-Row".

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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #945412
Posted Wednesday, June 30, 2010 6:33 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:22 AM
Points: 14,205, Visits: 28,534
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 Query Performance Tuning
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #945414
Posted Wednesday, June 30, 2010 6:35 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 7:27 AM
Points: 35,770, Visits: 32,437
Heh... I finally beat Grant to the punch for a change.

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #945418
Posted Wednesday, June 30, 2010 7:12 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:22 AM
Points: 14,205, Visits: 28,534
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 Query Performance Tuning
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #945446
Posted Friday, July 9, 2010 1:01 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, August 12, 2014 7:30 AM
Points: 37, Visits: 119
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
Posted Friday, July 9, 2010 2:48 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, November 3, 2014 8:05 AM
Points: 1,191, Visits: 9,892
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
Posted Friday, July 9, 2010 6:00 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:22 AM
Points: 14,205, Visits: 28,534
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 Query Performance Tuning
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #949875
Posted Tuesday, July 13, 2010 3:11 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, August 12, 2014 7:30 AM
Points: 37, Visits: 119
Yes both are 2008 servers
Post #951225
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse