Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 43E9D64FCFF for ; Thu, 21 Aug 2008 11:39:39 -0300 (ADT) Received: from postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 16850-10 for ; Thu, 21 Aug 2008 11:39:20 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from wf-out-1314.google.com (wf-out-1314.google.com [209.85.200.173]) by postgresql.org (Postfix) with ESMTP id 75CD064FCE6 for ; Thu, 21 Aug 2008 11:39:26 -0300 (ADT) Received: by wf-out-1314.google.com with SMTP id 25so13537wfc.28 for ; Thu, 21 Aug 2008 07:39:24 -0700 (PDT) Received: by 10.142.163.13 with SMTP id l13mr516088wfe.34.1219329564816; Thu, 21 Aug 2008 07:39:24 -0700 (PDT) Received: by 10.143.39.19 with HTTP; Thu, 21 Aug 2008 07:39:24 -0700 (PDT) Message-ID: Date: Thu, 21 Aug 2008 07:39:24 -0700 From: "Scott Carey" To: "Moritz Onken" Subject: Re: Slow query with a lot of data Cc: pgsql-performance@postgresql.org In-Reply-To: <7DE7116C-4BBC-4776-AEE0-63A2E2DEAF16@houseofdesign.de> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_49013_9040357.1219329564811" References: <90AA1B69-4E9C-4710-9D58-F65620DDA022@houseofdesign.de> <0BFF9855-F1FA-4A83-B8B2-622D7CE69F77@houseofdesign.de> <3663375B-7AC0-43F6-B6EC-D3EE572F3D34@houseofdesign.de> <24E3FBFF-E0B8-435E-ACE1-652E93FE2213@houseofdesign.de> <380CD097-4A33-4134-8C13-B9BF34EC6C63@houseofdesign.de> <7DE7116C-4BBC-4776-AEE0-63A2E2DEAF16@houseofdesign.de> X-Google-Sender-Auth: 87b8eac57eed7977 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.001 tagged_above=0 required=5 tests=HTML_MESSAGE=0.001 X-Spam-Level: X-Archive-Number: 200808/250 X-Sequence-Number: 31131 ------=_Part_49013_9040357.1219329564811 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline It looks to me like the work_mem did have an effect. Your earlier queries had a sort followed by group aggregate at the top, and now its a hash-aggregate. So the query plan DID change. That is likely where the first 10x performance gain came from. The top of the plan was: GroupAggregate (cost=11745105.66..12277396. 81 rows=28704 width=12)" -> Sort (cost=11745105.66..11878034.93 rows=53171707 width=12)" -> Merge Join (cost=149241.25..1287278.89 rows=53171707 width=12)" Merge Cond: (b.domain = a.domain)" and now it is: "HashAggregate (cost=1685527.69..1686101.77 rows=28704 width=12)" -> Merge Join (cost=148702.25..1286739.89 rows=53171707 width=12)" Merge Cond: (b.domain = a.domain)" The HashAggregate replaced the Sort followed by GroupAggregate at about 1/10 the cost. It probably only took the first couple hundred MB of work_mem to do this, or less given that you were at the default originally. Note how the estimated cost on the latter is 1.6 million, and it is 11 million in the first one. You won't get a large table aggregate significantly faster than this -- you're asking it to scan through 53 million records and aggregate. An explain analyze will be somewhat instructive to help identify if there is more I/O or CPU bound overall as we can compare the estimated cost with the actual times, but this probably won't get very far. After that, inserting 16M rows requires rather different tuning and bottleneck identification. On Thu, Aug 21, 2008 at 12:03 AM, Moritz Onken wrote: > > Am 20.08.2008 um 20:06 schrieb Scott Carey: > > Ok, so the problem boils down to the sort at the end. >> >> The query up through the merge join on domain is as fast as its going to >> get. The sort at the end however, should not happen ideally. There are not >> that many rows returned, and it should hash_aggregate if it thinks there is >> enough space to do so. >> >> The query planner is going to choose the sort > agg over the hash-agg if >> it estimates the total number of resulting rows to be large enough so that >> the hash won't fit in work_mem. However, there seems to be another factor >> here based on this: >> >> >> GroupAggregate (cost=11745105.66..12277396. >> 81 rows=28704 width=12)" >> " -> Sort (cost=11745105.66..11878034.93 rows=53171707 width=12)" >> >> " Sort Key: a."user", b.category" >> " -> Merge Join (cost=149241.25..1287278.89 rows=53171707 >> width=12)" >> >> " Merge Cond: (b.domain = a.domain)" >> >> >> The planner actually thinks there will only be 28704 rows returned of >> width 12. But it chooses to sort 53 million rows before aggregating. Thats >> either a bug or there's something else wrong here. That is the wrong way >> to aggregate those results no matter how much work_mem you have unless I'm >> completely missing something... >> >> You can try rearranging the query just to see if you can work around this. >> What happens if you compare the explain on: >> >> select >> a."user", b.category, sum(1.0/b.cat_count)::float >> from result a, domain_categories b >> where a."domain" = b."domain" >> and b.depth < 4 >> and a.results > 100 >> and a."user" < 30000 >> group by a."user", b.category >> >> >> > "HashAggregate (cost=1685527.69..1686101.77 rows=28704 width=12)" > " -> Merge Join (cost=148702.25..1286739.89 rows=53171707 width=12)" > " Merge Cond: (b.domain = a.domain)" > " -> Index Scan using domain_categories_domain on domain_categories > b (cost=0.00..421716.32 rows=5112568 width=12)" > " Filter: (depth < 4)" > " -> Sort (cost=148415.16..148513.60 rows=39376 width=8)" > " Sort Key: a.domain" > " -> Bitmap Heap Scan on result a (cost=1249.93..145409.79 > rows=39376 width=8)" > " Recheck Cond: ("user" < 30000)" > " Filter: (results > 100)" > " -> Bitmap Index Scan on result_user_idx > (cost=0.00..1240.08 rows=66881 width=0)" > " Index Cond: ("user" < 30000)" > > > > to >> >> select >> c."user", c.category, sum(1.0/c.cat_count)::float >> from (select a."user", b.category, b.cat_count >> from result a, domain_categories b >> where a."domain" = b."domain" >> and b.depth < 4 >> and a.results > 100 >> and a."user" < 30000 ) c >> group by c."user", c.category >> >> > > "HashAggregate (cost=1685527.69..1686101.77 rows=28704 width=12)" > " -> Merge Join (cost=148702.25..1286739.89 rows=53171707 width=12)" > " Merge Cond: (b.domain = a.domain)" > " -> Index Scan using domain_categories_domain on domain_categories > b (cost=0.00..421716.32 rows=5112568 width=12)" > " Filter: (depth < 4)" > " -> Sort (cost=148415.16..148513.60 rows=39376 width=8)" > " Sort Key: a.domain" > " -> Bitmap Heap Scan on result a (cost=1249.93..145409.79 > rows=39376 width=8)" > " Recheck Cond: ("user" < 30000)" > " Filter: (results > 100)" > " -> Bitmap Index Scan on result_user_idx > (cost=0.00..1240.08 rows=66881 width=0)" > " Index Cond: ("user" < 30000)" > > > > It shouldn't make a difference, but I've seen things like this help before >> so its worth a try. Make sure work_mem is reasonably sized for this test. >> > > It's exactly the same. work_mem was set to 3000MB. > > >> >> Another thing that won't be that fast, but may avoid the sort, is to >> select the subselection above into a temporary table, analyze it, and then >> do the outer select. Make sure your settings for temporary space >> (temp_buffers in 8.3) are large enough for the intermediate results (700MB >> should do it). That won't be that fast, but it will most likely be faster >> than sorting 50 million + rows. There are lots of problems with this >> approach but it may be worth the experiment. >> >> > I'll try this. > > Thanks so far! > > > -- > Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-performance > ------=_Part_49013_9040357.1219329564811 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline
It looks to me like the work_mem did have an effect. 

Your earlier queries had a sort followed by group aggregate at the top, and now its a hash-aggregate.  So the query plan DID change.  That is likely where the first 10x performance gain came from. 

The top of the plan was:

GroupAggregate  (cost=11745105.66..12277396.
81 rows=28704 width=12)"
  ->  Sort  (cost=11745105.66..11878034.93 rows=53171707 width=12)"
        ->  Merge Join  (cost=149241.25..1287278.89 rows=53171707 width=12)"
              Merge Cond: (b.domain = a.domain)"

and now it is:

"HashAggregate  (cost=1685527.69..1686101.77 rows=28704 width=12)"
  ->  Merge Join  (cost=148702.25..1286739.89 rows=53171707 width=12)"
        Merge Cond: (b.domain = a.domain)"

The HashAggregate replaced the Sort followed by GroupAggregate at about 1/10 the cost.
 
It probably only took the first couple hundred MB of work_mem to do this, or less given that you were at the default originally.
Note how the estimated cost on the latter is 1.6 million, and it is 11 million in the first one.

You won't get a large table aggregate significantly faster than this -- you're asking it to scan through 53 million records and aggregate.  An explain analyze will be somewhat instructive to help identify if there is more I/O or CPU bound overall as we can compare the estimated cost with the actual times, but this probably won't get very far.

After that, inserting 16M rows requires rather different tuning and bottleneck identification.

On Thu, Aug 21, 2008 at 12:03 AM, Moritz Onken <onken@houseofdesign.de> wrote:

Am 20.08.2008 um 20:06 schrieb Scott Carey:


Ok, so the problem boils down to the sort at the end.

The query up through the merge join on domain is as fast as its going to get.  The sort at the end however, should not happen ideally.  There are not that many rows returned, and it should hash_aggregate if it thinks there is enough space to do so.

The query planner is going to choose the sort > agg over the hash-agg if it estimates the total number of resulting rows to be large enough so that the hash won't fit in work_mem.   However, there seems to be another factor here based on this:


GroupAggregate  (cost=11745105.66..12277396.
81 rows=28704 width=12)"
"  ->  Sort  (cost=11745105.66..11878034.93 rows=53171707 width=12)"

"        Sort Key: a."user", b.category"
"        ->  Merge Join  (cost=149241.25..1287278.89 rows=53171707 width=12)"

"              Merge Cond: (b.domain = a.domain)"


The planner actually thinks there will only be 28704 rows returned of width 12.  But it chooses to sort 53 million rows before aggregating.  Thats either a bug or there's something else wrong here.   That is the wrong way to aggregate those results no matter how much work_mem you have unless I'm completely missing something...

You can try rearranging the query just to see if you can work around this.  What happens if you compare the explain on:

select
 a."user", b.category, sum(1.0/b.cat_count)::float
 from result a, domain_categories b
 where a."domain" = b."domain"
 and b.depth < 4
 and a.results > 100
 and a."user" < 30000
 group by a."user", b.category



"HashAggregate  (cost=1685527.69..1686101.77 rows=28704 width=12)"
"  ->  Merge Join  (cost=148702.25..1286739.89 rows=53171707 width=12)"

"        Merge Cond: (b.domain = a.domain)"
"        ->  Index Scan using domain_categories_domain on domain_categories b  (cost=0.00..421716.32 rows=5112568 width=12)"
"              Filter: (depth < 4)"
"        ->  Sort  (cost=148415.16..148513.60 rows=39376 width=8)"

"              Sort Key: a.domain"
"              ->  Bitmap Heap Scan on result a  (cost=1249.93..145409.79 rows=39376 width=8)"
"                    Recheck Cond: ("user" < 30000)"
"                    Filter: (results > 100)"
"                    ->  Bitmap Index Scan on result_user_idx  (cost=0.00..1240.08 rows=66881 width=0)"
"                          Index Cond: ("user" < 30000)"



to

select
 c."user", c.category, sum(1.0/c.cat_count)::float
 from (select a."user", b.category, b.cat_count
  from result a, domain_categories b
    where a."domain" = b."domain"
      and b.depth < 4
      and a.results > 100
      and a."user" < 30000 ) c
 group by c."user", c.category



"HashAggregate  (cost=1685527.69..1686101.77 rows=28704 width=12)"
"  ->  Merge Join  (cost=148702.25..1286739.89 rows=53171707 width=12)"

"        Merge Cond: (b.domain = a.domain)"
"        ->  Index Scan using domain_categories_domain on domain_categories b  (cost=0.00..421716.32 rows=5112568 width=12)"
"              Filter: (depth < 4)"
"        ->  Sort  (cost=148415.16..148513.60 rows=39376 width=8)"

"              Sort Key: a.domain"
"              ->  Bitmap Heap Scan on result a  (cost=1249.93..145409.79 rows=39376 width=8)"
"                    Recheck Cond: ("user" < 30000)"
"                    Filter: (results > 100)"
"                    ->  Bitmap Index Scan on result_user_idx  (cost=0.00..1240.08 rows=66881 width=0)"
"                          Index Cond: ("user" < 30000)"



It shouldn't make a difference, but I've seen things like this help before so its worth a try.  Make sure work_mem is reasonably sized for this test.

It's exactly the same. work_mem was set to 3000MB.




Another thing that won't be that fast, but may avoid the sort, is to select the subselection above into a temporary table, analyze it, and then do the outer select.  Make sure your settings for temporary space (temp_buffers in 8.3) are large enough for the intermediate results (700MB should do it).  That won't be that fast, but it will most likely be faster than sorting 50 million + rows.  There are lots of problems with this approach but it may be worth the experiment.


I'll try this.

Thanks so far!


--
Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance

------=_Part_49013_9040357.1219329564811--