cYn
cYn

Reputation: 3381

PostgreSQL - Does doing analyze have the same performance cost as count(*)?

To start off, I want to do an estimated count of how large a table is. Since I am building an analytical data of the database using graphs, exact count is not important. Thus I came across this wiki and it suggested doing an estimated count using

SELECT reltuples FROM pg_class WHERE relname = 'table_name';

Now in order to get an updated count, we would have to do an analyze on that table.

So my question is that is using analyze to get an updated count of the reltuples the same thing as doing count(*)? Is there a performance hit on doing analyze as much as doing count(*)?

Upvotes: 0

Views: 120

Answers (1)

jjanes
jjanes

Reputation: 44147

The cost of running analyze depends on how much of the table is sampled, which depends on the default_statistics_target or the per-column statistics setting. It could be either faster or slower than count(*) depending on your specifics--there is not replacement for actually trying and seeing on your own system with our own data.

But normally if you are happy with an estimate, you would just use the reltuples you find there already. re-analyzing the table each time would seem to defeat the purpose.

Upvotes: 1

Related Questions