joins and indexes -- a=b or b=a?

Started by will trillichabout 25 years ago2 messagesgeneral
Jump to latest
#1will trillich
will@serensoft.com

if you have a main table on which you're doing a linear scan:

create table person (
id varchar(12), -- handle/id/login
name varchar(30),
gang int4
);

that joins another indexed table:

create table gang (
id serial,
name varchar(30),
primary key(id)
);

is there any significant difference in the following two queries:

select
p.id as handle, p.name as person, g.name as gang
from
person p, gang g
where
PERSON.GANG = GANG.ID
;

-- as opposed to:

select
p.id as handle, p.name as person, g.name as gang
from
person p, gang g
where
GANG.ID = PERSON.GANG
;

the only difference is GANG.ID=PERSON.GANG versus
PERSON.GANG=GANG.ID in the WHERE clause. does it matter?

--
It is always hazardous to ask "Why?" in science, but it is often
interesting to do so just the same.
-- Isaac Asimov, 'The Genetic Code'

will@serensoft.com
http://newbieDoc.sourceforge.net/ -- we need your brain!
http://www.dontUthink.com/ -- your brain needs us!

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: will trillich (#1)
Re: joins and indexes -- a=b or b=a?

will trillich <will@serensoft.com> writes:

the only difference is GANG.ID=PERSON.GANG versus
PERSON.GANG=GANG.ID in the WHERE clause. does it matter?

No. The query planner knows about commutativity ;-)

regards, tom lane