Retrieve table row count from statistics rather than table scan

See https://wiki.postgresql.org/wiki/Slow_Counting
This commit is contained in:
Chris Bandy
2014-11-21 05:25:44 +00:00
parent 597428b6c5
commit 159f589c0c
3 changed files with 123 additions and 118 deletions

View File

@@ -108,7 +108,11 @@ SELECT column_name, data_type, is_nullable, character_maximum_length, character_
func (client *Client) TableInfo(table string) (*Result, error) {
return client.query(fmt.Sprintf(`
SELECT pg_size_pretty(pg_table_size('%s')) AS data_size, pg_size_pretty(pg_indexes_size('%s')) AS index_size, pg_size_pretty(pg_total_relation_size('%s')) AS total_size, (SELECT COUNT(*) FROM %s) AS rows_count`,
SELECT
pg_size_pretty(pg_table_size('%s')) AS data_size
, pg_size_pretty(pg_indexes_size('%s')) AS index_size
, pg_size_pretty(pg_total_relation_size('%s')) AS total_size
, (SELECT reltuples FROM pg_class WHERE oid = '%s'::regclass) AS rows_count`,
table, table, table, table,
))
}