Kouber Saparev <kouber@xxxxxxxxxxx> writes: > Now the planner believes there're 910 rows, which is a bit closer to the > real data: > swing=# select avg(length) from (select username, count(*) as length > from login_attempt group by username) as freq; > avg > ---------------------- > 491.6087310427555479 > (1 row) Hmph, that's still not real good. Ideally it should be estimating *less* than the average frequency, because the estimate is made after excluding all the most-common-values, which evidently 'kouber' is not one of. I suppose there's quite a large number of infrequently-seen usernames and the ndistinct estimate is much less than reality? (Look at the pg_stats row for this column.) It might be worth going all the way to stats target 1000 for this column. regards, tom lane -- Sent via pgsql-performance mailing list (pgsql-performance@xxxxxxxxxxxxxx) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance