2013-07-27 77 views
0

現在,我將數據庫從Microsoft SQL Server移動到PostgreSQL 9.1。 有一個簡單的查詢,來計算我們店的一些總結:關於Postgres totaly的查詢按下了服務器

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,不到一秒鐘,大約有15萬條記錄spareparts表。

在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(); 

回答

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 
1

我不認爲你實際需要的子查詢;你可以簡單地寫:

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

應該更有效率。

+0

對不起,我不能接受2個答案!謝謝!!! – Andrey