对Postgres的查询总结了服务器

时间:2013-07-27 19:31:33

标签: postgresql

现在,我将数据库从Microsoft SQL Server迁移到PostgreSQL 9.1。 有一个简单的查询,来计算我们商店的一些摘要:

SELECT DISTINCT  p.part_name_id,

(SELECT     SUM(p1.quantity) 
FROM          parts.spareparts p1
WHERE      p1.part_name_id = p.part_name_id) AS AllQuantity, 

(SELECT     SUM(p2.price * p2.quantity) 
FROM          parts.spareparts p2 
WHERE      p2.part_name_id = p.part_name_id) AS AllPrice 


FROM parts.spareparts p

它在MSSQL上工作得非常快,不到一秒钟,spareparts表中有大约150,000条记录。

在PostgreSQL中,我等待了200,000毫秒,而不是等待结果。

我哪里错了?

P.S。:表格定义:

-- Table: parts.spareparts

-- DROP TABLE parts.spareparts;

CREATE TABLE parts.spareparts
(
  id serial NOT NULL,
  big_id bigint NOT NULL,
  part_unique integer NOT NULL,
  store_address integer,
  brand_id integer,
  model_id integer,
  category_id integer,
  part_name_id integer,
  price money,
  quantity integer,
  description character varying(250),
  private_info character varying(600),
  manager_id integer,
  company_id integer,
  part_type smallint,
  box_number integer,
  com_person character varying(200),
  com_phone character varying(200),
  vendor_id integer,
  is_publish boolean DEFAULT true,
  is_comission boolean DEFAULT false,
  is_new boolean DEFAULT false,
  is_warning boolean DEFAULT false,
  catalog_no character varying(200),
  disc_id integer,
  is_set boolean,
  w_height numeric(3,2),
  w_width numeric(3,2),
  w_diam numeric(3,2),
  w_type integer,
  page_url character varying(150),
  last_edit_manager_id integer,
  CONSTRAINT spareparts_pk PRIMARY KEY (id)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE parts.spareparts
  OWNER TO asap;

-- Index: parts.sparepart_part_unique_idx

-- DROP INDEX parts.sparepart_part_unique_idx;

CREATE INDEX sparepart_part_unique_idx
  ON parts.spareparts
  USING btree
  (part_unique, company_id);

-- Index: parts.spareparts_4param_idx

-- DROP INDEX parts.spareparts_4param_idx;

CREATE INDEX spareparts_4param_idx
  ON parts.spareparts
  USING btree
  (brand_id, model_id, category_id, part_name_id);

-- Index: parts.spareparts_bigid_idx

-- DROP INDEX parts.spareparts_bigid_idx;

CREATE INDEX spareparts_bigid_idx
  ON parts.spareparts
  USING btree
  (big_id);

-- Index: parts.spareparts_brand_id_part_id_quantity_idx

-- DROP INDEX parts.spareparts_brand_id_part_id_quantity_idx;

CREATE INDEX spareparts_brand_id_part_id_quantity_idx
  ON parts.spareparts
  USING btree
  (brand_id, part_name_id, quantity);

-- Index: parts.spareparts_brand_id_quantity_idx

-- DROP INDEX parts.spareparts_brand_id_quantity_idx;

CREATE INDEX spareparts_brand_id_quantity_idx
  ON parts.spareparts
  USING btree
  (brand_id, quantity);

-- Index: parts.spareparts_company_id_part_unique_idx

-- DROP INDEX parts.spareparts_company_id_part_unique_idx;

CREATE INDEX spareparts_company_id_part_unique_idx
  ON parts.spareparts
  USING btree
  (company_id, part_unique);

-- Index: parts.spareparts_model_id_company_id

-- DROP INDEX parts.spareparts_model_id_company_id;

CREATE INDEX spareparts_model_id_company_id
  ON parts.spareparts
  USING btree
  (model_id, company_id);
COMMENT ON INDEX parts.spareparts_model_id_company_id
  IS 'Для frmFilter';

-- Index: parts.spareparts_url_idx

-- DROP INDEX parts.spareparts_url_idx;

CREATE INDEX spareparts_url_idx
  ON parts.spareparts
  USING btree
  (page_url COLLATE pg_catalog."default");


-- Trigger: spareparts_delete_trigger on parts.spareparts

-- DROP TRIGGER spareparts_delete_trigger ON parts.spareparts;

CREATE TRIGGER spareparts_delete_trigger
  AFTER DELETE
  ON parts.spareparts
  FOR EACH ROW
  EXECUTE PROCEDURE parts.spareparts_delete_fn();

-- Trigger: spareparts_update_trigger on parts.spareparts

-- DROP TRIGGER spareparts_update_trigger ON parts.spareparts;

CREATE TRIGGER spareparts_update_trigger
  AFTER INSERT OR UPDATE
  ON parts.spareparts
  FOR EACH ROW
  EXECUTE PROCEDURE parts.spareparts_update_fn();

2 个答案:

答案 0 :(得分:1)

我认为你实际上不需要子查询;你可以简单地写一下:

SELECT part_name_id,
       SUM(quantity) AS AllQuantity,
       SUM(price * quantity) AS AllPrice
  FROM parts.spare_parts
 GROUP
    BY part_name_id
;

应该更有效率。

答案 1 :(得分:1)

我认为您可以在不需要嵌套选择的情况下重写查询:

SELECT p.part_name_id,
       SUM(p.quantity) AS AllQuantity, 
       SUM(p.price * p.quantity) AS AllPrice 
FROM parts.spareparts p
group by p.part_name_id