PostgreSQL里实用sql

1、统计一个库里面含有某个类型的的全部表信息

select c.relname, ATTNAME, ATTTYPID, TYPNAME from sys_attribute a, sys_class c, sys_namespace n, sys_type t where a.attrelid = c.oid and a.attnum > (长度,根据实际指定) and c.relnamespace = n.oid and nspname = 'public' and a.atttypid = t.oid and typname = '需要统计的类型' and c.relkind = 'r';

2、批量统计表数量
SELECT 'select count(*) from '||table_name||' union all' FROM information_schema.tables WHERE table_schema='cma_am' order by table_name asc;

3、统计重复索引
SELECT relname,(array_agg(idx))[1] idx1,pg_get_indexdef((array_agg(idx))[1]) idx1_def,(array_agg(idx))[2] idx2,pg_get_indexdef((array_agg(idx))[2]) idx2_def,(array_agg(idx))[3] idx3,pg_get_indexdef((array_agg(idx))[3]) idx3_def FROM (SELECT indrelid::regclass AS relname,indexrelid::regclass AS idx,(indrelid::text || indclass::text || indkey::text || COALESCE(indexprs::text, '') || COALESCE(indpred::text, '')) AS KEY FROM pg_index) sub GROUP BY relname, KEY HAVING count(*) > 1;

4、查询单个索引占据空间大小

select indexrelname,

pg_size_pretty(pg_relation_size(indexrelid))

from pg_stat_user_indexes

where schemaname='public'  order by pg_relation_size(indexrelid) desc;

5、查询某个表上所有索引大小总和

select pg_size_pretty(pg_indexes_size('索引所在的表名'));


6、查看某个模式下表的数量

SELECT count(tablename) FROM sys_tables WHERE tablename NOT LIKE 'sys%' AND tablename NOT LIKE 'sql_%';


7、查看特定表中索引数量

select count(1) from user_indexes t where t.table_name like 'DT%' or t.table_name like 'BUDGET%';

8、查看本地连接数:
ksql中执行语句select count(*) from sys_stat_activity where client_addr='127.0.0.1';

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值