pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: mariusz <marius@mtvk.pl>
To: Arup Rakshit <ar@zeit.io>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: SQl help to build a result with custom aliased bool column
Date: Mon, 8 Apr 2019 17:21:05 +0200
Message-ID: <20190408172105.479079f4@marlap> (raw)
In-Reply-To: <3B9E3645-57A1-417E-83C0-6660033ABDE0@zeit.io>
References: <6AC24472-2D13-49DD-A29E-9FB41321CCEF@zeit.io>
	<CAFjNrYtsiGU4UXys0_Mu4-X+KOWqado9CrriwLRswsTexuN8fw@mail.gmail.com>
	<D83FD8F0-F056-4450-A8B1-6D032A58393C@zeit.io>
	<20190408130644.7bffa63d@marlap>
	<3B9E3645-57A1-417E-83C0-6660033ABDE0@zeit.io>

On Mon, 8 Apr 2019 19:21:37 +0530
Arup Rakshit <ar@zeit.io> wrote:

> Hi,
> 
> Thanks for showing different ways to achieve the goal. So what should
> be the optimal way to solve this. I have an composite index using
> company_id and feature_id columns for project_features table.

there are even more ways for that simple task. i can imagine some fancy
ways including lateral joins, cte returning subset of company_features
to produce positive results and be reused to produce set difference for
negative results, etc. but too fancy isn't good. the simpler the better.

those already mentioned should be enough. since you need all features
and all company_features for a given company id, there won't be any
much better.

it is enough to limit company_features to company_id which we already
do in join condition, and for big tables optimizer could use your index.

we can probably assume there won't be so much companies and so much
features to make really big table of three ids tuples to make optimizer
even consider using an index, but it may be good habit to think how we
could help optimizer to filter out unnecessary data sooner than later.

regards, mariusz

> I do ruby on rails development, where table names are plural always
> by convention. The tables I created above in different schema to ask
> question with sample data and test the query output. So they are
> little inconsistent, yes you are right.
> 
> Thanks,
> 
> Arup Rakshit
> ar@zeit.io
> 
> 
> 
> > On 08-Apr-2019, at 4:36 PM, mariusz <marius@mtvk.pl> wrote:
> > 
> > bear in mind that with a large number of companies and proper index
> > on company_features the optimizer could limit company_features as
> > necessary, while your examples read everything anyway and mangle
> > output to get proper result (with dups and bugs, but also not
> > optimal)
> 
> 
> 






view thread (7+ messages)

Message-ID: <20190408172105.479079f4@marlap>
Permalink:  ../20190408172105.479079f4@marlap/
Also on:    postgresql.org/message-id/20190408172105.479079f4@marlap

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: marius@mtvk.pl, ar@zeit.io
  Subject: Re: SQl help to build a result with custom aliased bool column
  In-Reply-To: <20190408172105.479079f4@marlap>
  References: <6AC24472-2D13-49DD-A29E-9FB41321CCEF@zeit.io> <CAFjNrYtsiGU4UXys0_Mu4-X+KOWqado9CrriwLRswsTexuN8fw@mail.gmail.com> <D83FD8F0-F056-4450-A8B1-6D032A58393C@zeit.io> <20190408130644.7bffa63d@marlap> <3B9E3645-57A1-417E-83C0-6660033ABDE0@zeit.io> <20190408172105.479079f4@marlap>

* 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