SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Does Anti JOIN SQL run faster than JOIN SQL?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

TomHand
Starting Member

Australia
8 Posts

Posted - 11/06/2012 :  18:16:26  Show Profile  Reply with Quote
this is conventional JOIN
SELECT D1.deptno, D1.dname
FROM dept D1
MINUS
SELECT D2.deptno, D2.dname
FROM dept D2, emp E2
WHERE D2.deptno = E2.deptno
ORDER BY 1;

this query can be rewritten in Anti JOIN

SELECT D.deptno, D.dname
FROM dept D
WHERE NOT EXISTS
(
SELECT 1
FROM emp E
WHERE E.deptno = D.deptno
)
ORDER BY D.deptno;

which query (Anti JOIN or JOIN) has a faster performance regradless of SQL engine platform (Orcle, SQL server, Mysql)?



robvolk
Most Valuable Yak

USA
15663 Posts

Posted - 11/06/2012 :  19:36:22  Show Profile  Visit robvolk's Homepage  Reply with Quote
It's meaningless to state "regardless of SQL engine platform" as MINUS is only valid for Oracle. The equivalent for SQL Server is EXCEPT, and I don't know if MySQL has an equivalent or if is supports EXISTS(). In any case, even if you find a syntax they all support, none of them will optimize or process the SQL the same way, and performance will vary.

By the way, the LEFT JOIN syntax would be better classified as a "conventional JOIN", as it is supported by the ISO SQL standard:
SELECT D.deptno, D.dname
FROM dept D
LEFT JOIN emp E ON D.deptno = E.deptno
WHERE E.deptno IS NULL
ORDER BY D.deptno;
That is somewhat more likely to work on all 3 platforms, and actually uses JOIN syntax. They still won't process the same way, although their optimizers may construct the same or very similar plan to any of the other forms you listed.
Go to Top of Page

Transact Charlie
Flowing Fount of Yak Knowledge

United Kingdom
3451 Posts

Posted - 11/07/2012 :  07:09:05  Show Profile  Visit Transact Charlie's Homepage  Reply with Quote
quote:

which query (Anti JOIN or JOIN) has a faster performance regradless of SQL engine platform (Orcle, SQL server, Mysql)?


It's easy to forget that when we talk about set based code from a logical principal -- that underneath the hood there is no set based operation going on.

Any engine on any classical computer will need to break down into loops of code at some point. So your question isn't really answerable.

That leaves us with style and statement:

For me a LEFT JOIN and a WHERE XX IS NULL construction isn't really stating what you want (its not being declarative)

Both EXCEPT and NOT EXISTS are more declarative. EXCEPT is the iso standard (oracle uses MINUS)

but if you want to talk about performance you need to talk in terms of engines and implementations.

Transact Charlie
Msg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
http://nosqlsolution.blogspot.co.uk/
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000