473,788 Members | 2,692 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join efficiency

Hello all,

Recently a post on this list made me think a bit about the way in which I
write my queries.

I have always written queries with ordinary joins in this manner:

SELECT * FROM a, b WHERE a.x=b.x;

However I recently saw an laternative syntax:

SELECT * FROM a JOIN b ON a.x=b.x;

Is there any difference between these queries in terms of the speed of
planning or the quality of the plan untimately used? I'd imagine that the
second form provides more information that the planner may be able to use
to make a better plan (or make a good plan more easily), but I've never
had any problems with the first form.

It also seems to me that the second form is more self-documenting, which
is something I'm always in favour of.

I'd appreciate anyone's thought/insight.

Thanks.

--

Russell Brown

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #1
10 1800
NOTE: The first way cannot support OUTER joins, the second way can. Hence sometimes one has to use
the second way for at least some of the joins.

PREVIOUSLY: The second way can allow one to tell the planner a "better way" to join the tables.
Likewise it can also enable the programmer to force the planner into a worse way. Oops!
NOW: I believe that the latest version of postgres (7.4.x) the planner will override the 2nd methods
requested join method if it knows of a better way and can do the better way. (Outer joins need to
be done last, by the nature of them, and so cannot be changed much, there may be other cases where
the planner cannot change the requested plan).

I am not an expert, but this is what I recall from following the list.

Terry Fielder
Manager Software Development and Deployment
Great Gulf Homes / Ashton Woods Homes
te***@greatgulf homes.com
Fax: (416) 441-9085

-----Original Message-----
From: pg************* ****@postgresql .org
[mailto:pg****** ***********@pos tgresql.org]On Behalf Of Russ Brown
Sent: Wednesday, September 01, 2004 7:55 AM
To: pg***********@p ostgresql.org
Subject: [GENERAL] Join efficiency
Hello all,

Recently a post on this list made me think a bit about the
way in which I
write my queries.

I have always written queries with ordinary joins in this manner:

SELECT * FROM a, b WHERE a.x=b.x;

However I recently saw an laternative syntax:

SELECT * FROM a JOIN b ON a.x=b.x;

Is there any difference between these queries in terms of the
speed of
planning or the quality of the plan untimately used? I'd
imagine that the
second form provides more information that the planner may be
able to use
to make a better plan (or make a good plan more easily), but
I've never
had any problems with the first form.

It also seems to me that the second form is more
self-documenting, which
is something I'm always in favour of.

I'd appreciate anyone's thought/insight.

Thanks.

--

Russell Brown

---------------------------(end of
broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index
scan if your
joining column's datatypes do not match

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #2
Russ Brown wrote:

I have always written queries with ordinary joins in this manner:

SELECT * FROM a, b WHERE a.x=b.x;

However I recently saw an laternative syntax:

SELECT * FROM a JOIN b ON a.x=b.x;

Is there any difference between these queries in terms of the speed of
planning or the quality of the plan untimately used? I'd imagine that
the second form provides more information that the planner may be able
to use to make a better plan (or make a good plan more easily), but
I've never had any problems with the first form.
The first form allows PG to plan however it sees fit. The second will
force the join order to be the same as you specify in the query. This
doesn't matter here, but might with a more complicated query.

With v7.4 and higher, I believe this join forcing is configurable
(join_collapse_ limit).
It also seems to me that the second form is more self-documenting,
which is something I'm always in favour of.


I tend to prefer the WHERE form, but that might just be me.

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #3
Hi, thanks for your reply,

On Wed, 1 Sep 2004 08:10:52 -0400, <te***@ashtonwo odshomes.com> wrote:
NOTE: The first way cannot support OUTER joins, the second way can.
Hence sometimes one has to use
the second way for at least some of the joins.

Yes, I've always done OUTER joins the second way. I suppose it's just the
way I was taught SQL: I was initially taught now to do 'ordinary' joins
using the first syntax, and then taught 'LEFT' joins using the second
syntax when I came to need to use them (I very much leaned SQL 'on the
job', though I know of people who *always* use OUTER joins in their
queries). I'd never considered that there was another syntax!
PREVIOUSLY: The second way can allow one to tell the planner a "better
way" to join the tables.
Likewise it can also enable the programmer to force the planner into a
worse way. Oops!
NOW: I believe that the latest version of postgres (7.4.x) the planner
will override the 2nd methods
requested join method if it knows of a better way and can do the better
way. (Outer joins need to
be done last, by the nature of them, and so cannot be changed much,
there may be other cases where
the planner cannot change the requested plan).

That being the case, would it be true to say that with recent versions of
PostgreSQL they both perform identically, meaning the second could be
considered preferable due to its self-documenting nature (and consistency
with the OUTER JOIN syntax)?
I am not an expert, but this is what I recall from following the list.

Terry Fielder
Manager Software Development and Deployment
Great Gulf Homes / Ashton Woods Homes
te***@greatgulf homes.com
Fax: (416) 441-9085

-----Original Message-----
From: pg************* ****@postgresql .org
[mailto:pg****** ***********@pos tgresql.org]On Behalf Of Russ Brown
Sent: Wednesday, September 01, 2004 7:55 AM
To: pg***********@p ostgresql.org
Subject: [GENERAL] Join efficiency
Hello all,

Recently a post on this list made me think a bit about the
way in which I
write my queries.

I have always written queries with ordinary joins in this manner:

SELECT * FROM a, b WHERE a.x=b.x;

However I recently saw an laternative syntax:

SELECT * FROM a JOIN b ON a.x=b.x;

Is there any difference between these queries in terms of the
speed of
planning or the quality of the plan untimately used? I'd
imagine that the
second form provides more information that the planner may be
able to use
to make a better plan (or make a good plan more easily), but
I've never
had any problems with the first form.

It also seems to me that the second form is more
self-documenting, which
is something I'm always in favour of.

I'd appreciate anyone's thought/insight.

Thanks.

--

Russell Brown

---------------------------(end of
broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index
scan if your
joining column's datatypes do not match


--

Russell Brown

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #4
Does anyone know if there is a postgres shorthand for Oracle's (+)
notation to denote an outer join?

eg

SELECT * from a, b where a.x = b.x (+)

John Sidney-Woollett

Richard Huxton wrote:
Russ Brown wrote:

I have always written queries with ordinary joins in this manner:

SELECT * FROM a, b WHERE a.x=b.x;

However I recently saw an laternative syntax:

SELECT * FROM a JOIN b ON a.x=b.x;

Is there any difference between these queries in terms of the speed
of planning or the quality of the plan untimately used? I'd imagine
that the second form provides more information that the planner may
be able to use to make a better plan (or make a good plan more
easily), but I've never had any problems with the first form.

The first form allows PG to plan however it sees fit. The second will
force the join order to be the same as you specify in the query. This
doesn't matter here, but might with a more complicated query.

With v7.4 and higher, I believe this join forcing is configurable
(join_collapse_ limit).
It also seems to me that the second form is more self-documenting,
which is something I'm always in favour of.

I tend to prefer the WHERE form, but that might just be me.


---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #5
John Sidney-Woollett wrote:
Does anyone know if there is a postgres shorthand for Oracle's (+)
notation to denote an outer join?

eg

SELECT * from a, b where a.x = b.x (+)


Just the standard LEFT JOIN ... afaik

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddres sHere" to ma*******@postg resql.org)

Nov 23 '05 #6
Russ Brown wrote:
SELECT * FROM a, b WHERE a.x=b.x; SELECT * FROM a JOIN b ON a.x=b.x;
That being the case, would it be true to say that with recent versions of
PostgreSQL they both perform identically, meaning the second could be
considered preferable due to its self-documenting nature (and consistency
with the OUTER JOIN syntax)?


Assuming join_collapse_l imit is at it's default or set higher...

As far as I can say from reading the documentation, following the hackers
list and trying out myself: yes, both versions should yield the same
optimized query plan and are therefore equal performance wise.

You can just use the one you prefer.

Best Regards,
Michael Paesold
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddres sHere" to ma*******@postg resql.org)

Nov 23 '05 #7
"Russ Brown" <po******@dot4d ot.plus.com> writes:
Is there any difference between these queries in terms of the speed of
planning or the quality of the plan untimately used?


http://www.postgresql.org/docs/7.4/s...cit-joins.html
http://www.postgresql.org/docs/7.3/s...cit-joins.html
http://www.postgresql.org/docs/7.2/s...cit-joins.html
http://www.postgresql.org/docs/7.1/s...cit-joins.html

depending on which version you are using. (I think 7.1-7.3
are essentially alike, but 7.4 is not.)

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postg resql.org

Nov 23 '05 #8
On Wed, 01 Sep 2004 10:31:07 -0400, Tom Lane <tg*@sss.pgh.pa .us> wrote:
"Russ Brown" <po******@dot4d ot.plus.com> writes:
Is there any difference between these queries in terms of the speed of
planning or the quality of the plan untimately used?


http://www.postgresql.org/docs/7.4/s...cit-joins.html
http://www.postgresql.org/docs/7.3/s...cit-joins.html
http://www.postgresql.org/docs/7.2/s...cit-joins.html
http://www.postgresql.org/docs/7.1/s...cit-joins.html

depending on which version you are using. (I think 7.1-7.3
are essentially alike, but 7.4 is not.)

regards, tom lane


Thanks for that: very informative.

I should have spotted that in the manual myself, though it has been nice
reading other people's opinions on the subject too.

Regards.

--

Russell Brown

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #9
This thread also brings up the question... what ever happened to the *
notation of the SQL2 standard for LEFT and RIGHT outer joins?

To pull all rows from table 'a' and only those from table 'b' that match
the column criteria.
SELECT * FROM a, b WHERE a.x*=b.x;

To pull all rows from table 'b' and only those from table 'a' that match
the column criteria.
SELECT * FROM a, b WHERE a.x=*b.x;

This notation was always easy to remember when you think of the asterisk
as being a wildcard (or 'all') for its side of the expression.

Russ Brown wrote:
Hello all,

Recently a post on this list made me think a bit about the way in
which I write my queries.

I have always written queries with ordinary joins in this manner:

SELECT * FROM a, b WHERE a.x=b.x;

However I recently saw an laternative syntax:

SELECT * FROM a JOIN b ON a.x=b.x;

Is there any difference between these queries in terms of the speed
of planning or the quality of the plan untimately used? I'd imagine
that the second form provides more information that the planner may
be able to use to make a better plan (or make a good plan more
easily), but I've never had any problems with the first form.

It also seems to me that the second form is more self-documenting,
which is something I'm always in favour of.

I'd appreciate anyone's thought/insight.

Thanks.


--
Thanks,
Laura Vance
Systems Engineer
Winfree Academy Charter Schools
6221 Riverside Dr. Suite 110
Irving, Tx 75039
Web: www.winfreeacademy.com

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #10

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

Similar topics

4
1371
by: Denis St-Michel | last post by:
Hello All, Hope some Guru will be able to help me with this. Let's take this example table A ------------------------------------------------------------------------------- id | TicketNo | evaluation | Username ------------------------------------------------------------------------------- 1 1 9 Jamie 2 1 8.5 ...
2
3044
by: kjc | last post by:
Not sure if this is the right group to post this to but. This is the current query that I have. select tableA.id,tableB.artist,tableB.image,from tableA,tableB where tableA.image = tableB.image AND tableB.price >0 AND tableB.price < 20 order by tableB.price DESC' What I need is, for each row returned I need information from a third and fourth table. tableC, and tableD.
7
11195
by: steve | last post by:
Can you join two tables across different servers in mySQL, or am I dreaming of Oracle? -- Posted using the http://www.dbforumz.com interface, at author's request Articles individually checked for conformance to usenet standards Topic URL: http://www.dbforumz.com/mySQL-Join-servers-ftopict225285.html Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbforumz.com/eform.php?p=777346
2
2674
by: terence.parker | last post by:
I am often faced with the dilemma of whether to use a JOIN query across three tables in order to grab a bunch of results - or whether to create another table to represent what I want. The latter is less normalised, but seems less computationally expensive to me(?). Basicially what I have is: Friend (uid1,uid2,fid) -> FriendshipCategories(fid,cid) -> Categories(cid,name) If I want to find out all the 'categories' (names) which a user...
5
4267
by: Bob Stearns | last post by:
As part of what I want in a report, I want the associated row from t2, if there is one, with the largest date in a range based on the base row from t1. I attempted the following: left outer join IS3.service_ai t2 on T1.dam_BHID=T2.cow_BHID and t2.datex = (select max(datex) from is3.service_ai where days(datex) < (days(t1.birth_date)-283+45) and days(datex) > (days(t1.birth_date)-283-45) )
14
5722
by: mike | last post by:
I'm using postgresl 7.3.2 and have a query that executes very slowly. There are 2 tables: Item and LogEvent. ItemID (an int4) is the primary key of Item, and is also a field in LogEvent. Some ItemIDs in LogEvent do not correspond to ItemIDs in Item, and periodically we need to purge the non-matching ItemIDs from LogEvent. The query is: delete from LogEvent where EventType != 'i' and ItemID in
335
11890
by: extrudedaluminiu | last post by:
Hi, Is there any group in the manner of the C++ Boost group that works on the evolution of the C language? Or is there any group that performs an equivalent function? Thanks, -vs
19
2933
by: vamshi | last post by:
Hi all, This is a question about the efficiency of the code. a :- int i; for( i = 0; i < 20; i++ ) printf("%d",i); b:- int i = 10;
9
3320
by: OldBirdman | last post by:
Efficiency I've never stumbled on any discussion of efficiency of various methods of coding, although I have found posts on various forums where individuals were concerned with efficiency. I'm not concerned when dealing with user typing, but I am if a procedure is called by a query. Does the VBA compiler generate "in-line" code for some apparent function calls? For example, y = Abs(x) might be compiled as y = x & mask. The string...
0
9656
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10366
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10173
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7517
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5399
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3674
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.