473,387 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Why are the "same" queries optimised differently ?

We have seen this a lot, but have just experienced the opposite to what
we have always seen previously, so this has prompted me to ask a high
level - why do we get this behaviour?

If we re-write queries in the following pattern (simplified)

select ...
from table1 t1 inner join table2 t2 on (t1.pk = t2.fk)
inner join table3 t3 on (t3.fk = t2.pk)

to

select
(select .. from table3 t3 where t3.fk = t2.pk)
from
table1 t1 inner join table2 t2 on (t1.pk = t2.fk)

It optimises to a plan significantly better (very often).

However, yesterday we saw the opposite (for the first time).

So, my question is simply this (not too much detail req.) -
If/why does the compiler rewrite/optimise these patterns differently
?

Many thanks.

PaulR.

Nov 12 '05 #1
6 1365

Hi Paul,

When using the explicit JOIN syntax, you force the optimizer to choose that
join order, in your case first t2 and t3 and then t1.
(I just tried it again using empty tables to proove it is still this way in
V8)

If you use the WHERE clause instead, you give the optimizer the freedom of
choosing the join order, it will use statistics to determine which is best
for you.

So how about rewriting using WHERE like

select ... from t1, t2, t3 where t1.pk=t2.fk and t3.fk=t2.pk

That should do it best.

Juliane
--
Message posted via http://www.dbmonster.com
Nov 12 '05 #2
Juliane via DBMonster.com wrote:
Hi Paul,

When using the explicit JOIN syntax, you force the optimizer to choose that
join order, in your case first t2 and t3 and then t1.
(I just tried it again using empty tables to proove it is still this way in
V8)

I don't this is correct in DB2 V8 for LUW at least.
Without join reordering life in the optimizer would be rather dull ... ;-)
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #3

Serge,

That is true for inner joins - I tested with three tables only in the sample
database.

Still, I checked again on the sample database using outer joins:
2 statements returning the same:

a)select c.projname, a.firstnme, a.lastname, b.deptno
from employee a inner join department b on a.empno=b.mgrno left outer join
project c on c.deptno=b.deptno

b)select c.projname, a.firstnme, a.lastname, b.deptno
from department b left outer join project c on c.deptno=b.deptno inner join
employee a on b.mgrno = a.empno

but resulting in different access plans and slightly different costs.

The optimized SQL returns for
a)SELECT Q4.PROJNAME AS "PROJNAME", Q3.$C2 AS "FIRSTNME", Q3.$C1 AS
"LASTNAME",
Q3.$C0 AS "DEPTNO"
FROM
(SELECT Q2.DEPTNO, Q1.LASTNAME, Q1.FIRSTNME
FROM U48JTO.EMPLOYEE AS Q1, U48JTO.DEPARTMENT AS Q2
WHERE (Q1.EMPNO = Q2.MGRNO)) AS Q3 LEFT OUTER JOIN U48JTO.PROJECT AS Q4 ON

(Q4.DEPTNO = Q3.$C0)

b)SELECT Q3.$C2 AS "PROJNAME", Q4.FIRSTNME AS "FIRSTNME", Q4.LASTNAME AS
"LASTNAME", Q3.$C1 AS "DEPTNO"
FROM
(SELECT Q2.MGRNO, Q2.DEPTNO, Q1.PROJNAME
FROM U48JTO.PROJECT AS Q1 RIGHT OUTER JOIN U48JTO.DEPARTMENT AS Q2 ON
(Q1.DEPTNO = Q2.DEPTNO)) AS Q3, U48JTO.EMPLOYEE AS Q4
WHERE (Q3.$C0 = Q4.EMPNO)
Any comments ?

Juliane

Serge Rielau wrote:
Hi Paul,

When using the explicit JOIN syntax, you force the optimizer to choose that
join order, in your case first t2 and t3 and then t1.
(I just tried it again using empty tables to proove it is still this way in
V8)

I don't this is correct in DB2 V8 for LUW at least.
Without join reordering life in the optimizer would be rather dull ... ;-)

--
Message posted via http://www.dbmonster.com
Nov 12 '05 #4
Juliane via DBMonster.com wrote:
Serge,

That is true for inner joins - I tested with three tables only in the sample
database.

Still, I checked again on the sample database using outer joins:
2 statements returning the same:

a)select c.projname, a.firstnme, a.lastname, b.deptno
from employee a inner join department b on a.empno=b.mgrno left outer join
project c on c.deptno=b.deptno

b)select c.projname, a.firstnme, a.lastname, b.deptno
from department b left outer join project c on c.deptno=b.deptno inner join
employee a on b.mgrno = a.empno

but resulting in different access plans and slightly different costs.

The optimized SQL returns for
a)SELECT Q4.PROJNAME AS "PROJNAME", Q3.$C2 AS "FIRSTNME", Q3.$C1 AS
"LASTNAME",
Q3.$C0 AS "DEPTNO"
FROM
(SELECT Q2.DEPTNO, Q1.LASTNAME, Q1.FIRSTNME
FROM U48JTO.EMPLOYEE AS Q1, U48JTO.DEPARTMENT AS Q2
WHERE (Q1.EMPNO = Q2.MGRNO)) AS Q3 LEFT OUTER JOIN U48JTO.PROJECT AS Q4 ON

(Q4.DEPTNO = Q3.$C0)

b)SELECT Q3.$C2 AS "PROJNAME", Q4.FIRSTNME AS "FIRSTNME", Q4.LASTNAME AS
"LASTNAME", Q3.$C1 AS "DEPTNO"
FROM
(SELECT Q2.MGRNO, Q2.DEPTNO, Q1.PROJNAME
FROM U48JTO.PROJECT AS Q1 RIGHT OUTER JOIN U48JTO.DEPARTMENT AS Q2 ON
(Q1.DEPTNO = Q2.DEPTNO)) AS Q3, U48JTO.EMPLOYEE AS Q4
WHERE (Q3.$C0 = Q4.EMPNO)
Any comments ?

A mixed flavors of joins are harder to permutate.

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #5
So what does the syntax

select
(select .. from table3 t3 where t3.fk = t2.pk)
from
table1 t1 inner join table2 t2 on (t1.pk = t2.fk)

force/assist (if anything) the optimiser to do?

NB. The performance differences are significant, in our usage
scenarios.

Thanks.

Nov 12 '05 #6
PaulR wrote:
So what does the syntax

select
(select .. from table3 t3 where t3.fk = t2.pk)
from
table1 t1 inner join table2 t2 on (t1.pk = t2.fk)

force/assist (if anything) the optimiser to do?

NB. The performance differences are significant, in our usage
scenarios.

The scalar subselect on table3 has:
"NULL on EMPTY" (like OUTER JOIN) and "ENFORCE MAX CARDINALITY = 1"
semantics.
In theory the optimizer could realize that the foreign key relationship
makes both properties obsolete and reduce the scalar subquery, once
rewritten to an outer join, to a regular inner join.
I'm not sure whether this is done at all and if so under which
circumstances.
If you want to get into this level of detail I'd guess a PMR would be in
order.

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #7

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

Similar topics

3
by: Dmitri Shvetsov | last post by:
Hi, Maybe somebody knows why it's happening? I wrote a C# Windows Application working with the remote database through a DataSet. It works cool from my computer but when I gave this...
7
by: funktacular | last post by:
Hi- I have script that works fine when I run it from a server, but I need to be able to load the page and have it work from my hard drive. However, it seems the when the url changes from...
94
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock...
19
by: glchin | last post by:
Does a compiler guarantee that the variable w below is placed on an eight-byte aligned address? void myFunction( long iFreq ) { const double w = two_pi * iFreq; ... ... }
3
by: Willy | last post by:
I have a website that uses querystrings on a certain page to show multiple contents. I have created mapped pages to hide the gory details of these querystrings. So instead of...
9
AmberJain
by: AmberJain | last post by:
Hello, What is the difference between --------> 1. clrscr(); // defined in various header files 2. system("cls"); //available in stdio.h in bloodshed dev c++ Well,...
5
by: dougmeece | last post by:
I have two forms with one calling the other to perform searches. When I run the search form the 2nd form a blank record is added to my table. The table is appended or updated from entries on the...
2
by: sabbadin12 | last post by:
Hi, I'm going to work on an application that uses a postgreSQL database so that it can uses SQLServer 2005. I think I solved most problems on the programming side, but I still have some doubts...
7
by: Ray Proffitt | last post by:
Hi. obj.ToString(); or obj + ""; Which is more efficient and why?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.