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;

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
DevUI开源经验分享:从0到1开始运营你的开源项目
摘要:DevUI是面向企业中后台产品的开源前端解决方案,于2019年6月正式在GitHub开源,从当时的无人问津到去年8月累计1000 Star,再到现如今上千名社区成员、上百位贡献者、达成了3000 star的成就,也只经历了短短2年的时间。 本文分享自华为云社区《从0到1开始运营你的开源项目——华为云DevUI成长经验分享》,作者:HCDG小助手。 在10月29日举行的第七届开源年会上,我们有幸邀请到了华为云DevUI团队的Kagol老师与大家分享DevUI开源的故事。从摸着石头过河到现在专注于前端组件库建设和开源社区运营,Kagol老师有太多宝贵的经验值得我们去学习。 开源年会现场Kagol老师与参与DevUI开源贡献者宋同学合影 DevUI是面向企业中后台产品的开源前端解决方案,于2019年6月正式在GitHub开源,从当时的无人问津到去年8月累计1000 Star,再到现如今上千名社区成员、上百位贡献者、达成了3000 star的成就,也只经历了短短2年的时间。 我们总结了以下4点运营经验: 1、打响知名度: 通过在掘金等开发者聚集的技术社区撰写技术文章,通过优质内容吸引关注...
- 下一篇
Docker 与 WasmEdge 合作,发布 WebAssembly 支持
在 KubeCon NA 2022 的 Cloud native Wasm day 活动上, Docker 与 CNCF 的 WasmEdge Runtime 项目发布了 Docker+Wasm 技术预览。 只需一个命令 docker compose up, 上千万的 Docker 开发者可以立即构建、共享并运行一个完整的 Wasm 应用。 Wasm 最初是作为 Web 浏览器中的安全沙箱而开发的。近年来,它在服务器端作为 VM 和 Linux 容器 (LXC) 的安全、轻量级、快速和可移植的替代方案,有了很多应用程序。Linux 容器这一领域最初由 Docker 开创。 Docker+Wasm 中的标准 demo 应用是由 Second State 提供的。这是一个数据库驱动的 Web 应用程序,它有一个用于整个 Web 服务(微服务)的 WasmEdge “容器”,以及两个用于支持服务的 Linux 容器:一个用于 MySQL 数据库,一个用于 Niginx,为前端 UI 提供静态 HTML 页面。 这三个容器在同一个网络中并排运行并形成一个应用程序。 微服务用 Rust 编写并编...
相关文章
文章评论
共有0条评论来说两句吧...