Greenplum是关系型的分布式数据库,需要存储的数据库在进入数据库时,将先进行数据分布的处理工作,将一个表的数据按照某种方式分到每个节点上,并为每个表指定一个分发列(distribute Column),之后根据Hash来分布数据。
本篇介绍查询某模式下所有表的分布键信息,适用于:
具体sql如下:
SELECT
aaa.nspname AS "模式名",
aaa.relname AS "表名",
aaa.table_comment AS "中文表明",
ccc.attname AS "分布键"
FROM
(
SELECT aa.oid,
obj_description (aa.oid) AS table_comment,
aa.relname,
bb.localoid,
bb.attrnums,
regexp_split_to_table (
array_to_string (bb.attrnums, ','),
','
) att,
dd.nspname FROM pg_class aa --原数据信息 最重要的表!
LEFT
JOIN gp_distribution_policy bb ON bb.localoid = aa.oid --分布键表
LEFT
JOIN pg_namespace dd ON dd.oid = aa.relnamespace --模式
LEFT
JOIN pg_inherits hh ON aa.oid = hh.inhrelid --继承表
WHERE dd.nspname = 'dim' -- 替换成需要的模式
AND hh.inhrelid IS NULL
) aaa
LEFT JOIN pg_attribute ccc ON ccc.attrelid = aaa.oid
AND ccc.attnum = aaa.att
WHERE
ccc.attnum > 0
ORDER BY
aaa.relname;
运行结果:
本篇使用GP数据库版本信息:PostgreSQL 8.2.15 (Greenplum Database 4.3.5.2 build 1)
亲们如果有更好的sql或者意见欢迎留言!
---- UE打开发现语句里面有好多符号。。。还是用下面的吧~
select aaa.nspname AS "模式名",
aaa.relname AS "表名",
aaa.table_comment AS "中文表明",
ccc.attname AS "分布键"
FROM(
SELECT aa.oid,
obj_description (aa.oid) AS table_comment,
aa.relname,
bb.localoid,
bb.attrnums,
regexp_split_to_table (
array_to_string (bb.attrnums, ','),
','
) att,
dd.nspname FROM pg_class aa --原数据信息 最重要的表!
LEFT
JOIN gp_distribution_policy bb ON bb.localoid = aa.oid --分布键表
LEFT
JOIN pg_namespace dd ON dd.oid = aa.relnamespace --模式
LEFT
JOIN pg_inherits hh ON aa.oid = hh.inhrelid --继承表
WHERE dd.nspname = 'dim' -- 替换成需要的模式
AND hh.inhrelid IS NULL
) aaa
LEFT JOIN pg_attribute ccc ON ccc.attrelid = aaa.oid
AND ccc.attnum = aaa.att
WHERE
ccc.attnum > 0
ORDER BY
aaa.relname;
-- 查找dim模式下,哪些表有user_number字段
select obj_description (aa.oid) AS table_comment,
aa.relname "表名",
attname "字段",
dd.nspname "模式"
FROM pg_class aa
LEFT JOIN pg_namespace dd ON dd.oid = aa.relnamespace --模式
LEFT JOIN pg_inherits hh ON aa.oid = hh.inhrelid --继承表
LEFT JOIN pg_attribute tt ON tt.attrelid = aa.oid
WHERE dd.nspname = 'dim' -- 替换成需要的模式
AND hh.inhrelid IS NULL
AND attname = 'user_number'
order by relname,attname ;
相关文章:【gp数据库】十条实用数据库SQL优化建议
修改分布键可参考:【gp数据库】工作中常用SQL语句干货