Re: Optimizing count(), but Explain estimates wildly off

2024-02-26 Thread Laurenz Albe
On Mon, 2024-02-26 at 18:25 -0600, Chema wrote: > I'm trying to optimize simple queries on two tables (tenders & items) with a > couple > million records.  Besides the resulting records, the app also displays the > count of > total results.  Doing count() takes as much time as the other query (wh

Re: Optimizing count(), but Explain estimates wildly off

2024-02-26 Thread Vitalii Tymchyshyn
Is your transaction id more or less monotonic according to the date? If so, something like the next can help: with tenders_filtered as (select * from tenders where country = 'Mexico' and "date" >= '2023-01-01' and "date" < '2024-01-01') Select * from tenders_filtered inner join items on transactio

Optimizing count(), but Explain estimates wildly off

2024-02-26 Thread Chema
Dear pgsqlers, I'm trying to optimize simple queries on two tables (tenders & items) with a couple million records. Besides the resulting records, the app also displays the count of total results. Doing count() takes as much time as the other query (which can be 30+ secs), so it's an obvious tar