From: Hengky Liwandouw <hengkyliwandouw@gmail.com>
To: Torsten Förtsch <torsten.foertsch@gmx.net>
Cc: pgsql-performance@postgresql.org
Subject: Re: Speed up the query
Date: Sun, 1 Dec 2013 20:25:38 +0800
Message-ID: <8A61AB71-AE85-40A6-B9EF-316D39DEABD8@gmail.com> (raw)
In-Reply-To: <529B265B.5030206@gmx.net>
References: <B097B80F-2D51-4F42-BC0D-5A38CBCD0C01@gmail.com>
<20131201071231.GA4780@tux>
<335A179F-B1A5-4634-998F-BA30ACD84209@gmail.com>
<20131201083541.GA10012@tux>
<980E5A18-104B-46C8-AB98-A66A391C8272@gmail.com>
<529B265B.5030206@gmx.net>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-performance>
Dear All,
Thanks for the suggestion ! I tried to change the work_mem and the query only needs 4.9 sec to display the result !
Torsten, your 2nd option didnt work with this error :
ERROR: syntax error at or near "discount"
LINE 1: ...rang, keluar, case when discount<=...
^
For Mat : what command i can use to show how big the tables in MB ?
Thanks
On Dec 1, 2013, at 8:06 PM, Torsten Förtsch wrote:
> On 01/12/13 10:07, Hengky Liwandouw wrote:
>> with qry1 as
>> (select tanggal, extract(month from tanggal) as bulan, tblsupplier.id, nama, kodebarang, namabarang, keluar,
>> case when discount<=100 then
>> keluar*(harga -(discount/100*harga))
>> when tbltransaksi.discount>100 then
>> keluar*(harga-discount)
>> end
>> as jumlah
>> from tbltransaksi
>> join tblproduk on tbltransaksi.kodebarang=tblproduk.produkid
>> join tblsupplier on tblproduk.supplierid=tblsupplier.id
>> where jualid is not null or returjualid is not null
>> and extract(year from tanggal)='2013')
>>
>> select
>> id, nama, kodebarang, namabarang,
>> sum(case when bulan = 1 then keluar else 0 end) as Jan,
>> sum(case when bulan = 2 then keluar else 0 end) as Feb,
>> sum(case when bulan = 3 then keluar else 0 end) as Maret,
>> sum(case when bulan = 4 then keluar else 0 end) as April,
>> sum(case when bulan = 5 then keluar else 0 end) as Mei,
>> sum(case when bulan = 6 then keluar else 0 end) as Juni,
>> sum(case when bulan = 7 then keluar else 0 end) as Juli,
>> sum(case when bulan = 8 then keluar else 0 end) as Agust,
>> sum(case when bulan = 9 then keluar else 0 end) as Sept,
>> sum(case when bulan = 10 then keluar else 0 end) as Okt,
>> sum(case when bulan = 11 then keluar else 0 end) as Nov,
>> sum(case when bulan = 12 then keluar else 0 end) as Des,
>> sum(coalesce(keluar,0)) as total
>> from qry1
>> group by id, nama, kodebarang, namabarang
>> order by total desc
>> limit 1000
>>
>> this is the explain analyse :
>>
>> "Limit (cost=346389.90..346392.40 rows=1000 width=376) (actual time=56765.848..56766.229 rows=1000 loops=1)"
>> " CTE qry1"
>> " -> Hash Join (cost=4444.64..62683.91 rows=766519 width=84) (actual time=87.342..1786.851 rows=737662 loops=1)"
>> " Hash Cond: ((tbltransaksi.kodebarang)::text = (tblproduk.produkid)::text)"
>> " -> Seq Scan on tbltransaksi (cost=0.00..24704.06 rows=766519 width=29) (actual time=0.010..271.147 rows=767225 loops=1)"
>> " Filter: ((jualid IS NOT NULL) OR ((returjualid IS NOT NULL) AND (date_part('year'::text, (tanggal)::timestamp without time zone) = 2013::double precision)))"
>> " Rows Removed by Filter: 37441"
>> " -> Hash (cost=3380.52..3380.52 rows=85130 width=68) (actual time=87.265..87.265 rows=65219 loops=1)"
>> " Buckets: 16384 Batches: 1 Memory Usage: 5855kB"
>> " -> Hash Join (cost=21.68..3380.52 rows=85130 width=68) (actual time=0.748..59.469 rows=65219 loops=1)"
>> " Hash Cond: ((tblproduk.supplierid)::text = (tblsupplier.id)::text)"
>> " -> Seq Scan on tblproduk (cost=0.00..2188.30 rows=85130 width=51) (actual time=0.005..17.184 rows=85034 loops=1)"
>> " -> Hash (cost=14.08..14.08 rows=608 width=26) (actual time=0.730..0.730 rows=609 loops=1)"
>> " Buckets: 1024 Batches: 1 Memory Usage: 28kB"
>> " -> Seq Scan on tblsupplier (cost=0.00..14.08 rows=608 width=26) (actual time=0.006..0.298 rows=609 loops=1)"
>> " -> Sort (cost=283705.99..283897.62 rows=76652 width=376) (actual time=56765.846..56766.006 rows=1000 loops=1)"
>> " Sort Key: (sum(COALESCE(qry1.keluar, 0::numeric)))"
>> " Sort Method: top-N heapsort Memory: 280kB"
>> " -> GroupAggregate (cost=221247.80..279503.25 rows=76652 width=376) (actual time=50731.735..56739.181 rows=23630 loops=1)"
>> " -> Sort (cost=221247.80..223164.10 rows=766519 width=376) (actual time=50731.687..54455.528 rows=737662 loops=1)"
>> " Sort Key: qry1.id, qry1.nama, qry1.kodebarang, qry1.namabarang"
>> " Sort Method: external merge Disk: 71872kB"
>> " -> CTE Scan on qry1 (cost=0.00..15330.38 rows=766519 width=376) (actual time=87.346..2577.066 rows=737662 loops=1)"
>> "Total runtime: 56787.136 ms"
>
> I'd try 2 things:
>
> 1) set work_mem to ~100Mb. You don't have to do that globally in
> postgresql.conf. You can set it for the current session only.
>
> set work_mem to '100MB';
>
> Then run your query.
>
> 2) change the common table expression to a subquery:
>
> select
> id, nama, kodebarang, namabarang,
> sum(case when bulan = 1 then keluar else 0 end) as Jan,
> sum(case when bulan = 2 then keluar else 0 end) as Feb,
> sum(case when bulan = 3 then keluar else 0 end) as Maret,
> sum(case when bulan = 4 then keluar else 0 end) as April,
> sum(case when bulan = 5 then keluar else 0 end) as Mei,
> sum(case when bulan = 6 then keluar else 0 end) as Juni,
> sum(case when bulan = 7 then keluar else 0 end) as Juli,
> sum(case when bulan = 8 then keluar else 0 end) as Agust,
> sum(case when bulan = 9 then keluar else 0 end) as Sept,
> sum(case when bulan = 10 then keluar else 0 end) as Okt,
> sum(case when bulan = 11 then keluar else 0 end) as Nov,
> sum(case when bulan = 12 then keluar else 0 end) as Des,
> sum(coalesce(keluar,0)) as total
> from (select tanggal, extract(month from tanggal) as bulan,
> tblsupplier.id, nama, kodebarang, namabarang, keluar,
> case
> when discount<=100 then
> keluar*(harga -(discount/100*harga))
> when tbltransaksi.discount>100 then
> keluar*(harga-discount)
> end as jumlah
> from tbltransaksi
> join tblproduk on tbltransaksi.kodebarang=tblproduk.produkid
> join tblsupplier on tblproduk.supplierid=tblsupplier.id
> where jualid is not null or returjualid is not null
> and extract(year from tanggal)='2013') qry1
> group by id, nama, kodebarang, namabarang
> order by total desc
> limit 1000
>
> Selamat berjaya,
> Torsten
--
Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-performance@postgresql.org
Cc: hengkyliwandouw@gmail.com, torsten.foertsch@gmx.net
Subject: Re: Speed up the query
In-Reply-To: <8A61AB71-AE85-40A6-B9EF-316D39DEABD8@gmail.com>
References: <B097B80F-2D51-4F42-BC0D-5A38CBCD0C01@gmail.com> <20131201071231.GA4780@tux> <335A179F-B1A5-4634-998F-BA30ACD84209@gmail.com> <20131201083541.GA10012@tux> <980E5A18-104B-46C8-AB98-A66A391C8272@gmail.com> <529B265B.5030206@gmx.net> <8A61AB71-AE85-40A6-B9EF-316D39DEABD8@gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox