pg.ddx.io pgsql-admin@postgresql.org mailing list archive
help / color / mirror / Atom feedIN subselects and index / seq_scan question....
2+ messages / 2 participants
[nested] [flat]
* IN subselects and index / seq_scan question....
@ 2002-11-19 00:28 Marc Mitchell <marcm@eisolution.com>
2002-11-19 03:43 ` Re: IN subselects and index / seq_scan question.... Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 2+ messages in thread
From: Marc Mitchell @ 2002-11-19 00:28 UTC (permalink / raw)
To: pgsql-admin
I am having an optimization problem with queries that include IN clauses
and subselects.
xxx=> select version();
version
-------------------------------------------------------------
PostgreSQL 7.2.1 on i686-pc-linux-gnu, compiled by GCC 2.96
The basic query I have is:
UPDATE my_table
SET some_col=some_value
WHERE my_table.foreign_key_with_an_index IN (
SELECT primary_key
FROM my_other_table
WHERE my_other_table.some_indexed_column = some_other_value);
The problem is that even though <my_table> has an index on
<my_table.foreign_key_with_an_index>, an EXPLAIN reports and performance
supports the fact that the server performs a sequential scan on my_table
instead of an indexed scan on the column. This is in contrast to the
query:
UPDATE my_table
SET some_col=some_value
WHERE my_table.foreign_key_with_an_index IN (val1,val2,val3...)
which will show an indexed scan and accordingly good performance.
Having done digging within the online user docs, including
http://www.ca.postgresql.org/docs/faq-english.html#4.22, I'm not sure
whether this is an issue of Postgres always wanting to seq_scan when faced
with IN clauses and subselects or if I have a stats problem where the
optimizer thinks my subselect will return hundreds of rows (even though I
know it's never more than 4) and thus opts for a single seq rather than
100s of random page accesses to go from index to base table.
If it is the former, then I've got an issue with the optimizers
capabilibilities and will be looking for info that something within a later
release will makes things better.
If it is the latter, I need to continue my learning journey into the
optimizer, Analyze and statistics.
Can someone shed light as to which path I should be following?
Marc Mitchell - Senior Application Architect
Enterprise Information Solutions, Inc.
Downers Grove, IL 60515
marcm@eisolution.com
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: IN subselects and index / seq_scan question....
2002-11-19 00:28 IN subselects and index / seq_scan question.... Marc Mitchell <marcm@eisolution.com>
@ 2002-11-19 03:43 ` Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 2+ messages in thread
From: Tom Lane @ 2002-11-19 03:43 UTC (permalink / raw)
To: Marc Mitchell <marcm@eisolution.com>; +Cc: pgsql-admin
"Marc Mitchell" <marcm@eisolution.com> writes:
> I am having an optimization problem with queries that include IN clauses
> and subselects.
The optimizer currently has no clue at all about dealing with
"IN (subselect)", and always converts it into the worst sort of
nested-loop plan. I'm hoping to do something about that for 7.4, but
it's only pie-in-the-sky today. In the meantime, the conventional
wisdom is to convert your query into an EXISTS() test instead of an IN()
test. This is still nested-loop, but not as bad --- the clause you push
down into the EXISTS() has a shot at using an index. I believe the
details are in the FAQ.
regards, tom lane
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2002-11-19 03:43 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-11-19 00:28 IN subselects and index / seq_scan question.... Marc Mitchell <marcm@eisolution.com>
2002-11-19 03:43 ` Tom Lane <tgl@sss.pgh.pa.us>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox