9个GaussDB常用的对象语句

gaussdb,常用,对象,语句 · 浏览次数 : 131

小编点评

#生成内容时需要带简单的排版 **示例:** ```select schemaname, relname, pg_size_pretty(table_size) as table_size, pg_size_pretty(indexes_size) as indexes_size, pg_size_pretty(total_size) as total_size, round((total_size / pg_database_size(current_database())) * 100,2) as \"percent(%)\", n_live_tup,n_dead_tup,(n_dead_tup/(n_live_tup+1)) as dead_tuple_rating from (select schemaname, relname, pg_table_size(concat(schemaname,'.',relname)) as table_size, pg_indexes_size(concat(schemaname,'.',relname)) as indexes_size, pg_total_relation_size(concat(schemaname,'.',relname)) as total_size,n_live_tup,n_dead_tup from pg_stat_user_tables) order by \"percent(%)\" desc;``` **排版说明:** 1.** `select`** 语句:用于查询所需数据 2.** `schemaname, relname, pg_size_pretty(table_size) as table_size, pg_size_pretty(indexes_size) as indexes_size, pg_size_pretty(total_size) as total_size, round((total_size / pg_database_size(current_database())) * 100,2) as \"percent(%)\", n_live_tup,n_dead_tup,(n_dead_tup/(n_live_tup+1)) as dead_tuple_rating from (** select ... ** )** 3.** `order by`** 语句:用于排序数据 **排版说明说明:** 1.** `select`** 语句:用于查询所需数据 2.** `schemaname, relname, pg_size_pretty(table_size) as table_size, pg_size_pretty(indexes_size) as indexes_size, pg_size_pretty(total_size) as total_size, round((total_size / pg_database_size(current_database())) * 100,2) as \"percent(%)\", n_live_tup,n_dead_tup,(n_dead_tup/(n_live_tup+1)) as dead_tuple_rating from (** select ... ** )** 3.** `order by`** 语句:用于排序数据 **排版说明说明:** 1.** `select`** 语句:用于查询所需数据 2.** `schemaname, relname, pg_size_pretty(table_size) as table_size, pg_size_pretty(indexes_size) as indexes_size, pg_size_pretty(total_size) as total_size, round((total_size / pg_database_size(current_database())) * 100,2) as \"percent(%)\", n_live_tup,n_dead_tup,(n_dead_tup/(n_live_tup+1)) as dead_tuple_rating from (** select ... ** )** 3.** `order by`** 语句:用于排序数据

正文

摘要:本文介绍了9个GaussDB常用的对象语句,希望对大家有帮助。

本文分享自华为云社区《GaussDB对象相关语句》,作者:酷哥。

1. 常用函数

pg_database_size() -- 数据库使用的磁盘空间。
pg_table_size() -- 表使用的磁盘空间。
pg_total_relation_size() -- 表和索引共使用的磁盘空间。
pg_indexes_size() -- 索引使用的磁盘空间。

2. 常用系统表

pg_class -- 存储数据库对象信息及其之间的关系。
pg_index -- 存储索引的一部分信息,其他的信息大多数在PG_CLASS中。    pg_namespace -- 存储schema相关的信息。
pg_database -- 存储数据库相关的信息。

3. 常用视图

pg_stat_user_tables -- 显示所有用户自定义普通表和toast表的状态信息。
pg_stat_user_indexes -- 显示数据库中用户自定义普通表和toast表的索引状态信息。

4. 常用语句

4.1查询库大小

select datname, pg_database_size(datname), pg_database_size(datname)/1024/1024/1024 as "dataSize_GB" FROM pg_database where datname not in ('template1', 'template0');

4.2查看schema的所有者

-- pg_user这个视图只有sysadmin用户有权限查,普通用户无法查询。
SELECT s.nspname schema_name,u.usename schema_owner FROM pg_namespace s, pg_user u WHERE nspname = '$schema_name' AND s.nspowner = u.usesysid;

4.3获取表结构

set current_schema to $schema;
select pg_get_tabledef('$table_name');
或
select pg_get_tabledef('$schema_name.$table_name');

示例:

select pg_get_tabledef('testschema.t1');
pg_get_tabledef
----------------------------------------
SET search_path = testschema; +
CREATE TABLE t1 ( +
id integer, +
name character varying(15) +
) +
WITH (orientation=row, compression=no)+
DISTRIBUTE BY HASH(id) +
TO GROUP group_version1;
(1 row)

4.4查询表大小

-- 查询单个确定表的大小。
select pg_table_size('$schema_name.$table_name'); -- 不包含索引,单位B
select pg_total_relation_size('$schema_name.$table_name'); -- 包含索引,单位B
-- 查询连接的数据中,所有用户表的大小
SELECT schemaname,relname, pg_total_relation_size(concat(schemaname,'.',relname))/1024/1024/1024 table_size_GB FROM PG_STAT_USER_TABLES ORDER BY 3 DESC; -- 包含索引
SELECT schemaname,relname, pg_table_size(concat(schemaname,'.',relname))/1024/1024/1024 table_size_GB FROM PG_STAT_USER_TABLES ORDER BY 3 DESC; -- 不包含索引"
SELECT table_name,pg_size_pretty(table_size) AS table_size,pg_size_pretty(indexes_size) AS indexes_size,pg_size_pretty(total_size) AS total_size FROM (SELECT table_name,pg_table_size(table_name) AS table_size,pg_indexes_size(table_name) AS indexes_size,pg_total_relation_size(table_name) AS total_size FROM (SELECT concat(table_schema,concat('.',table_name)) AS table_name FROM information_schema.tables where table_schema ilike '$schema_name') AS all_tables ORDER BY total_size DESC) AS pretty_sizes;

示例

-- 查询单个确定表的大小。
select pg_table_size('testschema.t1');
pg_table_size
---------------
17801216
(1 row)
-- 查询连接的数据中,所有用户表的大小
SELECT schemaname,relname, pg_total_relation_size(concat(schemaname,'.',relname))/1024/1024/1024 table_size_GB FROM PG_STAT_USER_TABLES ORDER BY 3 DESC; -- 包含索引
schemaname | relname | table_size_gb
------------+--------------+--------------------
testschema | t_ran2 | 2.288818359375e-05
testschema | t_ran1 | 1.52587890625e-05
testschema | t_ran3 | 1.52587890625e-05
-- 需要指定schema
SELECT table_name,pg_size_pretty(table_size) AS table_size,pg_size_pretty(indexes_size) AS indexes_size,pg_size_pretty(total_size) AS total_size FROM (SELECT table_name,pg_table_size(table_name) AS table_size,pg_indexes_size(table_name) AS indexes_size,pg_total_relation_size(table_name) AS total_size FROM (SELECT concat(table_schema,concat('.',table_name)) AS table_name FROM information_schema.tables where table_schema ilike 'testschema') AS all_tables ORDER BY total_size DESC) AS pretty_sizes;
table_name | table_size | indexes_size | total_size
-------------------------+------------+--------------+------------
testschema.t_ran2 | 24 kB | 0 bytes | 24 kB
testschema.t_ran3 | 16 kB | 0 bytes | 16 kB
testschema.t_ran1 | 16 kB | 0 bytes | 16 kB

4.5查看表的统计信息

-- 查看指定schema和table的统计信息。
select * from pg_stat_user_tables where schemaname = '$schema_name' and relname = '$table_name';
-- 查询全库的表的活跃元组数、死元组数及死元组占比。
select schemaname, relname, n_live_tup, n_dead_tup, (n_dead_tup/(n_live_tup+1)) as dead_rating from pg_stat_user_tables order by rating desc,n_dead_tup desc limit 30; 
-- 查看表大小及活跃元组、死元组、死元组比例。
select schemaname, relname, pg_size_pretty(table_size) as table_size, pg_size_pretty(indexes_size) as indexes_size, pg_size_pretty(total_size) as total_size, round((total_size / pg_database_size(current_database())) * 100,2) as "percent(%)", n_live_tup,n_dead_tup,(n_dead_tup/(n_live_tup+1)) as dead_tuple_rating from (select schemaname, relname, pg_table_size(concat(schemaname,'.',relname)) as table_size, pg_indexes_size(concat(schemaname,'.',relname)) as indexes_size, pg_total_relation_size(concat(schemaname,'.',relname)) as total_size,n_live_tup,n_dead_tup from pg_stat_user_tables) order by "percent(%)" desc;

示例:

select schemaname, relname, pg_size_pretty(table_size) as table_size, pg_size_pretty(indexes_size) as indexes_size, pg_size_pretty(total_size) as total_size, round((total_size / pg_database_size(current_database())) * 100,2) as "percent(%)", n_live_tup,n_dead_tup,(n_dead_tup/(n_live_tup+1)) as dead_tuple_rating from (select schemaname, relname, pg_table_size(concat(schemaname,'.',relname)) as table_size, pg_indexes_size(concat(schemaname,'.',relname)) as indexes_size, pg_total_relation_size(concat(schemaname,'.',relname)) as total_size,n_live_tup,n_dead_tup from pg_stat_user_tables) order by "percent(%)" desc;
schemaname | relname | table_size | indexes_size | total_size | percent(%) | n_live_tup | n_dead_tup | dead_tuple_rating
------------+--------------+------------+--------------+------------+------------+------------+------------+-------------------
testschema | t_ran2 | 24 kB | 0 bytes | 24 kB | .01 | 4 | 0 | 0
testschema | t_ran1 | 16 kB | 0 bytes | 16 kB | .01 | 3 | 0 | 0
testschema | t_ran3 | 16 kB | 0 bytes | 16 kB | .01 | 6 | 0 | 0

4.6查询数据是否倾斜

SELECT a.count,b.node_name FROM (SELECT count(*) AS count,xc_node_id FROM table_name GROUP BY xc_node_id) a, pgxc_node b WHERE a.xc_node_id=b.node_id ORDER BY a.count desc;

4.7查询给定分布键归属的DN

select * from pgxc_node where node_id = (select xc_node_id from $table where $col = $value limit 1);

示例

select * from pgxc_node where node_id = (select xc_node_id from t1 where id = 1);
node_name | node_type | node_port | node_host | node_port1 | node_host1 | hostis_primary | nodeis_primary | nodeis_preferred | node_id | sctp_port | control_port | sctp_port1 | control_po
rt1 | nodeis_central | nodeis_active
-------------------+-----------+-----------+--------------+------------+--------------+----------------+----------------+-------
dn_xxx | D | 45700 | 10.30.41.163 | 45700 | 10.30.41.163 | t | f | f | -564789568 | 45702 | 45703 | 0 |
0 | f | t

4.8查询表的主键

select pg_constraint.conname as pk_name
from pg_constraint
inner join pg_class on pg_constraint.conrelid = pg_class.oid
where pg_class.relname = '$table_name'
and pg_constraint.contype = 'p';

4.9查询事务信息

select xmin, xmax, * from $table_name;

 

点击关注,第一时间了解华为云新鲜技术~

与9个GaussDB常用的对象语句相似的内容:

9个GaussDB常用的对象语句

摘要:本文介绍了9个GaussDB常用的对象语句,希望对大家有帮助。 本文分享自华为云社区《GaussDB对象相关语句》,作者:酷哥。 1. 常用函数 pg_database_size() -- 数据库使用的磁盘空间。 pg_table_size() -- 表使用的磁盘空间。 pg_total_re

9个计算机的“网络层”知识点

摘要:网络层介于传输层和数据链路层之间,其主要作用是实现两个网络系统之间的数据透明传送,具体包括路由选择,拥塞控制和网际互连等。 本文分享自华为云社区《计算机的“网络层”究竟有哪些需要学习的?9个知识点看你是否都了解了》,作者:breakDawn 。 1.IP地址 1.1 分类表示法: 分类表示法已

开发者进阶必备的9个Tips & Tricks!

优秀的开发人员市场前景是十分广阔的,但想找到一份理想的工作,仅有代码知识是不够的。优秀的工程师应该是一个终身学习者、问题的创造性解决者,着迷于整个软件世界。要成为一名优秀的开发者,应该具备哪些品质并做出哪些努力?本文给出了一些简单的tips,除了优秀的行为习惯之外,还有一些代码工具使用的小技巧。 方

Python 潮流周刊#55:分享 9 个高质量的技术类信息源!

大家好,我是猫哥,今天给大家分享几个高质量的技术类信息源。 本文分享的信息源都是周刊类型的,所谓周刊类,就是以固定每周的频率更新,每期分享很多精华内容的链接。它的特点是信息密度极高,可以节省你去查找信息的时间,高效的学习者都会喜欢这类内容。 如果不是看了这篇文章,我猜你可能想不到 Python 竟会

[转帖]在 Linux 下 9 个有用的 touch 命令示例

https://linux.cn/article-9597-1.html touch 命令用于创建空文件,也可以更改 Unix 和 Linux 系统上现有文件时间戳。这里所说的更改时间戳意味着更新文件和目录的访问以及修改时间。 让我们来看看 touch 命令的语法和选项: 语法: # touch {

[转帖]工信部电子标准院授予阿里巴巴9个开源项目“优秀”评级

http://blog.itpub.net/31545803/viewspace-2932061/ 1月13日消息,工信部电子标准院近日公布第二批通过开源项目成熟度评估的开源项目名单,阿里巴巴主导的Flink、Dubbo、龙蜥操作系统、Seata、Nacos等多个开源项目获得“优秀”评级。 电子标准

华为云低代码技术:让矿区管理“智变”,一览无遗

摘要:基于华为低代码平台,万洲嘉智复用开发了9个数字化管理功能,成功解决了矿区管理的空区和难点,帮助煤矿园区实现了智能化管控。 本文分享自华为云社区《【云享·伙伴】第10期:华为云低代码技术:让矿区管理“智变”,一览无遗》,作者:华为云社区精选。 “路口禁止停车,请司机师傅尽快驶离路口”。坐在监控管

痞子衡嵌入式:MCUBootUtility v4.0发布,开始支持MCX啦

-- 痞子衡维护的 NXP-MCUBootUtility 工具距离上一个大版本(v3.5.0)发布过去 9 个月了,这一次痞子衡为大家带来了版本升级 v4.0.0,这个版本主要有两个重要更新需要跟大家特别说明一下。 一、v4.0更新记录 二、几个不可忽视的更新 2.1 更多MCU型号支持 v4.0

[转帖]Redis进阶实践之十五 Redis-cli命令行工具使用详解第二部分(结束)

https://www.cnblogs.com/PatrickLiu/p/8527770.html 一、介绍 今天继续redis-cli使用的介绍,上一篇文章写了一部分,写到第9个小节,今天就来完成第二部分。话不多说,开始我们今天的讲解。如果要想看第一篇文章,地址如下:http://www.cnbl

ChatGPT的原理与前端领域实践

## 一、ChatGPT 简介 ### ChatGPT的火爆 ChatGPT作为一个web应用,自22年12月发布,仅仅不到3个月的时间,月活用户就累积到1亿。在此之前,最快记录的保持者也需要9个月才达到月活1亿。 ![](https://p3-juejin.byteimg.com/tos-cn-i