Re: Adding LIMIT changes PostgreSQL plan from good to a bad one

2019-11-25 Thread Michael Korbakov
On November 22, 2019 at 20:28:39, Michael Lewis (mle...@entrata.com) wrote: I try to avoid DISTINCT and use GROUP BY when feasible, as well as avoiding OR condition. If you combined anon1 and anon2 with UNION ALL, and did (inner) join instead of left, or even moved all of that to EXISTS, perhaps t

Re: Adding LIMIT changes PostgreSQL plan from good to a bad one

2019-11-22 Thread Michael Lewis
I try to avoid DISTINCT and use GROUP BY when feasible, as well as avoiding OR condition. If you combined anon1 and anon2 with UNION ALL, and did (inner) join instead of left, or even moved all of that to EXISTS, perhaps that gives you better consistent performance. Something like this- SELECT co

Re: Adding LIMIT changes PostgreSQL plan from good to a bad one

2019-11-22 Thread Michael Korbakov
On November 21, 2019 at 19:14:33, Pavel Stehule (pavel.steh...@gmail.com) wrote: čt 21. 11. 2019 v 17:19 odesílatel Michael Korbakov napsal: > Hi everybody. > > I stumbled upon a weird problem with the query planner. I have a query > on a typical EAV schema: > > SELECT contacts.id > FROM conta

Re: Adding LIMIT changes PostgreSQL plan from good to a bad one

2019-11-21 Thread Pavel Stehule
čt 21. 11. 2019 v 22:04 odesílatel Michael Korbakov napsal: > On November 21, 2019 at 19:14:33, Pavel Stehule (pavel.steh...@gmail.com) > wrote: > > > > čt 21. 11. 2019 v 17:19 odesílatel Michael Korbakov > napsal: > >> Hi everybody. >> >> I stumbled upon a weird problem with the query planner.

Re: Adding LIMIT changes PostgreSQL plan from good to a bad one

2019-11-21 Thread Pavel Stehule
čt 21. 11. 2019 v 17:19 odesílatel Michael Korbakov napsal: > Hi everybody. > > I stumbled upon a weird problem with the query planner. I have a query > on a typical EAV schema: > > SELECT contacts.id > FROM contacts > LEFT OUTER JOIN (SELECT DISTINCT contacts_values.company_id AS > company_i