Re: [SQL] qurey plan and indices - Mailing list pgsql-sql

From Vadim Mikheev
Subject Re: [SQL] qurey plan and indices
Date
Msg-id 37AA43C4.5DDBE65F@krs.ru
Whole thread Raw
In response to Re: [SQL] qurey plan and indices  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: [SQL] qurey plan and indices
List pgsql-sql
Tom Lane wrote:
> 
> Yes.  The inner query must happen by sequential scan.  But the outer
> query can use an index if it is in the form
>         ... where (name = 'a') or (name = 'b') or (name = 'c');
> which is what "where name in ('a','b','c')" gets translated to.
> 
> However, the *real* problem here is that the inner query is treated
> as a subplan, which means it is re-evaluated for each tuple scanned
> by the outer query.  This is not very bright; the system ought to
> notice that the inner query does not depend on the state of the
> outer query, and do it only once.  There is already a notion of
> InitPlan vs. SubPlan.  I'm not sure why this case is being classified
> as a SubPlan, but it sure looks like it ought to be done as an
> InitPlan...

Not in all cases. First, to use InitPlan you would have to add 
DISTINCT to subquery to avoid duplicates. Second, there is another
way of optimization - cache/hash subquery results to avoid 
re-execution of subquery plan. Planner should make decision what
way to follow. 

Vadim


pgsql-sql by date:

Previous
From: Richard Dice
Date:
Subject: subscribe
Next
From: Bruce Momjian
Date:
Subject: Re: [SQL] qurey plan and indices