分类分类
2024-02-08 11:17作者:下载吧
一般方法
select count(1) from table_name;
全量扫描一遍表,记录越多,查询速度越慢
PostgreSQL 还真提供了一个这样的途径,那就是系统表 pg_class,这个系统表里头,存储着每个表的统计信息,其中 reltuples 就是对应的表的统计行,统计行的数据是pg有个独立进程,定期扫描不同的表,收集这些表的统计信息,保存在系统表里头。
select
reltuples::int as total
from
pg_class
where
relname = ‘table_name’
and relnamespace = (select oid from pg_namespace where nspname = ‘schema’);
相关文章