软件下载吧文章资讯

分类分类

postgreSQL 非count方法算记录数操作

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’);

展开全部

相关文章

说两句网友评论
    我要跟贴
    取消