Postgres分区表与非分区表联接

时间:2019-11-29 09:23:21

标签: postgresql-9.4 database-partitioning

我有两个表,一个被分区,另一个没有。表的定义如下:

CREATE TABLE partition.statuschanges
(
  statuschangeid serial NOT NULL,
  username character varying(32) NOT NULL,
  taskid integer NOT NULL,
  status character varying(255) NOT NULL,
  reason character varying(255),
  CONSTRAINT pk_statuschanges_statuschangeid PRIMARY KEY (statuschangeid),
  CONSTRAINT fk_statuschanges_task FOREIGN KEY (taskid) REFERENCES partition.tasks (taskid) MATCH SIMPLE ON UPDATE CASCADE ON DELETE CASCADE,
      ON UPDATE CASCADE ON DELETE CASCADE
)

CREATE INDEX idx_statuschanges_taskid  ON partition.statuschanges  USING btree (taskid);

CREATE TABLE partition.tasks
(
  taskid serial NOT NULL,
  status character varying(255) NOT NULL,
  claimedby character varying(255),
  populateddate timestamp with time zone,
  CONSTRAINT pk_tasks_taskid PRIMARY KEY (taskid)
)

CREATE TABLE partition.tasks_active
(
  CONSTRAINT pk_tasks_taskid_active PRIMARY KEY (taskid)
  CONSTRAINT tasks_active_check1 CHECK (status::text <> ALL (ARRAY['Done'::character varying, 'Remove'::character varying]::text[]))
) INHERITS (partition.tasks)

CREATE TABLE partition.tasks_closed
(
  CONSTRAINT pk_tasks_taskid_closed PRIMARY KEY (taskid),
  CONSTRAINT tasks_active_check1 CHECK (status::text = ANY (ARRAY['Done'::character varying, 'Remove'::character varying]::text[]))
) INHERITS (partition.tasks)

当我尝试联接表时,查询计划者将忽略为StatusChange表创建的索引。

SELECT t.taskid FROM partition.tasks t, partition.statuschanges s where t.status = 'Done' and s.taskid = t.taskid

enter image description here

如何利用联接中的索引?

0 个答案:

没有答案