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 ««1234»»»

Outer Join Mystery Expand / Collapse
Author
Message
Posted Monday, May 15, 2006 7:38 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Tuesday, January 14, 2014 1:56 PM
Points: 522, Visits: 553

True and correct.  I just put in the example code for the old style. I had actually noticed the execution plans were pretty much identical.

How people can still write in that scares me

Post #279966
Posted Monday, May 15, 2006 7:58 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:51 AM
Points: 14,788, Visits: 27,264

A lot of it, unfortunately not all of it, comes from Oracle people. But even Oracle is starting down the path of eliminating the old style syntax. If you go out & search on the Oracle blogs & newsgroups, it's a big deal that lots of them are very unhappy about.



----------------------------------------------------
"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 #279975
Posted Monday, May 15, 2006 9:31 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, August 18, 2006 8:35 AM
Points: 15, Visits: 1

Hi,

This article & discussion is very interesting and it helped me a lot in understanding the Outer Join Query plus the old vs. new syntax.

Thanks once again

Regards

Amit Gurjar

Post #280046
Posted Monday, May 15, 2006 10:31 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, January 28, 2014 10:33 AM
Points: 24, Visits: 29

To get the Execution plans to match between the old and new syntax, the "old style" join query needs to look something like this:

SELECT
S.IIATransactionId
,substring(rats_filename,1+patindex('%{________-____-____-____-____________}%',rats_filename),36) AS OracleTransactionId
FROM iiafeedtransaction S,
ratsiiafeedtransaction o
WHERE S.IIATransactionId *= substring(rats_filename,1+patindex('%{________-____-____-____-____________}%',rats_filename),36)
GROUP BY
S.IIATransactionId,substring(rats_filename,1+patindex('%{________-____-____-____-____________}%',rats_filename),36)
HAVING substring(rats_filename,1+patindex('%{________-____-____-____-____________}%',rats_filename),36) IS NULL
ORDER BY S.IIATransactionId

Using the GROUP BY and the HAVING clauses puts the filter back into the execution plan and the correct results are seen.

Far easier to just use the new style of joins

Regards,

Mark Horton.

Post #280074
Posted Monday, May 15, 2006 11:30 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, January 30, 2008 2:38 PM
Points: 36, Visits: 2

Because you dont have control over the order of WHERE clause execution, the WHERE condition can occur before the JOIN condition, meaning that the WHERE occurs on the *Cartesian Product* of the two tables, and therefore doesnt filter rows properly.

If you think about this closely enough, it makes sense.

 

 

Post #280099
Posted Monday, May 15, 2006 12:25 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, June 06, 2006 1:55 PM
Points: 1, Visits: 1
So, how does Oracle return the "right" results?
Post #280128
Posted Monday, May 15, 2006 12:45 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 12:24 PM
Points: 2,835, Visits: 1,124

The use of the old-style syntax promoted the entire WHERE clause to the ON clause.  You get the same results as the "wrong" query if you used "LEFT JOIN ... ON S. = O. AND o.rats_filename IS NOT NULL".  The IS NOT NULL condition is being used as a a join condition instead of a filter condition and, since none of the fields are null, has no effect.

The only mystery to me is how Oracle differentiates join conditions and filter conditions.  Maybe only logical comparisons involving both tables are considered join conditions?




Post #280135
Posted Monday, May 15, 2006 1:00 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:51 AM
Points: 14,788, Visits: 27,264

It simply returns the anticipated results, which is only those rows that don't match. Precisely how... you need to ask someone that understands Oracle. I think Scott Coleman's answers have been more precise than anything I'm going to offer up.



----------------------------------------------------
"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 #280145
Posted Monday, May 15, 2006 1:04 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:51 AM
Points: 14,788, Visits: 27,264
Thanks. That makes more sense than any other explanation I've been able to come up with.

----------------------------------------------------
"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 #280147
Posted Monday, May 15, 2006 1:04 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Saturday, April 12, 2014 9:49 PM
Points: 39, Visits: 93

Itzik Ben-Gan wrote an article that explains this very clearly in SQL Magazine in the October 2004 issue. The InstantDoc ID is #43681.

The problem is that in Old Style Joins, you can't separate the join clause and the Where Clause so the result may include extraneous NULL values in your result set.

Here are two simple queries you can run in PUBS to see this:

-- Old Style Join
-- 13 Valid Entries, 6 Invalid Entries
select o.name, i.name,i.indid
from sysobjects o, sysindexes i
where o.id *= i.id and
o.type = 'S' and
i.indid = 1

-- New Style Join
-- 13 Valid Entries, 0 Invalid Entries
select o.name, i.name,i.indid
from sysobjects o
left outer join sysindexes i
on o.id = i.id
where o.type = 'S' and
i.indid = 1

Post #280148
« Prev Topic | Next Topic »

Add to briefcase ««1234»»»

Permissions Expand / Collapse