X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 782319FA5E6 for ; Tue, 30 May 2006 12:06:07 -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 25840-04 for ; Tue, 30 May 2006 12:06:01 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 605DD9FA5E2 for ; Tue, 30 May 2006 12:06:00 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.6/8.13.6) with ESMTP id k4UF5uZ7023932; Tue, 30 May 2006 11:05:56 -0400 (EDT) To: mario.splivalo@mobart.hr cc: pgsql-performance@postgresql.org Subject: Re: Speedup hint needed, if available? :) In-reply-to: <1148998681.18422.24.camel@localhost.localdomain> References: <1148998681.18422.24.camel@localhost.localdomain> Comments: In-reply-to Mario Splivalo message dated "Tue, 30 May 2006 16:18:01 +0200" Date: Tue, 30 May 2006 11:05:55 -0400 Message-ID: <23931.1149001555@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200605/524 X-Sequence-Number: 19311 Mario Splivalo writes: > Here is the query which gets information on particular user, shows > subscriptions to mailinglists and available credits on those > mailinglists: > SELECT u.subscription_id, u.user_id, u.mailinglist_id, u.valid_from, > u.valid_to, sum(credits.credits_given - credits.credits_taken)::integer > AS credits > FROM user_subscriptions u > LEFT JOIN > (SELECT user_subscription_credits_given.subscription_id, > user_subscription_credits_given.credits AS credits_given, 0 AS > credits_taken > FROM user_subscription_credits_given > UNION ALL > SELECT user_subscription_credits_taken.subscription_id, 0 AS > credits_given, user_subscription_credits_taken.credits AS credits_taken > FROM user_subscription_credits_taken) credits > ON u.subscription_id = credits.subscription_id > where > u.user_id = 1 > GROUP BY u.subscription_id, u.user_id, u.mailinglist_id, u.valid_from, > u.valid_to Do you have realistic test data? The EXPLAIN shows that this is pulling 275366 of the 826032 rows in the two tables, which seems like rather a lot for a single user. If it's reasonable that the query needs to fetch one-third of the data, then you should resign yourself to it taking awhile :-( If the expected number of matching rows were much smaller, it would make sense to use indexscans over the two big tables, but unfortunately existing PG releases don't know how to generate an indexscan join with a UNION ALL in between :-(. FWIW, 8.2 will be able to do it. In current releases the only thing I can suggest is to merge user_subscription_credits_given and user_subscription_credits_taken into one table so you don't need the UNION ALL. regards, tom lane