sql 在Postgres中对表进行分区
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 在Postgres中对表进行分区相关的知识,希望对你有一定的参考价值。
CREATE TABLE price_paid_data
(
id character varying(255) NOT NULL,
price integer NOT NULL,
purchase_date date NOT NULL,
postcode character varying(7),
property_type character varying(1),
new_build character varying(1) NOT NULL DEFAULT 0,
estate_type character varying(1),
paon character varying(255),
saon character varying(255),
street character varying(255),
locality character varying(100),
town character varying(100),
district character varying(100),
county character varying(100),
status character varying(1),
outcode character varying(4),
CONSTRAINT price_paid_data_pkey1 PRIMARY KEY (id);
)
WITH (
OIDS=FALSE
);
ALTER TABLE price_paid_data
ADD CONSTRAINT price_paid_data_pkey1 PRIMARY KEY(id);
CREATE TABLE price_paid_data_y1995 ( CHECK ( purchase_date >= DATE '1990-01-01' AND purchase_date < DATE '1996-01-01' ) ) INHERITS (price_paid_data);
CREATE TABLE price_paid_data_y1996 ( CHECK ( purchase_date >= DATE '1996-01-01' AND purchase_date < DATE '1997-01-01' ) ) INHERITS (price_paid_data);
CREATE TABLE price_paid_data_y1997 ( CHECK ( purchase_date >= DATE '1997-01-01' AND purchase_date < DATE '1998-01-01' ) ) INHERITS (price_paid_data);
CREATE TABLE price_paid_data_y1998 ( CHECK ( purchase_date >= DATE '1998-01-01' AND purchase_date < DATE '1999-01-01' ) ) INHERITS (price_paid_data);
CREATE TABLE price_paid_data_y1999 ( CHECK ( purchase_date >= DATE '1999-01-01' AND purchase_date < DATE '2000-01-01' ) ) INHERITS (price_paid_data);
CREATE TABLE price_paid_data_y2000 ( CHECK ( purchase_date >= DATE '2000-01-01' AND purchase_date < DATE '2001-01-01' ) ) INHERITS (price_paid_data);
CREATE TABLE price_paid_data_y2001 ( CHECK ( purchase_date >= DATE '2001-01-01' AND purchase_date < DATE '2002-01-01' ) ) INHERITS (price_paid_data);
CREATE TABLE price_paid_data_y2002 ( CHECK ( purchase_date >= DATE '2002-01-01' AND purchase_date < DATE '2003-01-01' ) ) INHERITS (price_paid_data);
CREATE TABLE price_paid_data_y2003 ( CHECK ( purchase_date >= DATE '2003-01-01' AND purchase_date < DATE '2004-01-01' ) ) INHERITS (price_paid_data);
CREATE TABLE price_paid_data_y2004 ( CHECK ( purchase_date >= DATE '2004-01-01' AND purchase_date < DATE '2005-01-01' ) ) INHERITS (price_paid_data);
CREATE TABLE price_paid_data_y2005 ( CHECK ( purchase_date >= DATE '2005-01-01' AND purchase_date < DATE '2006-01-01' ) ) INHERITS (price_paid_data);
CREATE TABLE price_paid_data_y2006 ( CHECK ( purchase_date >= DATE '2006-01-01' AND purchase_date < DATE '2007-01-01' ) ) INHERITS (price_paid_data);
CREATE TABLE price_paid_data_y2007 ( CHECK ( purchase_date >= DATE '2007-01-01' AND purchase_date < DATE '2008-01-01' ) ) INHERITS (price_paid_data);
CREATE TABLE price_paid_data_y2008 ( CHECK ( purchase_date >= DATE '2008-01-01' AND purchase_date < DATE '2009-01-01' ) ) INHERITS (price_paid_data);
CREATE TABLE price_paid_data_y2009 ( CHECK ( purchase_date >= DATE '2009-01-01' AND purchase_date < DATE '2010-01-01' ) ) INHERITS (price_paid_data);
CREATE TABLE price_paid_data_y2010 ( CHECK ( purchase_date >= DATE '2010-01-01' AND purchase_date < DATE '2011-01-01' ) ) INHERITS (price_paid_data);
CREATE TABLE price_paid_data_y2011 ( CHECK ( purchase_date >= DATE '2011-01-01' AND purchase_date < DATE '2012-01-01' ) ) INHERITS (price_paid_data);
CREATE TABLE price_paid_data_y2012 ( CHECK ( purchase_date >= DATE '2012-01-01' AND purchase_date < DATE '2013-01-01' ) ) INHERITS (price_paid_data);
CREATE TABLE price_paid_data_y2013 ( CHECK ( purchase_date >= DATE '2013-01-01' AND purchase_date < DATE '2014-01-01' ) ) INHERITS (price_paid_data);
CREATE TABLE price_paid_data_y2014 ( CHECK ( purchase_date >= DATE '2014-01-01' AND purchase_date < DATE '2015-01-01' ) ) INHERITS (price_paid_data);
CREATE TABLE price_paid_data_y2015 ( CHECK ( purchase_date >= DATE '2015-01-01' AND purchase_date < DATE '2016-01-01' ) ) INHERITS (price_paid_data);
CREATE TABLE price_paid_data_y2016 ( CHECK ( purchase_date >= DATE '2016-01-01' AND purchase_date < DATE '2017-01-01' ) ) INHERITS (price_paid_data);
CREATE TABLE price_paid_data_y2017 ( CHECK ( purchase_date >= DATE '2017-01-01' AND purchase_date < DATE '2018-01-01' ) ) INHERITS (price_paid_data);
CREATE TABLE price_paid_data_y2018 ( CHECK ( purchase_date >= DATE '2018-01-01' AND purchase_date < DATE '2019-01-01' ) ) INHERITS (price_paid_data);
CREATE TABLE price_paid_data_y2019 ( CHECK ( purchase_date >= DATE '2019-01-01' AND purchase_date < DATE '2020-01-01' ) ) INHERITS (price_paid_data);
CREATE TABLE price_paid_data_y2020 ( CHECK ( purchase_date >= DATE '2020-01-02') ) INHERITS (price_paid_data);
CREATE INDEX primary_idx_y1995 ON price_paid_data_y1995 (id);
CREATE INDEX primary_idx_y1996 ON price_paid_data_y1996 (id);
CREATE INDEX primary_idx_y1997 ON price_paid_data_y1997 (id);
CREATE INDEX primary_idx_y1998 ON price_paid_data_y1998 (id);
CREATE INDEX primary_idx_y1999 ON price_paid_data_y1999 (id);
CREATE INDEX primary_idx_y2000 ON price_paid_data_y2000 (id);
CREATE INDEX primary_idx_y2001 ON price_paid_data_y2001 (id);
CREATE INDEX primary_idx_y2002 ON price_paid_data_y2002 (id);
CREATE INDEX primary_idx_y2003 ON price_paid_data_y2003 (id);
CREATE INDEX primary_idx_y2004 ON price_paid_data_y2004 (id);
CREATE INDEX primary_idx_y2005 ON price_paid_data_y2005 (id);
CREATE INDEX primary_idx_y2006 ON price_paid_data_y2006 (id);
CREATE INDEX primary_idx_y2007 ON price_paid_data_y2007 (id);
CREATE INDEX primary_idx_y2008 ON price_paid_data_y2008 (id);
CREATE INDEX primary_idx_y2009 ON price_paid_data_y2009 (id);
CREATE INDEX primary_idx_y2010 ON price_paid_data_y2010 (id);
CREATE INDEX primary_idx_y2011 ON price_paid_data_y2011 (id);
CREATE INDEX primary_idx_y2012 ON price_paid_data_y2012 (id);
CREATE INDEX primary_idx_y2013 ON price_paid_data_y2013 (id);
CREATE INDEX primary_idx_y2014 ON price_paid_data_y2014 (id);
CREATE INDEX primary_idx_y2015 ON price_paid_data_y2015 (id);
CREATE INDEX primary_idx_y2016 ON price_paid_data_y2016 (id);
CREATE INDEX primary_idx_y2017 ON price_paid_data_y2017 (id);
CREATE INDEX primary_idx_y2018 ON price_paid_data_y2018 (id);
CREATE INDEX primary_idx_y2019 ON price_paid_data_y2019 (id);
CREATE INDEX primary_idx_y2020 ON price_paid_data_y2020 (id);
CREATE INDEX postcode_idx_y1995 ON price_paid_data_y1995 (postcode);
CREATE INDEX postcode_idx_y1996 ON price_paid_data_y1996 (postcode);
CREATE INDEX postcode_idx_y1997 ON price_paid_data_y1997 (postcode);
CREATE INDEX postcode_idx_y1998 ON price_paid_data_y1998 (postcode);
CREATE INDEX postcode_idx_y1999 ON price_paid_data_y1999 (postcode);
CREATE INDEX postcode_idx_y2000 ON price_paid_data_y2000 (postcode);
CREATE INDEX postcode_idx_y2001 ON price_paid_data_y2001 (postcode);
CREATE INDEX postcode_idx_y2002 ON price_paid_data_y2002 (postcode);
CREATE INDEX postcode_idx_y2003 ON price_paid_data_y2003 (postcode);
CREATE INDEX postcode_idx_y2004 ON price_paid_data_y2004 (postcode);
CREATE INDEX postcode_idx_y2005 ON price_paid_data_y2005 (postcode);
CREATE INDEX postcode_idx_y2006 ON price_paid_data_y2006 (postcode);
CREATE INDEX postcode_idx_y2007 ON price_paid_data_y2007 (postcode);
CREATE INDEX postcode_idx_y2008 ON price_paid_data_y2008 (postcode);
CREATE INDEX postcode_idx_y2009 ON price_paid_data_y2009 (postcode);
CREATE INDEX postcode_idx_y2010 ON price_paid_data_y2010 (postcode);
CREATE INDEX postcode_idx_y2011 ON price_paid_data_y2011 (postcode);
CREATE INDEX postcode_idx_y2012 ON price_paid_data_y2012 (postcode);
CREATE INDEX postcode_idx_y2013 ON price_paid_data_y2013 (postcode);
CREATE INDEX postcode_idx_y2014 ON price_paid_data_y2014 (postcode);
CREATE INDEX postcode_idx_y2015 ON price_paid_data_y2015 (postcode);
CREATE INDEX postcode_idx_y2016 ON price_paid_data_y2016 (postcode);
CREATE INDEX postcode_idx_y2017 ON price_paid_data_y2017 (postcode);
CREATE INDEX postcode_idx_y2018 ON price_paid_data_y2018 (postcode);
CREATE INDEX postcode_idx_y2019 ON price_paid_data_y2019 (postcode);
CREATE INDEX postcode_idx_y2020 ON price_paid_data_y2020 (postcode);
CREATE INDEX purchase_date_idx_y1995 ON price_paid_data_y1995 (purchase_date);
CREATE INDEX purchase_date_idx_y1996 ON price_paid_data_y1996 (purchase_date);
CREATE INDEX purchase_date_idx_y1997 ON price_paid_data_y1997 (purchase_date);
CREATE INDEX purchase_date_idx_y1998 ON price_paid_data_y1998 (purchase_date);
CREATE INDEX purchase_date_idx_y1999 ON price_paid_data_y1999 (purchase_date);
CREATE INDEX purchase_date_idx_y2000 ON price_paid_data_y2000 (purchase_date);
CREATE INDEX purchase_date_idx_y2001 ON price_paid_data_y2001 (purchase_date);
CREATE INDEX purchase_date_idx_y2002 ON price_paid_data_y2002 (purchase_date);
CREATE INDEX purchase_date_idx_y2003 ON price_paid_data_y2003 (purchase_date);
CREATE INDEX purchase_date_idx_y2004 ON price_paid_data_y2004 (purchase_date);
CREATE INDEX purchase_date_idx_y2005 ON price_paid_data_y2005 (purchase_date);
CREATE INDEX purchase_date_idx_y2006 ON price_paid_data_y2006 (purchase_date);
CREATE INDEX purchase_date_idx_y2007 ON price_paid_data_y2007 (purchase_date);
CREATE INDEX purchase_date_idx_y2008 ON price_paid_data_y2008 (purchase_date);
CREATE INDEX purchase_date_idx_y2009 ON price_paid_data_y2009 (purchase_date);
CREATE INDEX purchase_date_idx_y2010 ON price_paid_data_y2010 (purchase_date);
CREATE INDEX purchase_date_idx_y2011 ON price_paid_data_y2011 (purchase_date);
CREATE INDEX purchase_date_idx_y2012 ON price_paid_data_y2012 (purchase_date);
CREATE INDEX purchase_date_idx_y2013 ON price_paid_data_y2013 (purchase_date);
CREATE INDEX purchase_date_idx_y2014 ON price_paid_data_y2014 (purchase_date);
CREATE INDEX purchase_date_idx_y2015 ON price_paid_data_y2015 (purchase_date);
CREATE INDEX purchase_date_idx_y2016 ON price_paid_data_y2016 (purchase_date);
CREATE INDEX purchase_date_idx_y2017 ON price_paid_data_y2017 (purchase_date);
CREATE INDEX purchase_date_idx_y2018 ON price_paid_data_y2018 (purchase_date);
CREATE INDEX purchase_date_idx_y2019 ON price_paid_data_y2019 (purchase_date);
CREATE INDEX purchase_date_idx_y2020 ON price_paid_data_y2020 (purchase_date);
CREATE OR REPLACE RULE price_paid_data_ignore_duplicate_inserts AS
ON INSERT TO price_paid_data
WHERE (EXISTS ( SELECT 1
FROM price_paid_data
WHERE price_paid_data.id::text = new.id::text)) DO INSTEAD NOTHING;
CREATE OR REPLACE FUNCTION ppd_insert_trigger()
RETURNS TRIGGER AS $$
BEGIN
IF ( NEW.purchase_date >= DATE '1990-01-01' AND
NEW.purchase_date < DATE '1996-01-01' ) THEN
INSERT INTO price_paid_data_y1995 VALUES (NEW.*);
ELSIF ( NEW.purchase_date >= DATE '1996-01-01' AND
NEW.purchase_date < DATE '1997-01-01' ) THEN
INSERT INTO price_paid_data_y1996 VALUES (NEW.*);
ELSIF ( NEW.purchase_date >= DATE '1997-01-01' AND
NEW.purchase_date < DATE '1998-01-01' ) THEN
INSERT INTO price_paid_data_y1997 VALUES (NEW.*);
ELSIF ( NEW.purchase_date >= DATE '1998-01-01' AND
NEW.purchase_date < DATE '1999-01-01' ) THEN
INSERT INTO price_paid_data_y1998 VALUES (NEW.*);
ELSIF ( NEW.purchase_date >= DATE '1999-01-01' AND
NEW.purchase_date < DATE '2000-01-01' ) THEN
INSERT INTO price_paid_data_y1999 VALUES (NEW.*);
ELSIF ( NEW.purchase_date >= DATE '2000-01-01' AND
NEW.purchase_date < DATE '2001-01-01' ) THEN
INSERT INTO price_paid_data_y2000 VALUES (NEW.*);
ELSIF ( NEW.purchase_date >= DATE '2001-01-01' AND
NEW.purchase_date < DATE '2002-01-01' ) THEN
INSERT INTO price_paid_data_y2001 VALUES (NEW.*);
ELSIF ( NEW.purchase_date >= DATE '2002-01-01' AND
NEW.purchase_date < DATE '2003-01-01' ) THEN
INSERT INTO price_paid_data_y2002 VALUES (NEW.*);
ELSIF ( NEW.purchase_date >= DATE '2003-01-01' AND
NEW.purchase_date < DATE '2004-01-01' ) THEN
INSERT INTO price_paid_data_y2003 VALUES (NEW.*);
ELSIF ( NEW.purchase_date >= DATE '2004-01-01' AND
NEW.purchase_date < DATE '2005-01-01' ) THEN
INSERT INTO price_paid_data_y2004 VALUES (NEW.*);
ELSIF ( NEW.purchase_date >= DATE '2005-01-01' AND
NEW.purchase_date < DATE '2006-01-01' ) THEN
INSERT INTO price_paid_data_y2005 VALUES (NEW.*);
ELSIF ( NEW.purchase_date >= DATE '2006-01-01' AND
NEW.purchase_date < DATE '2007-01-01' ) THEN
INSERT INTO price_paid_data_y2006 VALUES (NEW.*);
ELSIF ( NEW.purchase_date >= DATE '2007-01-01' AND
NEW.purchase_date < DATE '2008-01-01' ) THEN
INSERT INTO price_paid_data_y2007 VALUES (NEW.*);
ELSIF ( NEW.purchase_date >= DATE '2008-01-01' AND
NEW.purchase_date < DATE '2009-01-01' ) THEN
INSERT INTO price_paid_data_y2008 VALUES (NEW.*);
ELSIF ( NEW.purchase_date >= DATE '2009-01-01' AND
NEW.purchase_date < DATE '2010-01-01' ) THEN
INSERT INTO price_paid_data_y2009 VALUES (NEW.*);
ELSIF ( NEW.purchase_date >= DATE '2010-01-01' AND
NEW.purchase_date < DATE '2011-01-01' ) THEN
INSERT INTO price_paid_data_y2010 VALUES (NEW.*);
ELSIF ( NEW.purchase_date >= DATE '2011-01-01' AND
NEW.purchase_date < DATE '2012-01-01' ) THEN
INSERT INTO price_paid_data_y2011 VALUES (NEW.*);
ELSIF ( NEW.purchase_date >= DATE '2012-01-01' AND
NEW.purchase_date < DATE '2013-01-01' ) THEN
INSERT INTO price_paid_data_y2012 VALUES (NEW.*);
ELSIF ( NEW.purchase_date >= DATE '2013-01-01' AND
NEW.purchase_date < DATE '2014-01-01' ) THEN
INSERT INTO price_paid_data_y2013 VALUES (NEW.*);
ELSIF ( NEW.purchase_date >= DATE '2014-01-01' AND
NEW.purchase_date < DATE '2015-01-01' ) THEN
INSERT INTO price_paid_data_y2014 VALUES (NEW.*);
ELSIF ( NEW.purchase_date >= DATE '2015-01-01' AND
NEW.purchase_date < DATE '2016-01-01' ) THEN
INSERT INTO price_paid_data_y2015 VALUES (NEW.*);
ELSIF ( NEW.purchase_date >= DATE '2016-01-01' AND
NEW.purchase_date < DATE '2017-01-01' ) THEN
INSERT INTO price_paid_data_y2016 VALUES (NEW.*);
ELSIF ( NEW.purchase_date >= DATE '2017-01-01' AND
NEW.purchase_date < DATE '2018-01-01' ) THEN
INSERT INTO price_paid_data_y2017 VALUES (NEW.*);
ELSIF ( NEW.purchase_date >= DATE '2018-01-01' AND
NEW.purchase_date < DATE '2019-01-01' ) THEN
INSERT INTO price_paid_data_y2018 VALUES (NEW.*);
ELSIF ( NEW.purchase_date >= DATE '2019-01-01' AND
NEW.purchase_date < DATE '2020-01-01' ) THEN
INSERT INTO price_paid_data_y2019 VALUES (NEW.*);
ELSE
INSERT INTO price_paid_data_y2020 VALUES (NEW.*);
END IF;
RETURN NULL;
END;
$$
LANGUAGE plpgsql;
CREATE TRIGGER insert_ppd_trigger
BEFORE INSERT ON price_paid_data
FOR EACH ROW EXECUTE PROCEDURE ppd_insert_trigger();
以上是关于sql 在Postgres中对表进行分区的主要内容,如果未能解决你的问题,请参考以下文章