Re: Bad estimates

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Artur Zając <azajac(at)ang(dot)com(dot)pl>
Cc: pgsql-performance(at)lists(dot)postgresql(dot)org
Subject: Re: Bad estimates
Date: 2017-11-22 15:01:57
Message-ID: 12728.1511362917@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

=?iso-8859-2?Q?Artur_Zaj=B1c?= <azajac(at)ang(dot)com(dot)pl> writes:
[ poor estimates for WHERE clauses like "(gs & 1) = 1" ]

Don't hold your breath waiting for that to get better on its own.
You need to work with the planner, not expect it to perform magic.
It has no stats that would help it discover what the behavior of
that sort of WHERE clause is; nor is there a good reason for it
to think that the selectivity of such a clause is only 0.5 rather
than something more in line with the usual behavior of an equality
constraint on an integer value.

One way you could attack the problem, if you're wedded to this data
representation, is to create expression indexes on the terms "(gs & x)"
for all the values of x you use. Not only would that result in better
estimates (after an ANALYZE) but it would also open the door to satisfying
this type of query through an index search. A downside is that updating
all those indexes could make DML on the table pretty expensive.

If you're not wedded to this data representation, consider replacing that
integer flags column with a bunch of boolean columns. You might or might
not want indexes on the booleans, but in any case ANALYZE would create
stats that would allow decent estimates for "WHERE boolval".

regards, tom lane

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Alex Ignatov 2017-11-22 15:05:11 RE: Bad estimates
Previous Message Justin Pryzby 2017-11-22 14:57:13 Re: Bad estimates (DEFAULT_UNK_SEL)