真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

GreenPlum常用SQL

 下面整理記錄一些時常會使用到的SQL,也方便以后查閱:

專注于為中小企業(yè)提供成都網(wǎng)站設(shè)計、做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)正陽免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了數(shù)千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

描述

SQL

添加分區(qū)(在沒有創(chuàng)建DEFAULT PARTITION的情況下)

alter tabletablename add partition  partition_namestart (BIGINT '1471276800000')  inclusive end (BIGINT '1471363200000')  exclusive; 

添加分區(qū)(在創(chuàng)建DEFAULT PARTITION的情況下)

ALTER TABLE tablename SPLIT  DEFAULT PARTITION

START (1471363200000) INCLUSIVE END (1471449600000) EXCLUSIVE

INTO (PARTITIONpartition_name, default partition)

修改ID序列取值的方式,提高效率

ALTER SEQUENCEtablename_seq_name CACHE  100

創(chuàng)建分區(qū)

PARTITION BY RANGE (date)

      (PARTITION Jan16 START (BIGINT '1451577600') INCLUSIVE ,

      PARTITION Feb16 START (BIGINT '1454256000') INCLUSIVE ,

      PARTITION Mar16 START (BIGINT '1456761600') INCLUSIVE ,

      PARTITION Apr16 START (BIGINT '1459440000') INCLUSIVE ,

      PARTITION May16 START (BIGINT '1462032000') INCLUSIVE ,

      PARTITION Jun16 START (BIGINT '1464710400') INCLUSIVE ,

      PARTITION Jul16 START (BIGINT '1467302400') INCLUSIVE ,

      PARTITION Aug16 START (BIGINT '1469980800') INCLUSIVE ,

      PARTITION Sep16 START (BIGINT '1472659200') INCLUSIVE ,

      PARTITION Oct16 START (BIGINT '1475251200') INCLUSIVE ,

      PARTITION Nov16 START (BIGINT '1477929600') INCLUSIVE ,

      PARTITION Dec16 START (BIGINT '1480521600') INCLUSIVE

      END (BIGINT '1483200000') EXCLUSIVE );

創(chuàng)建分區(qū)(會創(chuàng)建天粒度的N多個分區(qū)20160701到20170101,每間隔一天一個分區(qū))

PARTITION BY RANGE (TIME)(

        PARTITION  partition_nameSTART (BIGINT ‘1467302400000’) INCLUSIVE END(BIGINT ‘1483200000000’)  EXCLUSIVE EVERY(BIGINT ‘86400000’)

)

刪除分區(qū)

ALTER TABLEtablename DROP PARTITION partition_name;

刪除表分區(qū)數(shù)據(jù)

ALTER TABLEtablename TRUNCATE PARTITION  partition_name;

刪除列

ALERT TABLEtablenameDROP COLUMNcolumn_name;

刪除序列

DROP SEQUENCE serial_name

添加列

ALTER TABLEtablename ADD COLUMN column_name column_type

修改列類型

ALTER TABLEtablename ALTER COLUMNcolumn_name

TYPEnew_column_type


函數(shù)操作舉例

例子一

刪除函數(shù):  DROP FUNCTION function_name(varchar);

創(chuàng)建函數(shù)
CREATE OR REPLACE FUNCTION convert_grade(grade varchar)
  RETURNS integer AS
$BODY$  
DECLARE 
    result int; 
BEGIN 
    if grade = '優(yōu)' then result = 1;
    ELSIF grade ='良' then  result = 2;
    ELSIF grade = '中' then  result = 3 ;
    ELSIF grade = '差' then  result = 4 ;
    end if ;
    RETURN result; 
END; 
$BODY$
  LANGUAGE plpgsql VOLATILE;

例子二

---------創(chuàng)建生成RADIUS表的函數(shù),參數(shù):省份的拼音------------------
CREATE OR REPLACE FUNCTION  CREATE_RADIUS_TABLE(province varchar)
RETURNS BOOLEAN AS $$
DECLARE
 passed BOOLEAN;
BEGIN
 EXECUTE
  'CREATE TABLE JK_'||province||'_RADIUS(
      ID bigserial,
     start_time BIGINT,
     end_time   BIGINT,
     user_account VARCHAR,
     src_ip     VARCHAR,
     bras       VARCHAR
     INSERT_DATE  DATE DEFAULT NOW()
    )DISTRIBUTED BY (ID)';
    EXECUTE 'ALTER SEQUENCE JK_'||province||'_RADIUS_ID_seq CACHE 100' ; 
    RETURN passed;
END;
$$  LANGUAGE plpgsql ;


----------用于創(chuàng)建所有省的所需的表-------------
CREATE OR REPLACE FUNCTION  CREATE_REPLACE_ALL_TABLE()
RETURNS BOOLEAN AS $$
DECLARE
 passed BOOLEAN:=TRUE;
 province varchar;
BEGIN

     FOR province IN
             SELECT OBJECT_EN_NAME FROM CM_PROVINCE
     LOOP
             EXECUTE 'DROP TABLE IF EXISTS JK_'||province||'_RADIUS'; --先刪除
             PERFORM CREATE_RADIUS_TABLE(province);
      END LOOP;
      RETURN passed;
END;
$$  LANGUAGE plpgsql ;

例子三
CREATE OR REPLACE FUNCTION  CREATE_PARTITION(beginT timestamp with time zone,partitionN INTEGER)
RETURNS BOOLEAN AS $$
DECLARE
 passed BOOLEAN:=TRUE;
 pname VARCHAR ;
 smsc bigint ;
 emsc bigint ;
 beginTime timestamp with time zone;
 partitionNum INTEGER ;
BEGIN
 beginTime := to_TIMESTAMP(to_char(beginT,'yyyymmddhh34'),'yyyymmddhh34') ;
 partitionNum := partitionN;
        WHILE partitionNum > 0 LOOP
       
     --得到分區(qū)名稱
     pname := to_char(beginTime,'yyyymmddhh34');
     --開始毫秒數(shù)
     smsc = extract(epoch from beginTime)::bigint * 1000;
     --結(jié)束毫秒
     beginTime := (beginTime + interval '1 hour');
     emsc = extract(epoch from beginTime)::bigint * 1000;
     --添加分區(qū)
     EXECUTE 'ALTER TABLE'||'JK_COMPLEX_XDR'||' SPLIT DEFAULT PARTITION START ('||smsc||') INCLUSIVE
     END ('||emsc||') EXCLUSIVE INTO (PARTITION p_day_'||pname||', default partition)';

     
     partitionNum := partitionNum-1;
     RAISE INFO 'current num: %,pname: %,stime:%,etime:%',partitionNum,pname,smsc,emsc;
     
    END LOOP;
        RETURN passed;
END;
$$  LANGUAGE plpgsql ;

例四:返回多條結(jié)果集
CREATE OR REPLACE FUNCTION  get_record()
RETURNS SETOF RECORDAS $$
DECLARE
       rec RECORD;
BEGIN
        FOR rec IN
             SELECT OBJECT_NAME FROM CM_PROVINCE
       LOOP
             RETURN NEXT rec;
        END LOOP;
        RETURN;
END;
$$  LANGUAGE plpgsql ;

--執(zhí)行函數(shù)
select * from get_record() t(name varchar);

添加兩層分區(qū)舉例
create table p_test(
    ID integer,
    name varchar ,
    time bigint
)DISTRIBUTED BY (ID)
PARTITION BY LIST(name)
SUBPARTITION BY RANGE(time)
SUBPARTITION TEMPLATE
(
 SUBPARTITION sp_time_111 START (BIGINT '111') INCLUSIVE END (BIGINT '222') EXCLUSIVE ,
 default SUBPARTITION sp_time_other
)
(
  PARTITION p_name_d VALUES('d'),
  PARTITION p_name_s VALUES('s'),
  PARTITION p_name_f VALUES('f')
);

--添加主分區(qū)
alter table p_test add partition p_name_x  VALUES('x');

--修改子分區(qū)模版,子分區(qū)將按照新的模版產(chǎn)生。已經(jīng)存在的分區(qū)不會被修改
ALTER TABLE p_test SET SUBPARTITION TEMPLATE
(
 SUBPARTITION sp_time_222 START (BIGINT '222') INCLUSIVE END (BIGINT '333') EXCLUSIVE ,
 DEFAULT SUBPARTITION other
)

--查看分區(qū)情況
SELECT partitionboundary, partitiontablename,
        partitionname, partitionlevel, partitionrank
FROM pg_partitions WHERE tablename='p_test';

--查詢指定的子分區(qū)
select * from p_test subpartition(sp_time_111);


文章題目:GreenPlum常用SQL
分享地址:http://weahome.cn/article/gjhjho.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部