我有一个表,其中分区按我的意图工作(1),一个没有(2):
表,在没有时区的s_date时间戳上进行分区NOT NULL
CREATE TABLE "diagnoseAW"."AWIORECORDERAWCOMMAND"
(
"ebpZone" text COLLATE pg_catalog."default" NOT NULL,
"elementName" text COLLATE pg_catalog."default" NOT NULL,
command text COLLATE pg_catalog."default" NOT NULL,
s_date timestamp without time zone NOT NULL,
i_io_value text COLLATE pg_catalog."default" NOT NULL,
source text COLLATE pg_catalog."default",
time_created timestamp with time zone,
user_created text COLLATE pg_catalog."default",
CONSTRAINT "AWIORECORDERAWCOMMAND_pkey" PRIMARY KEY ("ebpZone", "elementName", command, s_date, i_io_value)
USING INDEX TABLESPACE s1297_index
)
WITH (
OIDS = FALSE
)
TABLESPACE s1297_data;
分区看起来像这样:
CREATE TABLE partitions."AWIORECORDERAWCOMMAND_2018_02_20"
(
"ebpZone" ,
"elementName" ,
command ,
s_date ,
i_io_value ,
source ,
time_created ,
user_created ,
CONSTRAINT "AWIORECORDERAWCOMMAND_2018_02_20_NEW_pkey" PRIMARY KEY ("ebpZone", "elementName", command, s_date, i_io_value)
USING INDEX TABLESPACE s1297_data,
CONSTRAINT "AWIORECORDERAWCOMMAND_2018_02_20_s_date_check" CHECK (s_date::date = '2018-02-20'::date)
)
INHERITS ("diagnoseAW"."AWIORECORDERAWCOMMAND")
WITH (
OIDS = FALSE
)
TABLESPACE s1297_data;
执行此查询时:
SELECT "ebpZone", "elementName", command, s_date, i_io_value, source, time_created, user_created
FROM "diagnoseAW"."AWIORECORDERAWCOMMAND"
WHERE s_date::date = '2018-02-20'::date;
它工作正常,它只扫描所需的分区:
"Append (cost=0.00..3083.05 rows=610 width=48)"
" -> Seq Scan on "AWIORECORDERAWCOMMAND" (cost=0.00..0.00 rows=1 width=208)"
" Filter: ((s_date)::date = '2018-02-20'::date)"
" -> Seq Scan on "AWIORECORDERAWCOMMAND_2018_02_20" (cost=0.00..3083.05 rows=609 width=48)"
" Filter: ((s_date)::date = '2018-02-20'::date)"
表格," startDate"带时区的时间戳NOT NULL
CREATE TABLE "diagnoseAW"."AWIORECORDERMOVEMENT"
(
event text COLLATE pg_catalog."default",
"ebpZone" text COLLATE pg_catalog."default" NOT NULL,
"awName" text COLLATE pg_catalog."default" NOT NULL,
"startDate" timestamp with time zone NOT NULL,
"lossDate" timestamp with time zone NOT NULL,
"controlDate" timestamp with time zone NOT NULL,
"startDuration" numeric,
"totalDuration" numeric,
time_created timestamp with time zone,
user_created text COLLATE pg_catalog."default",
"lossEnd" boolean,
"controlEnd" boolean,
railtemp double precision,
airtemp double precision,
CONSTRAINT "AWIORECORDERMOVEMENT_pkey" PRIMARY KEY ("ebpZone", "awName", "startDate", "lossDate", "controlDate")
USING INDEX TABLESPACE s1297_index
)
WITH (
OIDS = FALSE
)
TABLESPACE s1297_data;
分区看起来像这样:
CREATE TABLE partitions."AWIORECORDERMOVEMENT_2018_02_20"
(
event ,
"ebpZone" ,
"awName" ,
"startDate" ,
"lossDate" ,
"controlDate" ,
"startDuration" ,
"totalDuration" ,
time_created ,
user_created ,
"lossEnd" ,
"controlEnd" ,
railtemp ,
airtemp ,
CONSTRAINT "AWIORECORDERMOVEMENT_2018_02_20_NEW_pkey" PRIMARY KEY ("ebpZone", "awName", "startDate", "lossDate", "controlDate")
USING INDEX TABLESPACE s1297_data,
CONSTRAINT "AWIORECORDERMOVEMENT_2018_02_20_startDate_check" CHECK ("startDate"::date = '2018-02-20'::date)
)
INHERITS ("diagnoseAW"."AWIORECORDERMOVEMENT")
WITH (
OIDS = FALSE
)
TABLESPACE s1297_data;
执行此查询时:
SELECT event, "ebpZone", "awName", "startDate", "lossDate", "controlDate", "startDuration", "totalDuration", time_created, user_created, "lossEnd", "controlEnd", railtemp, airtemp
FROM "diagnoseAW"."AWIORECORDERMOVEMENT"
WHERE "startDate"::date='2018-02-20'::date;
它不能正常工作,它会扫描所有分区:
"Append (cost=0.00..1386483.44 rows=241434 width=80)"
" -> Seq Scan on "AWIORECORDERMOVEMENT" (cost=0.00..0.00 rows=1 width=242)"
" Filter: (("startDate")::date = '2018-02-20'::date)"
" -> Seq Scan on "AWIORECORDERMOVEMENT_2017_01_02" (cost=0.00..2566.48 rows=446 width=80)"
" Filter: (("startDate")::date = '2018-02-20'::date)"
" -> Seq Scan on "AWIORECORDERMOVEMENT_2017_01_07" (cost=0.00..1588.55 rows=276 width=80)"
" Filter: (("startDate")::date = '2018-02-20'::date)"
" -> Seq Scan on "AWIORECORDERMOVEMENT_2017_01_09" (cost=0.00..3137.61 rows=546 width=80)"
" Filter: (("startDate")::date = '2018-02-20'::date)"
" -> Seq Scan on "AWIORECORDERMOVEMENT_2017_01_12" (cost=0.00..3279.47 rows=569 width=80)"
" Filter: (("startDate")::date = '2018-02-20'::date)"
" -> Seq Scan on "AWIORECORDERMOVEMENT_2017_01_13" (cost=0.00..3390.38 rows=589 width=80)"
" Filter: (("startDate")::date = '2018-02-20'::date)"
" -> Seq Scan on "AWIORECORDERMOVEMENT_2017_01_16" (cost=0.00..3139.07 rows=546 width=80)"
" Filter: (("startDate")::date = '2018-02-20'::date)"
...
我看到的唯一区别是我用于分区的时间戳中有或没有时间戳。 (另外在第二种情况下,分区有索引,不是第一种情况,但我认为它不相关) 任何想法如何我不能扫描所有我的分区CASE(2)的查询?我最好不会重新加载所有数十亿行。