9个GaussDB常用的对象语句
摘要:本文介绍了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常用的对象语句的更多相关文章
- 【Hibernate 6】常用的hql语句以及N+1问题
HQL:Hibernate Query Language,是Hibernate框架中的查询语言,十分接近于SQL语言!以下介绍一些常用的Hql语句: 一.测试类 Classes类: <span ...
- 常用SQL DDL语句
常用SQL DDL语句 DDL-数据库定义语言:直接提交的.CREATE:用于创建数据库对象.DECLARE:除了是创建只在过程中使用的临时表外,DECLARE语句和CREATE语句非常相似.唯一可以 ...
- 常用经典SQL语句大全完整版--详解+实例 (存)
常用经典SQL语句大全完整版--详解+实例 转 傻豆儿的博客 http://blog.sina.com.cn/shadou2012 http://blog.sina.com.cn/s/blog_84 ...
- 常用经典SQL语句大全完整版--详解+实例 《来自网络,很全没整理,寄存与此》
常用经典SQL语句大全完整版--详解+实例 下列语句部分是Mssql语句,不可以在access中使用. SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE) DML ...
- java:Hibernate框架1(环境搭建,Hibernate.cfg.xml中属性含义,Hibernate常用API对象,HibernteUitl,对象生命周期图,数据对象的三种状态,增删查改)
1.环境搭建: 三个准备+7个步骤 准备1:新建项目并添加hibernate依赖的jar文件 准备2:在classpath下(src目录下)新建hibernate的配置文件:hibernate.cf ...
- MySQL 常用的sql语句小结(待续)
mysql 常用的sql语句 1.查看数据库各个表中的记录数 USE information_schema; SELECT table_name,table_rows FROM tables WHER ...
- Document-对象属性和常用的对象方法
Document-对象属性和常用的对象方法 对象属性 document.title //设置文档标题等价于HTML的title标签 document ...
- 测试常用SQL注入语句大全
转载自Cracer,标题:<渗透常用SQL注入语句大全>,链接http://www.xxxx.com/?p=2226 1.判断有无注入点 整形参数判断 1.直接加' 2.and 1=1 3 ...
- 经典SQL语句大全以及50个常用的sql语句
经典SQL语句大全 一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql serv ...
- SQL Server中常用的SQL语句(转):
SQL Server中常用的SQL语句 转自:http://www.cnblogs.com/rainman/archive/2013/05/04/3060428.html 1.概述 名词 笛卡尔积.主 ...
随机推荐
- 《最新出炉》系列初窥篇-Python+Playwright自动化测试-22-处理select下拉框-上篇
1.简介 在实际自动化测试过程中,我们也避免不了会遇到下拉框选择的测试,因此宏哥在这里直接分享和介绍一下,希望小伙伴或者童鞋们在以后工作中遇到可以有所帮助.今天,我们讲下playwright的下拉框怎 ...
- .NET8.0 AOT 经验分享 FreeSql/FreeRedis/FreeScheduler 均已通过测试
2023年11月15日,对.net的开发圈是一个重大的日子,.net 8.0正式版发布. 圈内已经预热了有半个月有余,性能不断超越,开发体验越来越完美,早在.net 5.0的时候就各种吹风Aot编译, ...
- Acwing 180. 排书
给定 \(n\) 本书,编号为 1∼n. 在初始状态下,书是任意排列的. 在每一次操作中,可以抽取其中连续的一段,再把这段插入到其他某个位置. 我们的目标状态是把书按照 1∼n 的顺序依次排列. 求最 ...
- AcWing 190. 字串变换
原题连接:AcWing 190. 字串变换 题意: 已知有两个字串 \(A, B\) 及一组字串变换的规则(至多 \(6\) 个规则): \(A_1→B_1\) \(A_2→B_2\) \(-\) 规 ...
- Codeforces Round #696 (Div. 2) (A~C题解)
写在前边 链接:Codeforces Round #696 (Div. 2) A. Puzzle From the Future 链接:A题链接 题目大意: 给定一个\(a\),\(b\),\(d = ...
- 【Javaweb】Servlet九 | base标签的作用【详细介绍】 Web路径相关知识
base标签的作用 导言:路径跳转 <a href="/a/b/c.html">这是a下的b下的c</a></br> <a href=&q ...
- MNIST中文手写数字数据识别
MNIST中文手写数字数据识别 实验环境 python=3.7 torch==1.13.1+cu117 torchaudio==0.13.1+cu117 torchvision==0.14.1 数据描 ...
- Diffusion Model扩散模型
1.扩散模型基本原理: 扩散模型包括两个步骤: 固定的(或预设的)前向扩散过程q:该过程会逐渐将高斯噪声添加到图像中,直到最终得到纯噪声. 2.可训练的反向去噪扩散过程pθ:训练一个神经网络,从纯噪音 ...
- 华企盾DSC半透明无法打开加密文件常见处理方法
1.查看客户端日志进程是否显示legal:1 2.半透明只支持双击打开 3.半透明进程不能设置HOOK白名单 4.检查调用的进程是否都加了 5.半透明程序的运行方式不可以以管理员启动,去掉" ...
- 浅谈数字孪生和GIS融合的必要性
随着科技的不断发展和应用的不断深入,数字孪生和GIS在各自领域中展现出巨大的潜力.然而,更引人注目的是,数字孪生和GIS的融合将为许多行业带来全新的机遇和变革.在本文中,我们将探讨数字孪生和GIS融合 ...