X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id A243F3A1923 for ; Fri, 14 Jan 2005 09:39:43 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 43361-01 for ; Fri, 14 Jan 2005 09:39:31 +0000 (GMT) Received: from anchor-post-34.mail.demon.net (anchor-post-34.mail.demon.net [194.217.242.92]) by svr1.postgresql.org (Postfix) with ESMTP id BF4723A18F5 for ; Fri, 14 Jan 2005 09:39:32 +0000 (GMT) Received: from mwynhau.demon.co.uk ([193.237.186.96] helo=mainbox.archonet.com) by anchor-post-34.mail.demon.net with esmtp (Exim 4.42) id 1CpNvT-0003lf-DD; Fri, 14 Jan 2005 09:39:28 +0000 Received: from [192.168.1.17] (client17.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id 5156716571; Fri, 14 Jan 2005 09:39:26 +0000 (GMT) Message-ID: <41E7934E.2080509@archonet.com> Date: Fri, 14 Jan 2005 09:39:26 +0000 From: Richard Huxton User-Agent: Mozilla Thunderbird 1.0 (X11/20041206) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Madison Kelly Cc: pgsql-performance@postgresql.org Subject: Re: sum of all values References: <41E73D00.6030206@alteeve.com> In-Reply-To: <41E73D00.6030206@alteeve.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.077 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200501/181 X-Sequence-Number: 9871 Madison Kelly wrote: > Hi all, > > Is there a fast(er) way to get the sum of all integer values for a > certain condition over many thousands of rows? What I am currently doing > is this (which takes ~5-10sec.): OK, I'm assuming you've configured PG to your satisfaction and this is the only query giving you problems. > SELECT SUM (a.file_size) FROM file_info_1 a, file_set_1 b WHERE > a.file_name=b.fs_name AND a.file_parent_dir=b.fs_parent_dir AND > a.file_type=b.fs_type AND b.fs_backup='t'; You'll want to run EXPLAIN ANALYSE SELECT SUM... and post the output of that, although the query looks straightforward enough. > Here are the schemas, in case they help: > > tle-bu=> \d file_info_1 Table "public.file_info_1" > Column | Type | Modifiers > -----------------+---------+---------------------------- > file_acc_time | bigint | not null > file_group_name | text | not null > file_group_uid | integer | not null > file_mod_time | bigint | not null > file_name | text | not null > file_parent_dir | text | not null > file_perm | text | not null > file_size | bigint | not null > file_type | text | not null default 'f'::text > file_user_name | text | not null > file_user_uid | integer | not null > Indexes: > "file_info_1_display_idx" btree (file_parent_dir, file_name, file_type) > "file_info_1_search_idx" btree (file_parent_dir, file_name, file_type) > > tle-bu=> \d file_set_1 Table "public.file_set_1" > Column | Type | Modifiers > ---------------+---------+---------------------------- > fs_backup | boolean | not null default true > fs_display | boolean | not null default false > fs_name | text | not null > fs_parent_dir | text | not null > fs_restore | boolean | not null default false > fs_type | text | not null default 'f'::text > Indexes: > "file_set_1_sync_idx" btree (fs_parent_dir, fs_name, fs_type) 1. WHERE ARE YOUR PRIMARY KEYS??? 2. Why do you have two identical indexes on file_info_1 3. WHERE ARE YOUR PRIMARY KEYS??? 4. Am I right in thinking that always, file_name==fs_name (i.e. they represent the same piece of information) and if so, why are you storing it twice? Same for _parent_dir too 5. file_type/fs_type are being held as unbounded text? Not an index into some lookup table or a varchar(N)? Can you explain what you're trying to do here - it might be you want to alter your database design. -- Richard Huxton Archonet Ltd