设计具有复杂依赖性的数据库? (与计划有关)

时间:2018-07-17 03:53:03

标签: database-design scheduled-tasks

我正在寻求设计一个数据库来存储有关某些复杂依赖项的信息,并且实际上可以使用指向正确方向的点。我的Google搜索没有任何相关内容,但是也许您有一个不错的Web链接。

要提供上下文,它与作业计划有关,并将提供分析。不,我不是要创建工作计划程序。

关于数据的一些注释

  • 数据可以是递归的。作业C依赖于作业B,而作业B依赖于作业A
  • 可能存在一对多的依赖关系:作业C可能依赖于作业B1和作业B2
  • 依赖关系可能很复杂:作业C可能依赖于(作业B1或作业B2)和作业A
  • 可能存在一个令人困惑的变量...时间:作业A或上午10点

1 个答案:

答案 0 :(得分:1)

这是其中一种方法:

suggestion

CREATE TABLE job (
    id integer NOT NULL,
    title character varying NOT NULL,
    CONSTRAINT pk_job
        PRIMARY KEY (id)
);

-- simple job-job dependency, time-based dependency, complex job-job-dependency construct
CREATE TABLE dependency_type (
    id integer NOT NULL,
    title character varying NOT NULL,
    CONSTRAINT pk_dependency_type
        PRIMARY KEY (id)
);
COMMENT ON TABLE dependency_type IS 'simple job-job dependency, time-based dependency, complex job-job-dependency construct';

-- AND, OR, XOR
CREATE TABLE operator_type (
    id integer NOT NULL,
    title character varying NOT NULL,
    CONSTRAINT pk_operator_type
        PRIMARY KEY (id)
);
COMMENT ON TABLE operator_type IS 'AND, OR, XOR';

CREATE TABLE dependency (
    id integer NOT NULL,
    type_id integer NOT NULL,
    job_id integer,
    depend_on_job_id integer,
    depend_on_dependency_id integer,
    operator_type_id integer,
    condition varchar,
    CONSTRAINT pk_dependency
        PRIMARY KEY (id)
);

ALTER TABLE dependency
    ADD CONSTRAINT fk_dependency_depend_on_dependency_id
        FOREIGN KEY (depend_on_dependency_id)
        REFERENCES dependency (id) MATCH SIMPLE
        ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE dependency
    ADD CONSTRAINT fk_dependency_depend_on_job_id
        FOREIGN KEY (depend_on_job_id)
        REFERENCES job (id) MATCH SIMPLE
        ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE dependency
    ADD CONSTRAINT fk_dependency_job_id
        FOREIGN KEY (job_id)
        REFERENCES job (id) MATCH SIMPLE
        ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE dependency
    ADD CONSTRAINT fk_dependency_operator_type_id
        FOREIGN KEY (operator_type_id)
        REFERENCES poperator_type (id) MATCH SIMPLE
        ON UPDATE CASCADE ON DELETE RESTRICT;

ALTER TABLE dependency
    ADD CONSTRAINT fk_dependency_type_id
        FOREIGN KEY (type_id)
        REFERENCES dependency_type (id) MATCH SIMPLE
        ON UPDATE CASCADE ON DELETE CASCADE;

因此,对于您描述的情况:

否1。作业C依赖于作业B,而作业B依赖于作业A

INSERT INTO dependency (type_id, job_id, depend_on_job_id) VALUES (SIMPLE, JOB B, JOB A);
INSERT INTO dependency (type_id, job_id, depend_on_job_id) VALUES (SIMPLE, JOB C, JOB B);

否2。作业C可能取决于作业B1和作业B2

INSERT INTO dependency (type_id, depend_on_job_id) VALUES (SIMPLE, JOB B2);
INSERT INTO dependency (type_id, job_id, depend_on_job_id, depend_on_dependency_id, operator_type_id) VALUES (COMPLEX, JOB C, JOB B1, DEPENDENCY D1 (from previous insert), AND);

否3。作业C可能依赖于(作业B1或作业B2)和作业A

INSERT INTO dependency (type_id, depend_on_job_id) VALUES (SIMPLE, JOB B2);
INSERT INTO dependency (type_id, depend_on_job_id, depend_on_dependency_id, operator_type_id) VALUES (COMPLEX, JOB B1, DEPENDENCY D1 (from previous insert), OR);
INSERT INTO dependency (type_id, job_id, depend_on_job_id, depend_on_dependency_id, operator_type_id) VALUES (COMPLEX, JOB C, JOB A, DEPENDENCY D2 (from previous insert), AND);

否4。作业C可能取决于作业A或上午10点

INSERT INTO dependency (type_id, condition) VALUES (TIME-BASED, '10 AM');
INSERT INTO dependency (type_id, job_id, depend_on_job_id, depend_on_dependency_id, operator_type_id) VALUES (COMPLEX, JOB C, JOB A, DEPENDENCY D1 (from previous insert), OR);

此模型可能已有更新:您可能希望将depend_on_job_iddepend_on_dependency_id分成多个列(depend_on_job1_iddepend_on_job2_id等)。在某些情况下,这会减少树的深度(例如,情况否2。,然后仅用一个INSERT即可实现)。而且,当然,该模型会跳过索引,唯一键等。

此外,用SQL读取这棵树是完全不同的故事。