Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is count(*) taking extremely long in one PostgreSQL database but not another?

I have two Postgres databases. In one I have two tables, each with about 8,000,000 rows, and a count on either of them takes about a second. In another database, also Postgres, there are tables that are 1,000,000 rows, and a count takes 10s, and one table thats about 6,000,000 rows, and count takes 3min to run. What factors determine how long this will take? They are on different machines, but the database that takes longer is on a faster machine.

I've read about how postgres count is slow in general, but this seems odd to me. I can't really use a workaround, because I am using django, and it does a count in the admin, which is taking forever and making it dificult to use.

Any information on this would be helpful.

like image 324
Stephen Avatar asked Jun 28 '10 20:06

Stephen


1 Answers

Speed of counting depends not just on the number of rows in the table but on the time taken to read the data from disk. The time depends on many things:

  • Number of rows in the table - as you already mentioned.
  • The number of records per page (if each record takes more space you need to read more pages to read the same number of rows).
  • If pages are only partly full you have to read more pages.
  • If the tables is already cached in memory (having more memory available helps here).
  • If the table is indexed with a small index (the index can be counted instead).
  • Hardware differences.
  • etc....
like image 57
Mark Byers Avatar answered Sep 27 '22 23:09

Mark Byers