成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

postgresql分區(qū)表

EdwardUp / 836人閱讀

摘要:創(chuàng)建自動(dòng)分區(qū)采用兩種方式采用視圖分區(qū)方式采用直接分區(qū)方式創(chuàng)建表創(chuàng)建索引采用視圖分區(qū)方式建立視圖定義分表保證分區(qū)后的可以自增按照時(shí)間進(jìn)行分區(qū)分表觸發(fā)器定義更新更新觸發(fā)器直接分區(qū)方式分表觸發(fā)器兩種方式比較視圖分區(qū)所有操作都是對(duì)視圖的操

創(chuàng)建自動(dòng)分區(qū)采用兩種方式

采用視圖分區(qū)方式

采用直接分區(qū)方式

創(chuàng)建表
CREATE TABLE IF NOT EXISTS public.sales
(
    id bigserial primary key ,
    store_id varchar(50) ,
    business_date date,
    start_time time,
    end_time time,
    dine_in_tc int,
    delivery_tc int,
    takeout_tc int,
    dine_in_s decimal(20,4),
    delivery_s decimal(20,4),
    takeout_s decimal(20,4),
    voucher_overcharge decimal(20,4),
    freight decimal(20,4),
    currency varchar(16),
    created_at timestamp default now(),
    updated_at timestamp default now()
);
創(chuàng)建索引
CREATE INDEX sales_store_id ON public.sales (store_id);
CREATE INDEX sales_business_date ON public.sales (business_date);
ALTER TABLE public.sales  ADD CONSTRAINT sales_storeid_businessdate_starttime_endtime UNIQUE(store_id,business_date,start_time,end_time);
1.采用視圖分區(qū)方式 建立視圖
CREATE VIEW public.sales_view AS SELECT * FROM public.sales;
定義分表function
CREATE OR REPLACE FUNCTION public.insert_sales() 
RETURNS TRIGGER AS
"."$BODY"."$
DECLARE
    _start_dt text;
    _end_dt text;
    _table_name text;
BEGIN     
IF NEW.id IS NULL THEN
NEW.id := nextval("sales_id_seq");    // 保證分區(qū)后的id可以自增
END IF;
_table_name := "sales_view_" || to_char(NEW.business_date, "YYYY_MM");    // 按照時(shí)間進(jìn)行分區(qū)

PERFORM 1
FROM   pg_catalog.pg_class c
JOIN   pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE  c.relkind = "r"
AND    c.relname = _table_name
AND    n.nspname = public;
IF NOT FOUND THEN
    _start_dt := to_char(date_trunc("month", NEW.business_date), "YYYY-MM-DD");
    _end_dt:=_start_dt::timestamp + INTERVAL "1 month";
    EXECUTE 
    "CREATE TABLE public." 
    || quote_ident(_table_name) 
    || " (CHECK (business_date >= " 
    || quote_literal(_start_dt) 
    || "AND business_date < " 
    || quote_literal(_end_dt) 
    || ")) INHERITS (public.sales)";
    EXECUTE "CREATE INDEX " || quote_ident(_table_name||"_business_date"||_start_dt) || " ON public." || quote_ident(_table_name) || " (business_date)";
    EXECUTE "CREATE INDEX " || quote_ident(_table_name||"_store_id"||_start_dt) || " ON public." || quote_ident(_table_name) || " (store_id)";
    EXECUTE "ALTER TABLE public." || quote_ident(_table_name) || " ADD CONSTRAINT " || quote_ident(_table_name||"_storeid_businessdate_starttime_endtime"||_start_dt) || " UNIQUE (store_id,business_date,start_time,end_time)";
    EXECUTE "ALTER TABLE public." || quote_ident(_table_name) || " OWNER TO " || quote_ident(current_user);
    EXECUTE "GRANT ALL ON TABLE public." || quote_ident(_table_name) || " TO " || quote_ident(current_user);
END IF;
    EXECUTE "INSERT INTO public." || quote_ident(_table_name) || " VALUES ($1.*) RETURNING *" USING NEW;
    RETURN NEW;
END;
"."$BODY"."$
LANGUAGE plpgsql;
";
分表觸發(fā)器
CREATE TRIGGER insert_sales_trigger INSTEAD OF INSERT ON public.sales_view FOR EACH ROW EXECUTE PROCEDURE insert_sales();
定義更新function
CREATE OR REPLACE FUNCTION update_sales()
RETURNS TRIGGER AS $$
BEGIN
    DELETE FROM sales_view WHERE id = NEW.id;
    INSERT INTO sales_view VALUES (NEW.*);
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;
";
更新觸發(fā)器
CREATE TRIGGER update_sales_trigger INSTEAD OF UPDATE ON sales_view FOR EACH ROW EXECUTE PROCEDURE update_oc_sales();
2.直接分區(qū)方式
CREATE OR REPLACE FUNCTION insert_sales() 
RETURNS TRIGGER AS
"."$BODY"."$
DECLARE
    _start_dt text;
    _end_dt text;
    _table_name text;
BEGIN     
IF NEW.id IS NULL THEN
    NEW.id := nextval("".$this->tableName."_id_seq");  
END IF;
_table_name := "sales_" || to_char(NEW.business_date, "YYYY_MM");
PERFORM 1
FROM   pg_catalog.pg_class c
JOIN   pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE  c.relkind = "r"
AND    c.relname = _table_name
AND    n.nspname = "public";
IF NOT FOUND THEN
    _start_dt := to_char(date_trunc("month", NEW.business_date), "YYYY-MM-DD");
    _end_dt:=_start_dt::timestamp + INTERVAL "1 month";
    EXECUTE 
    "CREATE TABLE IF NOT EXISTS public." 
    || quote_ident(_table_name) 
    || " (CHECK (business_date >= " 
    || quote_literal(_start_dt) 
    || "AND business_date < " 
    || quote_literal(_end_dt) 
    || ")) INHERITS (public.sales)";
    EXECUTE "CREATE INDEX IF NOT EXISTS" || quote_ident(_table_name||"_business_date"||_start_dt) || " ON public." || quote_ident(_table_name) || " (business_date)";
    EXECUTE "CREATE INDEX IF NOT EXISTS" || quote_ident(_table_name||"_store_id"||_start_dt) || " ON public." || quote_ident(_table_name) || " (store_id)";
    EXECUTE "CREATE UNIQUE INDEX IF NOT EXISTS" || quote_ident(_table_name||"_storeid_businessdate_starttime_endtime"||_start_dt) || " ON public." || quote_ident(_table_name) || " (store_id,business_date,start_time,end_time)";
    EXECUTE "ALTER TABLE public." || quote_ident(_table_name) || " OWNER TO " || quote_ident(current_user);
    EXECUTE "GRANT ALL ON TABLE public." || quote_ident(_table_name) || " TO " || quote_ident(current_user);
END IF;
    EXECUTE "INSERT INTO public." || quote_ident(_table_name) || " VALUES ($1.*) on conflict(store_id,business_date,start_time,end_time) do nothing RETURNING *" USING NEW;
    RETURN NULL;
END;
"."$BODY"."$
LANGUAGE plpgsql;
分表觸發(fā)器
CREATE TRIGGER insert_sales_trigger BEFORE INSERT ON public.salses FOR EACH ROW EXECUTE PROCEDURE insert_sales();

兩種方式比較

視圖分區(qū)所有操作都是對(duì)視圖的操作,直接分區(qū)是對(duì)主表進(jìn)行操作;

視圖分區(qū)觸發(fā)器使用instead of,直接分區(qū)使用before,因?yàn)闊o法直接用觸發(fā)器替代對(duì)主表的操作,只能操作視圖;

視圖分區(qū)用instead of,在function中可以RETURN NEW,對(duì)數(shù)據(jù)庫操作后有明確的返回,直接分區(qū)用before方式,在function中采用RETURN NULL,數(shù)據(jù)庫操作沒有返回;

直接分區(qū)可以用on conflict對(duì)主表insert進(jìn)行ignore操作,視圖分區(qū)不能。

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/38984.html

相關(guān)文章

  • 新書推薦 |《PostgreSQL實(shí)戰(zhàn)》出版(提供樣章下載)

    摘要:作者譚峰張文升出版日期年月頁數(shù)頁定價(jià)元本書特色中國開源軟件推進(jìn)聯(lián)盟分會(huì)特聘專家撰寫,國內(nèi)多位開源數(shù)據(jù)庫專家鼎力推薦。張文升中國開源軟件推進(jìn)聯(lián)盟分會(huì)核心成員之一。 很高興《PostgreSQL實(shí)戰(zhàn)》一書終于出版,本書大體上系統(tǒng)總結(jié)了筆者 PostgreSQL DBA 職業(yè)生涯的經(jīng)驗(yàn)總結(jié),本書的另一位作者張文升擁有豐富的PostgreSQL運(yùn)維經(jīng)驗(yàn),目前就職于探探科技任首席PostgreS...

    Martin91 評(píng)論0 收藏0
  • PostgreSQL 自動(dòng)分區(qū)分表維護(hù)管理插件 pathman 基礎(chǔ)使用

    摘要:使用數(shù)據(jù)庫會(huì)自動(dòng)的根據(jù)從某幾個(gè)片中讀取數(shù)據(jù)。更加詳細(xì)的請(qǐng)參考德哥文章 官方地址:https://github.com/postgrespr...關(guān)于pathman的原理和優(yōu)化問題,請(qǐng)移步至https://yq.aliyun.com/article... 檢查環(huán)境變量如果直接執(zhí)行psql命令提示command not found則執(zhí)行下面的命令設(shè)置環(huán)境變量 root@host# PA...

    MASAILA 評(píng)論0 收藏0
  • 構(gòu)建可擴(kuò)展的PostgreSQL解決方案

    摘要:這可以通過負(fù)載平衡來實(shí)現(xiàn)數(shù)據(jù)分片當(dāng)問題不是并發(fā)查詢的數(shù)量,而是數(shù)據(jù)庫的大小和單個(gè)查詢的速度時(shí),可以實(shí)現(xiàn)不同的方法。 showImg(https://segmentfault.com/img/remote/1460000018875091); 來源 | 愿碼(ChainDesk.CN)內(nèi)容編輯 愿碼Slogan | 連接每個(gè)程序員的故事 網(wǎng)站 | http://chaindesk.cn...

    jonh_felix 評(píng)論0 收藏0
  • 構(gòu)建可擴(kuò)展的PostgreSQL解決方案

    摘要:這可以通過負(fù)載平衡來實(shí)現(xiàn)數(shù)據(jù)分片當(dāng)問題不是并發(fā)查詢的數(shù)量,而是數(shù)據(jù)庫的大小和單個(gè)查詢的速度時(shí),可以實(shí)現(xiàn)不同的方法。 showImg(https://segmentfault.com/img/remote/1460000018875091); 來源 | 愿碼(ChainDesk.CN)內(nèi)容編輯 愿碼Slogan | 連接每個(gè)程序員的故事 網(wǎng)站 | http://chaindesk.cn...

    FrozenMap 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<