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 9023C3A5A5E for ; Fri, 14 Jan 2005 03:35:11 +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 92336-01 for ; Fri, 14 Jan 2005 03:35:00 +0000 (GMT) Received: from tomts13-srv.bellnexxia.net (tomts13-srv.bellnexxia.net [209.226.175.34]) by svr1.postgresql.org (Postfix) with ESMTP id EB8E63A536F for ; Fri, 14 Jan 2005 03:34:58 +0000 (GMT) Received: from [70.48.36.251] by tomts13-srv.bellnexxia.net (InterMail vM.5.01.06.10 201-253-122-130-110-20040306) with ESMTP id <20050114033457.IUBY1899.tomts13-srv.bellnexxia.net@[70.48.36.251]> for ; Thu, 13 Jan 2005 22:34:57 -0500 Message-ID: <41E73D00.6030206@alteeve.com> Date: Thu, 13 Jan 2005 22:31:12 -0500 From: Madison Kelly User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.3) Gecko/20041020 X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: sum of all values Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200501/174 X-Sequence-Number: 9864 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.): 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'; I need to keep parts of the data in two tables. I currently use 'file_name/fs_name', 'file_parent_dir/fs_parent_dir' and 'file_type/fs_type' to match the entries in the two tables. The 'file_info_#' table is frequently dropped and re-created so this was the only way I could think to match the data. I am hoping that maybe there is something I can do differently that will return this value a lot faster (ideally within a second). I know that this is heavily dependant on the system underneath but the program is designed for Joe/Jane User so I am trying to do what I can in the script and within my DB calls to make this as efficient as possible. I realise that my goal may not be viable. 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) Thanks all! Madison