Counting Rows


ANALYZE t1;
or..
VACUUM t1;
or..
REINDEX TABLE t1;
then..
SELECT reltuples FROM pg_class WHERE relname = 't1';

SELECT COUNT(*) FROM t2;


If you know the maximum value of an integer column and have an index on it:

SELECT COUNT(*) FROM t2 WHERE c2 < 1000000;


If you need to know if a table has a certain number of rows:

SELECT CASE WHEN EXISTS (SELECT 1 FROM t2 OFFSET 1000 LIMIT 1) THEN 'Yes' ELSE 'No' END;


End of presentation

This is where the actual presentation ended. Comments and questions are welcome to greg at turnstep dot com. The final few pages are just some games I put in for fun.

Previous: Deleting identical rows         [Table of Contents]         Next: Hangman