pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Yeb Havinga <yebhavinga@gmail.com>
To: Віталій Тимчишин <tivv00@gmail.com>
Cc: pgsql-performance@postgresql.org
Subject: Re: Planner not using column limit specified for one column for another column equal to first
Date: Fri, 16 Apr 2010 10:31:06 +0200
Message-ID: <4BC8204A.7050904@gmail.com> (raw)
In-Reply-To: <m2y331e40661004160102tff445f9eg165a04efe3b409ed@mail.gmail.com>
References: <m2y331e40661004160102tff445f9eg165a04efe3b409ed@mail.gmail.com>

Віталій Тимчишин wrote:
> Hello.
>
> I have a query that performs very poor because there is a limit on 
> join column that is not applied to other columns:
>
> select * from company this_ left outer join company_tag this_1_ on 
> this_.id=this_1_.company_id left outer join company_measures 
> companymea2_ on this_.id=companymea2_.company_id left outer join 
> company_descr ces3_ on this_.id=ces3_.company_id where this_1_.tag_id 
> = 7 and this_.id>50000000 
> and this_1_.company_id>50000000
> order by this_.id asc limit 1000;
>
> (plan1.txt)
> Total runtime: 7794.692 ms
>
> At the same time if I apply the limit (>50000000) to other columns in 
> query itself it works like a charm:
>
> select * from company this_ left outer join company_tag this_1_ on 
> this_.id=this_1_.company_id left outer join company_measures 
> companymea2_ on this_.id=companymea2_.company_id left outer join 
> company_descr ces3_ on this_.id=ces3_.company_id where this_1_.tag_id 
> = 7 and this_.id>50000000 
> and this_1_.company_id>50000000
> and companymea2_.company_id>50000000 and ces3_.company_id>50000000
> order by this_.id asc limit 1000;
>
> (plan2.txt)
> Total runtime: 27.547 ms
>
> I've thought and someone in this list've told me that this should be 
> done automatically.
Yes, if you have in a query a=b and b=c, then the optimizer figures out 
that a=c as well. (a,b and c are then member of the same equivalence class).

However both queries are not the same, since the joins you're using are 
outer joins. In the first it's possible that records are returned for 
company records with no matching ces3_ records, the ces3_ records is 
null in that case. In the second query no NULL ces3_ information may be 
returned.

Another thing is it seems that the number of rows guessed is far off 
from the actual number of rows, is the number 5000000 artificial or are 
you're statistics old or too small histogram/mcv's?

regards,
Yeb Havinga




view thread (12+ messages)  latest in thread

Message-ID: <4BC8204A.7050904@gmail.com>
Permalink:  ../4BC8204A.7050904@gmail.com/
Also on:    postgresql.org/message-id/4BC8204A.7050904@gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: yebhavinga@gmail.com, tivv00@gmail.com
  Subject: Re: Planner not using column limit specified for one column for another column equal to first
  In-Reply-To: <4BC8204A.7050904@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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