Postgres中的长查询

时间:2016-06-30 14:00:21

标签: performance postgresql select

大家好。我有两个版本的查询。它仅与赋值运算符不同。当带有“IS NOT DISTINCT FROM”的查询超过10分钟时,带有'='的查询需要1.6秒才能执行。如何优化第二个查询?

尽管持续时间,还有另一个问题。第二个查询返回比第一个查询更多的元组。分析返回的元组数量,我发现查询B正常工作。但是,如果条件“IS NOT DISTINCT FROM”比条件“=”更严格,而不是为什么它返回比查询A更多的元组?

查询A:

INSERT INTO observations_optical(note_1,date,"RA","Dec_degree","Dec",magnitude,band,id_obser vatory,id_mpp)
SELECT note_1,date,"RA","Dec_degree","Dec",magnitude,band,observatories.id_observatory,mp_physical.id_mpp
FROM observations_comet_optical
JOIN mp_physical ON (mp_physical.designation =observations_comet_optical.mpp_designation AND mp_physical.periodic_number =observations_comet_optical.periodic_number)
JOIN observatories ON observatories.observatory_code= observations_comet_optical.observatory_code
WHERE mp_physical.mp_type='C';

对于第一个查询,我有执行计划:query A EXPLAIN
查询B:

INSERT INTO observations_optical(note_1,date,"RA","Dec_degree","Dec",magnitude,band,id_observatory,id_mpp)
SELECT note_1,date,"RA","Dec_degree","Dec",magnitude,band,observatories.id_observatory,mp_physical.id_mpp
FROM observations_comet_optical
JOIN mp_physical ON (mp_physical.designation IS NOT DISTINCT FROM observations_comet_optical.mpp_designation AND mp_physical.periodic_number IS NOT DISTINCT FROM observations_comet_optical.periodic_number)
JOIN observatories ON observatories.observatory_code IS NOT DISTINCT FROM observations_comet_optical.observatory_code
WHERE mp_physical.mp_type='C';

对于此查询,我有执行计划:query B EXPLAIN

observation_optical 转储:

CREATE TABLE observations_optical (
    id_obs_o bigint NOT NULL,
    note_1 character varying,
    date timestamp without time zone NOT NULL,
    "RA" time without time zone NOT NULL,
    "Dec" time without time zone NOT NULL,
    magnitude double precision,
    band character varying,
    id_observatory integer NOT NULL,
    id_mpp integer NOT NULL,
    "Dec_degree" integer
);

CREATE SEQUENCE observations_optical_id_obs_o_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE observations_optical_id_obs_o_seq OWNED BY observations_optical.id_obs_o;

ALTER TABLE ONLY observations_optical ALTER COLUMN id_obs_o SET DEFAULT nextval('observations_optical_id_obs_o_seq'::regclass);
ALTER TABLE ONLY observations_optical
    ADD CONSTRAINT "_C_id_obs_o" PRIMARY KEY (id_obs_o);

observation_comet_optical dump:

CREATE TEMPORARY TABLE observations_comet_optical(
    LIKE observations_optical
);
ALTER TABLE observations_comet_optical DROP COLUMN id_obs_o;
ALTER TABLE observations_comet_optical DROP COLUMN id_observatory;
ALTER TABLE observations_comet_optical DROP COLUMN id_mpp;
ALTER TABLE observations_comet_optical ADD COLUMN observatory_code varchar(3);
ALTER TABLE observations_comet_optical ADD COLUMN periodic_number integer;
ALTER TABLE observations_comet_optical ADD COLUMN mpp_designation varchar(30);
CREATE INDEX _I_oco_observatory_code ON observations_comet_optical USING btree (observatory_code);
CREATE INDEX _I_oco_periodic_number ON observations_comet_optical  USING btree (periodic_number);
CREATE INDEX _I_oco_mpp_designation ON observations_comet_optical  USING btree (mpp_designation);

mp_physical 转储:

CREATE TABLE mp_physical (
    id_mpp integer NOT NULL,
    id_comet_parts integer,
    "SPK_id" nonnegative_int,
    designation character varying(30),
    name character varying(100),
    prefix character varying,
    "is_NEO" boolean,
    "H" double precision,
    "G" double precision,
    diameter nonnegative_double,
    extent character varying(30),
    extent_error nonnegative_double,
    geometric_albedo nonnegative_double,
    rot_per nonnegative_double,
    "GM" nonnegative_double,
    "BV" nonnegative_double,
    "UB" nonnegative_double,
    "spec_B" character varying(30),
    "spec_T" character varying(30),
    lca double precision,
    multiplicity nonnegative_int,
    polar_ang double precision,
    polar_slope_ang double precision,
    a double precision,
    b double precision,
    mass nonnegative_double,
    mp_type mp_type NOT NULL,
    periodic_number nonnegative_int,
    diameter_method_def character varying(200),
    discovery_info text,
    "H_sigma" nonnegative_double,
    "G_sigma" nonnegative_double,
    diameter_sigma nonnegative_double,
    geometric_albedo_sigma nonnegative_double,
    rot_per_sigma nonnegative_double,
    "GM_sigma" nonnegative_double,
    "BV_sigma" nonnegative_double,
    "UB_sigma" nonnegative_double,
    lca_sigma nonnegative_double,
    a_sigma nonnegative_double,
    b_sigma nonnegative_double,
    polar_ang_sigma nonnegative_double,
    mass_sigma nonnegative_double
);

CREATE SEQUENCE mp_physical_id_mpp_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE mp_physical_id_mpp_seq OWNED BY mp_physical.id_mpp;
ALTER TABLE ONLY mp_physical ALTER COLUMN id_mpp SET DEFAULT  nextval('mp_physical_id_mpp_seq'::regclass);
ALTER TABLE ONLY mp_physical
    ADD CONSTRAINT "_C_id_ap" PRIMARY KEY (id_mpp);

CREATE INDEX "_I_designation" ON mp_physical USING btree (designation);
CREATE INDEX "_I_id_comet_parts" ON mp_physical USING btree (id_comet_parts);
CREATE INDEX "_I_mp_type" ON mp_physical USING btree (mp_type);
CREATE INDEX "_I_name" ON mp_physical USING btree (name);
CREATE INDEX "_I_periodic_number" ON mp_physical USING btree (periodic_number);
CREATE INDEX _i_mp_designation ON mp_physical USING btree (designation);
CREATE INDEX _i_mp_periodic_number ON mp_physical USING btree (periodic_number);

ALTER TABLE ONLY mp_physical
    ADD CONSTRAINT comet_parts_fk FOREIGN KEY (id_comet_parts) REFERENCES comet_parts(id_comet_parts) MATCH FULL ON UPDATE CASCADE ON DELETE RESTRICT;

天文台转储:

CREATE TABLE observatories (
    id_observatory integer NOT NULL,
    observatory_code character varying(3) NOT NULL,
    name character varying(200) NOT NULL,
    longitude nonnegative_double NOT NULL,
    cos double precision NOT NULL,
    sin double precision NOT NULL
);

CREATE SEQUENCE observatories_id_observatory_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;

ALTER SEQUENCE observatories_id_observatory_seq OWNED BY observatories.id_observatory;
ALTER TABLE ONLY observatories ALTER COLUMN id_observatory SET DEFAULT nextval('observatories_id_observatory_seq'::regclass);
ALTER TABLE ONLY observatories
    ADD CONSTRAINT "_C_id_observatory" PRIMARY KEY (id_observatory);
CREATE INDEX _i_obs_observatory_code ON observatories USING btree (observatory_code);

1 个答案:

答案 0 :(得分:0)

尝试使用" COALESCE"取而代之的是#34;而不是#34;。

我不确定这是否会更好。

相关问题