探索postgresql数据库(二)

发表时间:2013-09-22 11:36 | 分类:PostgreSQL | 浏览:2,121 次
前面博主介绍了探索postgresql数据库的第一部分,下面是这章节中的第二部分。。。
 
八、查看表占用多少磁盘空间
postgres=# select pg_relation_size('accounts');
 pg_relation_size
------------------                0
以上语句查看表占用空间
postgres=# select pg_total_relation_size('accounts');
 pg_total_relation_size
------------------------                0
以上语句表包括index索引和其他相关空间的总占用空间
或者用psql的内部元命令查看。
postgres=# \dt+ accounts
                        List of relations Schema |       Name               
| Type  | Owner  | Size    | Description ------------+--------------- 
--------+--------+---------+----------+---------------- public    | 
pgbench_accounts | table   | sriggs   | 13 MB | 
 
九、哪个是最大的表
SELECT  table_name 
      ,pg_relation_size(table_name) as size 
FROM information_schema.tables 
WHERE table_schema NOT IN ('information_schema',  
      'pg_catalog')  
ORDER BY size DESC  
LIMIT 10;
这个语句按照降序显示前十个最大的表,其中where语句后面主要是排除information_schema 或者 in pg_catalog中的系统表。
 
十、一张表中有多少行
zhang=# select count(*) from world  ;
 count 
-------
     3
(1 row)
count(*)这个函数会用“Sequential Scan”扫描表中的每一个行,而且postgresql数据库现在暂时没有对这个sql进行优化,因为postgresql开发组觉得没有where限制的count(*)用处不大。
 
十一、快速估计一张表中有多少行
zhang=# SELECT (CASE WHEN reltuples > 0 THEN pg_relation_size('world')/(8192*relpages/reltuples) ELSE 0 END)::bigint AS estimated_row_count FROM pg_class             
zhang-# WHERE oid = 'world'::regclass;  
 estimated_row_count 
---------------------
                   0
(1 row)

本文标签:

本文链接:https://www.sijitao.net/1438.html

欢迎您在本博客中留下评论,如需转载原创文章请注明出处,谢谢!

一键脚本 博客历程 留言联系 文章归档 网站地图 谷歌地图
Copyright © 2010-2024 章郎虫博客 All Rights Reserved.