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 4BA589FA5D2 for ; Wed, 31 May 2006 06:30:10 -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 45927-01 for ; Wed, 31 May 2006 06:30:01 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from fw.greenpeace.org (fw.greenpeace.org [212.19.215.77]) by postgresql.org (Postfix) with ESMTP id C7D1E9F9312 for ; Wed, 31 May 2006 06:30:00 -0300 (ADT) Received: (from root@localhost) by fw.greenpeace.org (8.9.3c/8.6.12) id LAA65837 for ; Wed, 31 May 2006 11:29:40 +0200 (CEST) Received: by fw.greenpeace.org (TUNIX/Firewall SMTP Server) for id sma064182; Wed, 31 May 06 11:27:49 +0200 Received: from bb.nli.gl3 (root@localhost) by bb.nli.gl3 (8.12.8/8.12.5) with SMTP id k4V9RnGA025738 for ; Wed, 31 May 2006 11:27:49 +0200 Received: from maila.greenpeace.org (fw.nli.gl3 [192.168.31.7]) by bb.nli.gl3 (8.12.8/8.13.3) with ESMTP id k4V9Rn7j025729; Wed, 31 May 2006 11:27:49 +0200 Received: from [192.168.200.143] (vpni.greenpeace.org [212.19.215.76]) (authenticated (128 bits)) by maila.greenpeace.org (8.13.4/8.11.6) with ESMTP id k4V9Rnb7002698 (using TLSv1/SSLv3 with cipher RC4-MD5 (128 bits) verified NO); Wed, 31 May 2006 11:27:49 +0200 Message-ID: <447D61BA.1040201@superlativ.dk> Date: Wed, 31 May 2006 11:28:26 +0200 From: Nis Jorgensen User-Agent: Thunderbird 1.5.0.2 (Windows/20060308) MIME-Version: 1.0 To: mario.splivalo@mobart.hr CC: pgsql-performance@postgresql.org Subject: Re: Speedup hint needed, if available? :) References: <1148998681.18422.24.camel@localhost.localdomain> In-Reply-To: <1148998681.18422.24.camel@localhost.localdomain> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Anti-Virus: Kaspersky Anti-Virus for MailServers 5.5.2/RELEASE, bases: 25072005 #131892, status: clean X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200605/565 X-Sequence-Number: 19352 Mario Splivalo wrote: > Hello again. > > I have to track user subscriptions to certain mailinglists, and I also > need to track credits users have on those mailinglists. On one side I > have procedures that add credits, on other side I have procedures that > subtract available credits. Add/subtract is pretty intensive, around > 30-50 adds per minute (usualy 10 or 100 credits), and around 200-500 > subtracts per minute (usualy by one or two credits). > > I have created table user_subscriptions to track user subscriptions to > certain mailing list. I have derived subscription_id as primary key. I > have two other tables, user_subscription_credits_given, and > _credits_taken, wich track credits for subscription added or subtracted > to or from certain subscription. I created those two tables so I could > eliminate a lot of UPDATES on user_subscriptions table (if I were to > have a column 'credits' in that table). It sounds to me like you have decided beforehand that the obvious solution (update a credit field in the user_subscriptions table) is not going to perform well. Have you tried it? How does it perform? If it does indeed give you performance problems, you could instead run some kind of batch job to update the credits field (and delete the /given/taken records). Finally: You could refactor the query to get rid of the union: SELECT u.subscription_id, u.user_id, u.mailinglist_id, u.valid_from, u.valid_to, ( SELECT sum(credits) FROM credits_given WHERE subscription_id = u.subscription_id ) - ( SELECT sum(credits) FROM credits_taken WHERE subscription_id = u.subscription_id) ) AS credits FROM user_subscriptions u WHERE u.user_id = 1 (Not tested). You will probably need a COALESCE around each of the subqueries to avoid problems with nulls. The sum of an empty set of numbers is 0. The conjunction of an empty set of booleans is true. The SQL standard somehow manages to get this wrong /Nis