摘要:創(chuàng)建自動分區(qū)采用兩種方式采用視圖分區(qū)方式采用直接分區(qū)方式創(chuàng)建表創(chuàng)建索引采用視圖分區(qū)方式建立視圖定義分表保證分區(qū)后的可以自增按照時間進行分區(qū)分表觸發(fā)器定義更新更新觸發(fā)器直接分區(qū)方式分表觸發(fā)器兩種方式比較視圖分區(qū)所有操作都是對視圖的操
創(chuàng)建自動分區(qū)采用兩種方式創(chuàng)建表采用視圖分區(qū)方式
采用直接分區(qū)方式
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"); // 按照時間進行分區(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ū)所有操作都是對視圖的操作,直接分區(qū)是對主表進行操作;
視圖分區(qū)觸發(fā)器使用instead of,直接分區(qū)使用before,因為無法直接用觸發(fā)器替代對主表的操作,只能操作視圖;
視圖分區(qū)用instead of,在function中可以RETURN NEW,對數(shù)據(jù)庫操作后有明確的返回,直接分區(qū)用before方式,在function中采用RETURN NULL,數(shù)據(jù)庫操作沒有返回;
直接分區(qū)可以用on conflict對主表insert進行ignore操作,視圖分區(qū)不能。
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/38984.html
摘要:作者譚峰張文升出版日期年月頁數(shù)頁定價元本書特色中國開源軟件推進聯(lián)盟分會特聘專家撰寫,國內(nèi)多位開源數(shù)據(jù)庫專家鼎力推薦。張文升中國開源軟件推進聯(lián)盟分會核心成員之一。 很高興《PostgreSQL實戰(zhàn)》一書終于出版,本書大體上系統(tǒng)總結(jié)了筆者 PostgreSQL DBA 職業(yè)生涯的經(jīng)驗總結(jié),本書的另一位作者張文升擁有豐富的PostgreSQL運維經(jīng)驗,目前就職于探探科技任首席PostgreS...
摘要:使用數(shù)據(jù)庫會自動的根據(jù)從某幾個片中讀取數(shù)據(jù)。更加詳細的請參考德哥文章 官方地址:https://github.com/postgrespr...關(guān)于pathman的原理和優(yōu)化問題,請移步至https://yq.aliyun.com/article... 檢查環(huán)境變量如果直接執(zhí)行psql命令提示command not found則執(zhí)行下面的命令設(shè)置環(huán)境變量 root@host# PA...
摘要:這可以通過負載平衡來實現(xiàn)數(shù)據(jù)分片當問題不是并發(fā)查詢的數(shù)量,而是數(shù)據(jù)庫的大小和單個查詢的速度時,可以實現(xiàn)不同的方法。 showImg(https://segmentfault.com/img/remote/1460000018875091); 來源 | 愿碼(ChainDesk.CN)內(nèi)容編輯 愿碼Slogan | 連接每個程序員的故事 網(wǎng)站 | http://chaindesk.cn...
摘要:這可以通過負載平衡來實現(xiàn)數(shù)據(jù)分片當問題不是并發(fā)查詢的數(shù)量,而是數(shù)據(jù)庫的大小和單個查詢的速度時,可以實現(xiàn)不同的方法。 showImg(https://segmentfault.com/img/remote/1460000018875091); 來源 | 愿碼(ChainDesk.CN)內(nèi)容編輯 愿碼Slogan | 連接每個程序員的故事 網(wǎng)站 | http://chaindesk.cn...
閱讀 2893·2021-09-22 15:20
閱讀 2965·2021-09-22 15:19
閱讀 3466·2021-09-22 15:15
閱讀 2396·2021-09-08 09:35
閱讀 2382·2019-08-30 15:44
閱讀 3014·2019-08-30 10:50
閱讀 3736·2019-08-29 16:25
閱讀 1593·2019-08-26 13:55