X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id A820B9FA5DD for ; Thu, 29 Jun 2006 23:55:34 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 08717-08 for ; Thu, 29 Jun 2006 23:55:27 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from weatherbeeta.com.au (mail.weatherbeeta.com.au [202.154.127.242]) by postgresql.org (Postfix) with SMTP id F2FD99FA382 for ; Thu, 29 Jun 2006 23:55:25 -0300 (ADT) Received: (qmail 13404 invoked from network); 30 Jun 2006 02:55:19 -0000 X-NetworkBox-HamSign: 0101;OUT;wbeetaau1;cd39f3d2bc567c5be477acc957015c2d; Received: from mail.weatherbeeta.com.au (HELO wb-email) (172.20.0.2)by network-box.weatherbeeta.com.au with SMTP; 30 Jun 2006 02:55:18 -0000 Received: from it-phil.weatherbeeta.com.au ([172.20.0.155]) by wb-email (602Pro LAN SUITE 2003) id 2d54b669 for pgsql-sql@postgresql.org; Fri, 30 Jun 2006 12:53:37 +1000 From: "Phillip Smith" To: Subject: Re: SELECT Aggregate Date: Fri, 30 Jun 2006 12:55:05 +1000 Message-ID: <006a01c69bf0$97a028f0$9b0014ac@ITPhil> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_006B_01C69C44.694C38F0" X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook, Build 10.0.6626 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2869 In-Reply-To: <20060630005111.45426.qmail@web31809.mail.mud.yahoo.com> Importance: Normal X-Scanned-By-wbeetaau1: Virus scan performed by network-box X-Scanned-By-wbeetaau1: Scanner file id is wbeetaau1-1151636118.794-13399-000 X-Scanned-By-wbeetaau1: No known viruses found in message (received+scanned in 0.05/0.11 secs) X-Scanned-By-wbeetaau1: Spam-Check-Result: No, hits=0 required=7 tests= autolearn=no version=2.0 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200606/327 X-Sequence-Number: 25563 This is a multi-part message in MIME format. ------=_NextPart_000_006B_01C69C44.694C38F0 Content-Type: text/plain;charset="us-ascii" Content-Transfer-Encoding: 7bit Well whatdyaknow?? Being a Postgres newbie I hadn't even played with indexes yet. They're awesome!! Using Richard's suggestion of the Sub-Select in the COLUMN list, combined with adding some indexes, I can now return this in under 5 seconds! I've included the new SELECT query, as well as the definitions of the indexes below for anyone who's interested. Thanks guys! QUERY: SELECT trans_no, customer, date_placed, date_complete, date_printed, (SELECT SUM(sell_price) FROM soh_product WHERE sales_orders.trans_no = soh_product.soh_num ) AS wholesale, ord_type, ord_status, customer_reference, salesman, parent_order, child_order, order_number FROM sales_orders WHERE (trans_no Like '8%' AND order_number Like '8%') OR (trans_no Like '9%' AND order_number Like '8%') OR (trans_no Like '8%' AND order_number Like '9%') OR (trans_no Like '9%' AND order_number Like '9%') AND warehouse='M' AND date_placed > (current_date + ('12 months ago'::interval)) ORDER BY trans_no DESC INDEXES: CREATE INDEX sales_orders_customer ON sales_orders USING btree (customer); CREATE INDEX sales_orders_orderno ON sales_orders USING btree (order_number); CREATE INDEX sales_orders_customer ON sales_orders USING btree (customer); CREATE INDEX soh_product_prodcode ON soh_product USING btree (prod_code); CREATE INDEX soh_product_transno ON soh_product USING btree (soh_num); -----Original Message----- From: Richard Broersma Jr [mailto:rabroersma@yahoo.com] Sent: Friday, 30 June 2006 10:51 To: Phillip Smith; pgsql-sql@postgresql.org Subject: Re: [SQL] SELECT Aggregate > I've tried Aaron's suggestion of the GROUP BY and I don't know much about > it, but it ran for around 17 hours and still going (it had a dedicated Dual > Xeon 3.0GHz box under RHEL4 running it!) Maybe, this query that you are trying to run is a good candidate for a "Materialize View". http://archives.postgresql.org/pgsql-performance/2004-02/msg00279.php Also before you run your query you might want to see the explain plan is. Perhap it is using a sequencial scan in a place where an index can improve query preformance. *******************Confidentiality and Privilege Notice******************* The material contained in this message is privileged and confidential to the addressee. If you are not the addressee indicated in this message or responsible for delivery of the message to such person, you may not copy or deliver this message to anyone, and you should destroy it and kindly notify the sender by reply email. Information in this message that does not relate to the official business of Weatherbeeta must be treated as neither given nor endorsed by Weatherbeeta. Weatherbeeta, its employees, contractors or associates shall not be liable for direct, indirect or consequential loss arising from transmission of this message or any attachments ------=_NextPart_000_006B_01C69C44.694C38F0 Content-Type: text/html;charset="us-ascii" Content-Transfer-Encoding: quoted-printable

Well = whatdyaknow?? Being a Postgres newbie I hadn't even played with indexes yet. They're = awesome!!

 

Using Richard's suggestion of the Sub-Select in the COLUMN list, combined with adding = some indexes, I can now return this in under 5 seconds!

 

I’ve = included the new SELECT query, as well as the definitions of the indexes below for = anyone who’s interested.

 

Thanks = guys!

 

QUERY:

SELECT      trans_no,

            = customer,

            = date_placed,

            = date_complete,

            = date_printed,

            = (SELECT  = SUM(sell_price)

      =            FROM   soh_product

      =            WHERE  sales_orders.trans_no =3D = soh_product.soh_num

      =            ) AS wholesale,

            = ord_type,

            = ord_status,

            = customer_reference,

            = salesman,

            = parent_order,

            = child_order,

            = order_number

FROM        = sales_orders

WHERE       (trans_no Like '8%' AND = order_number Like '8%')

 OR         (trans_no = Like '9%' AND order_number Like '8%')

 OR         (trans_no = Like '8%' AND order_number Like '9%')

 OR         (trans_no = Like '9%' AND order_number Like '9%')

 AND        = warehouse=3D'M'

 AND        date_placed > = (current_date + ('12 months ago'::interval))

ORDER BY    trans_no DESC

 

INDEXES:

CREATE INDEX sales_orders_customer

  ON sales_orders

  USING btree

  (customer);

 

CREATE INDEX sales_orders_orderno

  ON sales_orders

  USING btree

  (order_number);

 

CREATE INDEX sales_orders_customer

  ON sales_orders

  USING btree

  (customer);

 

CREATE INDEX soh_product_prodcode

  ON soh_product

  USING btree

  (prod_code);

 

CREATE INDEX soh_product_transno

  ON soh_product

  USING btree

  (soh_num);

 

 

-----Original Message-----
From: Richard Broersma Jr [mailto:rabroersma@yahoo.com]
Sent: Friday, 30 June 2006 10:51
To: Phillip Smith; pgsql-sql@postgresql.org
Subject: Re: [SQL] SELECT Aggregate

 

> I've tried Aaron's suggestion of the GROUP BY and I don't = know much about

> it, but it ran for around 17 hours and still going (it had = a dedicated Dual

> Xeon 3.0GHz box under RHEL4 running it!)

 

Maybe, this query that you are trying to run is a good candidate = for a "Materialize View".

http://archives.postgresql.org/pgsql-performance/2004-02/msg00279= .php

 

Also before you run your query you might want to see the explain = plan is.  Perhap it is using a

sequencial scan in a place where an index can improve query preformance.

 

 


*******************Confidentiality and Privilege Notice*******************

The material contained in this message is privileged and confidential to the addressee. If you are not the addressee indicated in this message or responsible for delivery of the message to such person, you may not copy or deliver this message to anyone, and you should destroy it and kindly notify the sender by reply email.

Information in this message that does not relate to the official business of Weatherbeeta must be treated as neither given nor endorsed by Weatherbeeta. Weatherbeeta, its employees, contractors or associates shall not be liable for direct, indirect or consequential loss arising from transmission of this message or any attachments
------=_NextPart_000_006B_01C69C44.694C38F0--