SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Odd behaviour from computed columns in a left outer join


Odd behaviour from computed columns in a left outer join

Author
Message
Kevin Dahl
Kevin Dahl
SSC Rookie
SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)

Group: General Forum Members
Points: 42 Visits: 313
I have the following query:


SELECT CE.ID ,
CA.CustomerID,
CA.PostalCode
FROM DBXN.CommunicationEntry CE
LEFT OUTER JOIN ( SELECT A.ID ,
CA.CustomerID ,
A.PostalCode,
A.EnglishAddressLine1
FROM DBXN.Customer_Address AS CA
INNER JOIN DBXN.Address AS A ON CA.AddressID = A.ID
) AS CA ON CE.CustomerID = CA.CustomerID
WHERE CE.ID = 6298103



When I run this I get very good results, there's an index seek and a key lookup on the Customer_Address table, and an index seek on the Address table - all with very low estimated number of rows (between 1-3). Each have seek predicates coming from the joined column.

However, the second I introduce this additional column:


SELECT CE.ID ,
CA.CustomerID,
CA.PostalCode,
CA.EnglishAddressLine1
FROM DBXN.CommunicationEntry CE
LEFT OUTER JOIN ( SELECT A.ID ,
CA.CustomerID ,
A.PostalCode,
A.EnglishAddressLine1
FROM DBXN.Customer_Address AS CA
INNER JOIN DBXN.Address AS A ON CA.AddressID = A.ID
) AS CA ON CE.CustomerID = CA.CustomerID
WHERE CE.ID = 6298103



...the query plan changes to a full index scan where the estimated number of rows includes every record in the table, and there are no predicates whatsoever used until further up stream when it hits a filter. By that time there's an estimated 82.5 million rows with an estimated data size of 21GB.

The EnglishAddressLine1 column is a computed column that takes several of the other fields in the Address table and builds a formatted address, there are several others like it for Line2/Line3, etc - but this is the simplest illustration of the problem.

Just wondering if anyone has any insight on why a computed column coming from a left outer join would cause this?
Attachments
NQ1.sqlplan (9 views, 34.00 KB)
NQ2.sqlplan (7 views, 406.00 KB)
Alexander Suprun
Alexander Suprun
Mr or Mrs. 500
Mr or Mrs. 500 (511 reputation)Mr or Mrs. 500 (511 reputation)Mr or Mrs. 500 (511 reputation)Mr or Mrs. 500 (511 reputation)Mr or Mrs. 500 (511 reputation)Mr or Mrs. 500 (511 reputation)Mr or Mrs. 500 (511 reputation)Mr or Mrs. 500 (511 reputation)

Group: General Forum Members
Points: 511 Visits: 1516
Please post plans for both of the queries.


Alex Suprun
Kevin Dahl
Kevin Dahl
SSC Rookie
SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)

Group: General Forum Members
Points: 42 Visits: 313
Hey Alex,

I've added the .sqlplan files to the original post... Thanks for your response!
Kenneth.Fisher
Kenneth.Fisher
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4276 Visits: 2033
Just a guess but you might consider looking at your indexes. You probably have an index that covers CE.ID, CA.CustomerID and CA.PostalCode but doesn't include CA.EnglishAddressLine1.

I did a quick test and I was able to include a computed column in an index. Both as part of the index and as an included column.

Kenneth Fisher
I strive to live in a world where a chicken can cross the road without being questioned about its motives.
--------------------------------------------------------------------------------
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/

Link to my Blog Post --> www.SQLStudies.com
Alexander Suprun
Alexander Suprun
Mr or Mrs. 500
Mr or Mrs. 500 (511 reputation)Mr or Mrs. 500 (511 reputation)Mr or Mrs. 500 (511 reputation)Mr or Mrs. 500 (511 reputation)Mr or Mrs. 500 (511 reputation)Mr or Mrs. 500 (511 reputation)Mr or Mrs. 500 (511 reputation)Mr or Mrs. 500 (511 reputation)

Group: General Forum Members
Points: 511 Visits: 1516
Kenneth.Fisher (8/30/2012)
Just a guess but you might consider looking at your indexes. You probably have an index that covers CE.ID, CA.CustomerID and CA.PostalCode but doesn't include CA.EnglishAddressLine1.

I did a quick test and I was able to include a computed column in an index. Both as part of the index and as an included column.
Unfortunately it's not the case here. The function is referencing columns from other tables.


Alex Suprun
Evil Kraig F
Evil Kraig F
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8565 Visits: 7660
Kevin,

Can you post the schema and indexes for the tables CommunicationEntry, Customer_Address, and Address please?


- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
Kevin Dahl
Kevin Dahl
SSC Rookie
SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)

Group: General Forum Members
Points: 42 Visits: 313
It's also only one of twelve similar columns on the same table, and that table currently sits at 19.7 million rows. Even if we could index each of them, I'm not sure we would. This is a db in development at the moment, and it would probably make more sense to go another route - like calling the address formatting function on the A.ID on the left side of the join, or elsewhere.

What I'm really interested in though, is determining why the join predicate gets applied so differently in each of the scenarios.
Kevin Dahl
Kevin Dahl
SSC Rookie
SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)

Group: General Forum Members
Points: 42 Visits: 313
Hey Craig,

I'm not sure I could post them in a useful format, if you're looking to try and re-create the actual problem. There ends up being quite a few foreign key relations through those three tables.

I'll check with the powers that be, and see how much I can post here.
Alexander Suprun
Alexander Suprun
Mr or Mrs. 500
Mr or Mrs. 500 (511 reputation)Mr or Mrs. 500 (511 reputation)Mr or Mrs. 500 (511 reputation)Mr or Mrs. 500 (511 reputation)Mr or Mrs. 500 (511 reputation)Mr or Mrs. 500 (511 reputation)Mr or Mrs. 500 (511 reputation)Mr or Mrs. 500 (511 reputation)

Group: General Forum Members
Points: 511 Visits: 1516
There are more tables in the query than those 3.
To be able to re-create the issue these tables are needed as well:

[DBXN].StreetNumberSuffixLookup
[DBXN].StreetTypeLookup
[DBXN].StreetDirectionLookup
[DBXN].RouteServiceTypeLookup
[DBXN].DeliveryInstallationTypeLookup
[DBXN].CountryLookup
[DBXN].ProvinceLookup


Alex Suprun
Evil Kraig F
Evil Kraig F
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8565 Visits: 7660
Kevin Dahl (8/30/2012)
Hey Craig,

I'm not sure I could post them in a useful format, if you're looking to try and re-create the actual problem. There ends up being quite a few foreign key relations through those three tables.

I'll check with the powers that be, and see how much I can post here.


I'm not looking to recreate the issue, I won't have your data disparity and other assundry bits to do that. What I'm looking to do is be able to analyze the indexes available, what's being ignored and what else it could use, things like that. With an understanding of the clustered vs. non-clustered we can offer you an alternative version of the same logic that will work with what you've already got built... hopefully.


- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search