473,499 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

outer join table filtering

Is it true that the following query:

SELECT emp.empno, emp.lastname, dept.deptname
FROM emp LEFT OUTER JOIN dept
ON emp.workdept = dept.deptno
WHERE emp.salary 50000.00;

will run faster in general if rewritten as:

SELECT emp.empno, emp.lastname, dept.deptname
FROM (SELECT empno, lastname FROM emp WHERE salary 50000.00) as
e
LEFT OUTER JOIN dept
ON emp.workdept = dept.deptno

I thought the where clause in the 1st query would filter out rows in
emp before joining with dept, just like the 2nd query does? How about
this:

SELECT emp.empno, emp.lastname, dept.deptname
FROM emp LEFT OUTER JOIN dept
ON emp.salary 50000.00 and emp.workdept = dept.deptno

Thanks.
Oct 2 '08 #1
1 2413
On Oct 2, 8:47*am, "Henry J." <tank209...@yahoo.comwrote:
Is it true that the following query:

* * SELECT emp.empno, emp.lastname, dept.deptname
* * *FROM emp LEFT OUTER JOIN dept
* * * * * ON emp.workdept = dept.deptno
* * * WHERE emp.salary 50000.00;
CELKO has a good discussion about the logic behind what is going on.
However, if you look at the plan that DB2 is using to generate this,
you will find that it takes several shortcuts to speed up processing.
DB2 will look at the statistics for the tables behind the query and
decide what to do based on those statistics. I suspect since you
imply that your first rewrite of the query was faster than the second
one that DB2 might have decided that joining the two tables up would
filter out more rows than applying the "emp.salary 50000.00"
condition would, and so it joined them up first and then filtered. If
the statisics were different, DB2 may decide to filter first and then
join. It may be that with better statistics with distribution turned
on that DB2 might run this query better for you.

In reply to CELKO, since this criteria is on the OUTER part of the
join, it is safe to apply the filter before the join - there is no
chance of getting extra rows this way. If the criteria was on the
inner part of the join, then yes, bad results would happen.

-Chris
Oct 3 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
6230
by: Guinness Mann | last post by:
In my (admittedly brief) sojurn as an SQL programmer I've often admired "outer joins" in textbooks but never really understood their use. I've finally come across a problem that I think is served...
8
4957
by: Matt | last post by:
Hello I have to tables ar and arb, ar holds articles and a swedish description, arb holds descriptions in other languages. I want to retreive all articles that match a criteria from ar and...
7
1665
by: Greg | last post by:
I'm a quantitative securities analyst working with Compustat data (company fiscal reports and pricing feeds). My coworker came across a problem that we fixed, but I'd like to understand 'why' it...
4
4844
by: thilbert | last post by:
All, I have a perplexing problem that I hope someone can help me with. I have the following table struct: Permission ----------------- PermissionId Permission
3
10040
by: Dam | last post by:
Using SqlServer : Query 1 : SELECT def.lID as IdDefinition, TDC_AUneValeur.VALEURDERETOUR as ValeurDeRetour FROM serveur.Data_tblDEFINITIONTABLEDECODES def,...
0
2334
by: Steve Claflin | last post by:
I'm using mysql 4.0.x, which does not allow the following query (which works fine in 4.1). Is there any way I can issue a single query to achieve the same results? (I want a list of all records...
3
19454
by: Martin | last post by:
Hello everybody, I have the following question. As a join clause on Oracle we use " table1.field1 = table2.field1 (+) " On SQL Server we use " table1.field1 *= table2.field1 " Does DB2...
1
2858
by: Bob Alston | last post by:
I am trying to use an access sql statement in a module that does a left outer join on one table - joined to a table that has a selection criteria. The result is an inner join. If I do this in...
9
9200
by: shanevanle | last post by:
I have two tables that are pretty big. I need about 10 rows in the left table and the right table is filtered to 5 rows as well. It seems when I join the tables in the FROM clause, I have to...
0
7131
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5470
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4600
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3099
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
297
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.