Greenplum經常使用命令、函數

  • Greenplum經常使用查詢命令

#查看test_bd事務(即數據庫)下的全部表名包含 user 的 表信息mysql

SELECT UPPER(A.SCHEMANAME) AS SCHEMANAME, UPPER(A.TABLENAME) AS TABLENAME,
D.ATTRELID,D.ATTRELID :: regclass,UPPER(D.ATTNAME) AS ATTNAME,
REPLACE(REPLACE(REPLACE(FORMAT_TYPE(D.ATTTYPID, D.ATTTYPMOD),'numeric','NUMBER'),
'character varying','VARCHAR2'),'date','DATE') AS DATA_TYPE,E.DESCRIPTION 
FROM PG_TABLES AS A
INNER JOIN PG_CLASS AS B ON A.TABLENAME = B.RELNAME
LEFT JOIN PG_CATALOG.PG_DESCRIPTION AS E ON B.OID = E.OBJOID
LEFT JOIN PG_CATALOG.PG_ATTRIBUTE AS D ON D.ATTRELID = E.OBJOID AND D.ATTNUM = E.OBJSUBID
WHERE SCHEMANAME ='test_bd'
AND A.TABLENAME LIKE '%user%'
AND D.ATTNUM > 0
ORDER BY A .TABLENAME,D.ATTNUM

  select pg_size_pretty(pg_database_size('gp_db'));     #查看gp數據庫大小sql

  select gp_segment_id,count(*) from db_name.tb_name group by gp_segment_id;     #查看數據分佈狀況數據庫

1.建立數據庫   createdb test_db;
2.刪除數據庫   dropdb test_db;
3.建立模式    create schema myschema;
4.刪除模式    drop schema myschema;
5.建立用戶    create user user_name with password '123456' ;
6.刪除用戶    drop user user_name;
7.查看系統用戶信息   select usename from pg_user;
8.查看版本信息     select version();
9.打開psql交互工具   psql name_db;    服務器

10.執行sql文件     mydb=> \i basics.sql \i 命令從指定的文件中讀取命令。
11.批量將文本文件中內容導入到wether表        copy weather from '/home/user/weather.txt';
12.查看搜索模式     show search_path;
13.設置搜索模式     set search_path to myschema,public;
14.建立表空間      create tablespace spacename_tb location 'file_path';
15.顯示默認表空間    show default_tablespace;
16.設置默認表空間    set default_tablespace=表空間名稱;
17.指定用戶登陸      psql mtps -u
18.顯示當前系統時間   select now() ;
19.配置plpgsql語言    create language 'plpgsql' handler plpgsql_call_handler;
20.刪除規則    drop rule name on relation [ cascade | restrict ];函數

21.當前日期屬於一年中第幾周    select extract(week from timestamp '2020-06-14');
22.查詢表是否存在    select * from pg_statio_user_tables where relname='test_tb';
23.導出表          ./pg_dump -p 端口號 -u 用戶 -t 表名稱 -f 備份文件位置 數據庫 ;
24.整個數據庫導出    pg_dumpall -d -p 端口號 -h 服務器ip -u postgres(用戶名) > /home/xiaop/all.bak
25.數據庫備份恢復    psql -h 192.168.0.48 -p 5433 -u postgres
26.數據庫備份      pg_dumpall -h 192.168.0.4 -p 5433 -u postgres >/databack/postgresql2020061401.dmp
27.當前日期函數     select current_date;
28.返回第十條開始的5條記錄    select * from tbname limit 5 offset 10;
29.查看數據庫大小    select pg_size_pretty(pg_database_size('mtps')) as fulldbsize;
30.查看數據庫表大小   select pg_size_pretty(pg_total_relation_size('test_db.t_l_collectfile')) as fulltblsize,pg_size_pretty(pg_relation_size('test_db.t_l_collectfile')) as justthetblsize;工具

31.設置執行超過指定秒數的sql語句輸出到日誌    log_min_duration_statement = 3
32.超過必定秒數sql自動執行執行計劃   shared_preload_libraries = 'auto_explain',custom_variable_classes = 'auto_explain',auto_explain.log_min_duration = 4s
33.數據庫備份
  select pg_start_backup('backup baseline');
  select pg_stop_backup();
  recovery.conf
  restore_command='cp /opt/buxlog/%f %p'post

34.數據字典查看錶結構   select column_name, data_type from information_schema.columns where table_name = 'test_tb';
35.查詢表結構         select a.attnum,a.attname as field,t.typname as type,a.attlen as length,a.atttypmod as lengthvar,a.attnotnull as notnull from pg_class c,pg_attribute a,pg_type t where c.relname=表名稱and a.attnum > 0 and a.attrelid = c.oid and a.atttypid = t.oid
36.將查詢結果直接輸出到文件,在psql中 \o 文件路徑
  select datname,rolname from pg_database a left outer join pg_roles b on a.datdba=b.oid; \o
37.查詢數據庫全部則    select datname,rolname from pg_database a left outer join pg_roles b on a.datdba=b.oid ;
38.結束正在執行的事務   select * from pg_stat_activity;
39.查看被鎖定表
    select pg_class.relname as table, pg_database.datname as database, pid, mode, granted from pg_locks, pg_class, pg_database where pg_locks.relation = pg_class.oid and pg_locks.database = pg_database.oid;
40.查看客戶端鏈接狀況     select client_addr ,client_port,waiting,query_start,current_query from pg_stat_activity;
41.常看數據庫.conf配置      show all;
  修改數據庫postgresql.conf參數
  修改postgresql.conf內容 pg_ctl reload
  回滾日誌強制恢復 pg_resetxlog -f 數據庫文件路徑ui

 

  • Greenplum經常使用命令與MySQL對比

  (1)列出全部的數據庫
    mysql: show databases
    psql: \l或\list
  (2)切換數據庫
    mysql: use dbname
    psql: \c dbnamespa

  (3)列出當前數據庫下的數據表
    mysql: show tables
    psql: \d線程

  (4)列出指定表的全部字段
    mysql: show columns from table name
    psql: \d tablename

  (5)查看指定表的基本狀況
    mysql: describe tablename
    psql: \d+ tablename

  (6)退出登陸
    mysql: quit 或者\q
    psql:\q

 

  • Greenplum經常使用調優命令

netstat -ano|grep 5432 -wc               #查看鏈接5432端口線程數
psql -d gpdatabase -h master_host -p 5432 -U gpadmin  #鏈接數據庫
psql gpdb_name                   #鏈接gpdb_name數據庫
vacuum table_name                  #清除數據庫碎片

  #查看索引的大小
  select pg_size_pretty(pg_relation_size('ind_t_id'));

  pg_restore -d test_db test_db.dump           #導入dump數據
  pg_dump -U gpadmin -Fc test_db > test.dump       #導入dump數據

  gp數據導入:
  psql -h localhost -p 5432 -d test_db -U gpadmin -c "\\copy tablename FROM '/home/user/test.csv' with DELIMITER as ',' NULL as 'null string'"
  psql -d test_db -h localhost -p dd -c "\copy(select * from ods.ods_dPmytest)to /home/gpadmin/loadout/test_db.csv"

  gp數據備份:
  gp_dump database_name;

  COPY TO  把一個表的全部內容都拷貝到一個文件,COPY只能用於表,不能用於視圖。
  COPY FROM  從一個文件裏拷貝數據到一個表裏(把數據附加到表中已經存在的內容裏)。
  COPY user_tb TO '/tmp/data/test.csv' WITH csv; 導出表數據
  COPY user_tb FROM '/tmp/data/test.csv' WITH csv; 導入表數據
  # 使用select命令
  COPY (select * from user_tb create_time > '2019-08-16 00:00:00' and create_time < '2019-08-17 00:00:00') TO '/tmp/data/user_20190816.csv' with csv;

  • Greenplum經常使用窗口函數

  row_number() over(partition by XXX order by XXX desc);    -- 生成序列號  max() over(partition by XXX order by XXX);          --取最大值  avg() over(partition by XXX order by XXX);           --取平均值

相關文章
相關標籤/搜索