Do a count on the primary key. Will force index access and you don’t access the entire row which may be very long.
LIKE : select count(ID) from my_table;
From: Mladen Gogala <gogala.mladen@gmail.com> Sent: Tuesday, July 12, 2022 11:58 AM To: MichaelDBA Vitale <michaeldba@sqlexec.com> Cc: pgsql-admin@lists.postgresql.org Subject: [EXT] Re: Improve "select count(*)" query - takes more than 30 mins for some large tables
What's wrong with parallelism? That's why it was invented. If you really need an accurate count at moment's notice, create a trigger to maintain it.
That will be a lot slower than just "select count(*) from my_table". You are delivering data to the user program (psql) and then shipping them to pipe and then processing the output with "wc". Depending on the version, PostgreSQL has very reliable parallelism and can do counting rather quickly. The speed of "select count(*) from my_table" depends on the speed of I/O. Since the table is big, it cannot be cached in the file system cache, so all that you have at your disposal is the raw disk speed. For the smaller machines, NVME is the king. For larger rigs, you should consider something like Pure, XTremIO or NetApp SolidFire. People frequently expect database to do miracles with under par hardware.