postgresql分区(引用)
1 建立大表。
2 创建分区继承
3 定义Rule或者Trigger?
1 建立大表
CREATE TABLE student (student_id bigserial, name varchar(32), score smallint)
2 创建分区继承
- CREATE TABLE student_qualified (CHECK (score >= 60 )) INHERITS (student) ;
- CREATE TABLE student_nqualified (CHECK (score < 60)) INHERITS (student) ;
CREATE OR REPLACE RULE insert_student_qualified
AS ON INSERT TO student
WHERE score >= 60
DO INSTEAD
INSERT INTO student_qualified VALUES(NEW.*);
CREATE OR REPLACE RULE insert_student_nqualified
AS ON INSERT TO student
WHERE score < 60
DO INSTEAD
INSERT INTO student_nqualified VALUES(NEW.*);
(2)创建方法及触发器
REATE OR REPLACE FUNCTION student_insert_trigger()
RETURNS TRIGGER AS
$$
- BEGIN
- IF(NEW.score
- INSERT
- ELSE
- INSERT
- END
- RETURN
- END;
$$
LANGUAGE plpgsql ;
//创建触发器
REATE TRIGGER insert_student
BEFORE INSERT ON student
FOR EACH row
EXECUTE PROCEDURE student_insert_trigger() ;