Greenplum使用簡明手冊sql
GP服務啓停
su - gpadmin數據庫
gpstart #正常啓動session
gpstop #正常關閉spa
gpstop -M fast #快速關閉命令行
gpstop –r #重啓日誌
gpstop –u #從新加載配置文件索引
登錄與退出Greenplum
#正常登錄隊列
psql gpdbip
psql -d gpdb -h gphostm -p 5432 -U gpadminit
#使用utility方式
PGOPTIONS="-c gp_session_role=utility" psql -h -d dbname hostname -p port
#退出
在psql命令行執行\q
參數查詢
psql -c 'SHOW ALL;' -d gpdb
gpconfig --show max_connections
建立數據庫
createdb -h localhost -p 5432 dhdw
建立GP文件系統
# 文件系統名
gpfsdw
# 子節點,視segment數建立目錄
mkdir -p /gpfsdw/seg1
mkdir -p /gpfsdw/seg2
chown -R gpadmin:gpadmin /gpfsdw
# 主節點
mkdir -p /gpfsdw/master
chown -R gpadmin:gpadmin /gpfsdw
gpfilespace -o gpfilespace_config
gpfilespace -c gpfilespace_config
建立GP表空間
psql gpdb
create tablespace TBS_DW_DATA filespace gpfsdw;
SET default_tablespace = TBS_DW_DATA;
刪除GP數據庫
gpdeletesystem -d /gpmaster/gpseg-1 -f
查看segment配置
select * from gp_segment_configuration;
文件系統
select * from pg_filespace_entry;
磁盤、數據庫空間
SELECT * FROM gp_toolkit.gp_disk_free ORDER BY dfsegment;
SELECT * FROM gp_toolkit.gp_size_of_database ORDER BY sodddatname;
日誌
SELECT * FROM gp_toolkit.__gp_log_master_ext;
SELECT * FROM gp_toolkit.__gp_log_segment_ext;
表描述
/d+ <tablename>
表分析
VACUUM ANALYZE tablename;
表數據分佈
SELECT gp_segment_id, count(*) FROM <table_name> GROUP BY gp_segment_id;
表佔用空間
SELECT relname as name, sotdsize/1024/1024 as size_MB, sotdtoastsize as toast, sotdadditionalsize as other
FROM gp_toolkit.gp_size_of_table_disk as sotd, pg_class
WHERE sotd.sotdoid = pg_class.oid ORDER BY relname;
索引佔用空間
SELECT soisize/1024/1024 as size_MB, relname as indexname
FROM pg_class, gp_toolkit.gp_size_of_index
WHERE pg_class.oid = gp_size_of_index.soioid
AND pg_class.relkind='i';
OBJECT的操做統計
SELECT schemaname as schema, objname as table, usename as role, actionname as action, subtype as type, statime as time
FROM pg_stat_operations
WHERE objname = '<name>';
鎖
SELECT locktype, database, c.relname, l.relation, l.transactionid, l.transaction, l.pid, l.mode, l.granted, a.current_query
FROM pg_locks l, pg_class c, pg_stat_activity a
WHERE l.relation=c.oid
AND l.pid=a.procpid
ORDER BY c.relname;
隊列
SELECT * FROM pg_resqueue_status;
加載(LOAD)數據到Greenplum數據庫
gpfdist外部表
# 啓動服務
gpfdist -d /share/txt -p 8081 –l /share/txt/gpfdist.log &
# 建立外部表,分隔符爲’/t’
drop EXTERNAL TABLE TD_APP_LOG_BUYER;
CREATE EXTERNAL TABLE TD_APP_LOG_BUYER (
IP text,
ACCESSTIME text,
REQMETHOD text,
URL text,
STATUSCODE int,
REF text,
name text,
VID text)
LOCATION ('gpfdist://gphostm:8081/xxx.txt')
FORMAT 'TEXT' (DELIMITER E'/t'
FILL MISSING FIELDS) SEGMENT REJECT LIMIT 1 percent;
# 建立普通表
create table test select * from TD_APP_LOG_BUYER;
# 索引
# CREATE INDEX idx_test ON test USING bitmap (ip);
# 查詢數據
select ip , count(*) from test group by ip order by count(*);
gpload
# 建立控制文件
# 加載數據
gpload -f my_load.yml
copy
COPY country FROM '/data/gpdb/country_data'
WITH DELIMITER '|' LOG ERRORS INTO err_country
SEGMENT REJECT LIMIT 10 ROWS;
從Greenplum數據庫卸載(UNLOAD)數據
gpfdist外部表
# 建立可寫外部表
CREATE WRITABLE EXTERNAL TABLE unload_expenses
( LIKE expenses )
LOCATION ('gpfdist://etlhost-1:8081/expenses1.out',
'gpfdist://etlhost-2:8081/expenses2.out')
FORMAT 'TEXT' (DELIMITER ',')
DISTRIBUTED BY (exp_id);
# 寫權限
GRANT INSERT ON writable_ext_table TO <name>;
# 寫數據
INSERT INTO writable_ext_table SELECT * FROM regular_table;
copy
COPY (SELECT * FROM country WHERE country_name LIKE 'A%') TO '/home/gpadmin/a_list_countries.out';
執行sql文件
psql gpdbname –f yoursqlfile.sql
或者psql登錄後執行
\i yoursqlfile.sql