Re: Improve "select count(*)" query - takes more than 30 mins for some large tables

From: Florents Tselai <florents(dot)tselai(at)gmail(dot)com>
To: Raj kumar <rajkumar820999(at)gmail(dot)com>
Cc: Pgsql-admin <pgsql-admin(at)lists(dot)postgresql(dot)org>
Subject: Re: Improve "select count(*)" query - takes more than 30 mins for some large tables
Date: 2022-07-11 07:23:01
Message-ID: 1B1927BA-F7DE-40E8-8F67-BCC001CD4C6B@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

> On 11 Jul 2022, at 10:16 AM, Raj kumar <rajkumar820999(at)gmail(dot)com> wrote:
>
> Hi,
>
> How can I improve "select count(*)" for larger tables? I'm doing a db migration and need to validate the data count.
> "select count(*) " queries are taking more than 30 minutes for some tables which is more than the downtime we have.
> Will work_mem increase help? or how can i speed up this row count?

Personally, whenever I’ve had slow count(*) or count (distinct id), eventually
I’ve resorted to Unix tools.

psql “select id from my_table" | sort -u | wc -l

The convenience/performance tradeoff depends heavily on your schema.
After all unix streams don’t know much about your integrity requirements.

>
> Thanks,
> Raj

In response to

Responses

Browse pgsql-admin by date

  From Date Subject
Next Message Holger Jakobs 2022-07-11 08:33:41 Re: Improve "select count(*)" query - takes more than 30 mins for some large tables
Previous Message Raj kumar 2022-07-11 07:16:23 Improve "select count(*)" query - takes more than 30 mins for some large tables