zoukankan      html  css  js  c++  java
  • Postgresql

    CREATE TABLE if not exists "o"."A"
    (
        --编号
        "Number" bigint NOT NULL,
         --……
         --创建时间
        "CreateTime" timestamp without time zone NOT NULL DEFAULT now(),
        --更新时间
        "UpdateTime" timestamp without time zone NOT NULL DEFAULT now()
    )
    partition by range("CreateTime");
    
    CREATE UNIQUE INDEX IF NOT EXISTS u_idx_AdsARH_CT_N
    ON "o"."A"("CreateTime","Number");
    CREATE INDEX if not exists idx_AdsARH_AdsCId_AdsAId 
    ON "orders"."A" USING btree ("CId","AId");
    
    
    --add partition
    DO 
    $do$
    DECLARE
    	r_year record;
    	r_month record;
        v_year integer;
        v_month integer;
        v_startData date;
    	v_endData date;
        str_sql text;
        str_next_month text;
        v_schema text:='o';
        v_table_preffix_name text:='A';
    	v_years int[]:= '{2015,2016,2017,2018,2019,
        2020,2021,2022,2023,2024,2025,2026,2027,2028,2029,
        2030,2031,2032,2033,2034,2035,2036,2037,2038,2039}';
        v_months int[]:='{1,2,3,4,5,6,7,8,9,10,11,12}';
    BEGIN
    
       for r_year in (
    		WITH data AS (
    			SELECT v_years::text[] AS arr
    		)
    		SELECT arr[i] AS "v_year"
    			FROM data, generate_subscripts((SELECT arr FROM data), 1) i
    	)
    	LOOP
    		RAISE NOTICE 'start.... ';
    		v_year=CAST(coalesce(r_year."v_year", '0') AS integer);	
    		RAISE NOTICE 'v_year: % ', v_year;
            continue when v_year is null;
    
    		for r_month in (
                WITH data AS (
                    SELECT v_months::int[] AS arr
                )
                SELECT arr[i] AS "v_month"
                    FROM data, generate_subscripts((SELECT arr FROM data), 1) i
            )
            LOOP
                v_month= CAST(coalesce(r_month."v_month", '0') AS integer);
                RAISE NOTICE 'v_month: % ', v_month;
                continue when v_month is null;
    
                v_startData=to_date(concat(v_year,v_month), 'yyyymm');
                RAISE NOTICE 'v_startData: %,%,%', 
    			v_startData,to_char(v_startData,'YYYYMMDD'),to_char(v_startData,'YYYY-MM-DD');
                v_endData=v_startData + interval '1 month';
                RAISE NOTICE 'v_endData: %,%', v_endData,to_char(v_endData,'YYYY-MM-DD');
                continue when v_startData is null;
                continue when v_endData is null;
                
    			str_sql = format('create table if not exists  "%I".%I 
                partition of "%I".%I 
                for values from (''%I'') to(''%I'')', 
                v_schema, concat(v_table_preffix_name,'_',to_char(v_startData,'YYYYMMDD')),
    			v_schema,v_table_preffix_name,
                v_startData,v_endData);
                RAISE NOTICE 'str_sql: % ', str_sql;
    			
    			execute str_sql;
    
            END LOOP;
    		str_sql = format('create table if not exists  "%I".%I 
                partition of "%I".%I 
                DEFAULT', 
                v_schema, concat(v_table_preffix_name,'_','default'),
    			v_schema,v_table_preffix_name);
            RAISE NOTICE 'str_sql: % ', str_sql;
            execute str_sql;
    		RAISE NOTICE 'end.... ';
    	END LOOP;
    END;
    $do$;	
    

      

    建立分区表的时候(Postgresql 不建议建立主键,这样会引起以后必要的麻烦,如果需要可以建立唯一索引)

    主键约束或唯一约束必须包含分区字段。这样才能确保整个分区表内的唯一性,因为每个分区上的唯一约束只维护自身的唯一性。

  • 相关阅读:
    JS——ajax login test
    Java——Java日期转Sql日期
    JDK动态代理实现原理
    Java 动态代理机制分析及扩展,第 1 部分
    Java枚举类
    Java强引用、 软引用、 弱引用、虚引用
    取模运算
    java集合框架
    字节和unicode
    编译原理随笔
  • 原文地址:https://www.cnblogs.com/panpanwelcome/p/14343627.html
Copyright © 2011-2022 走看看