X-Original-To: pgsql-sql@postgresql.org Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 11CF74758E1 for ; Wed, 5 Feb 2003 12:49:46 -0500 (EST) Received: from ll.mit.edu (LLMAIL.LL.MIT.EDU [129.55.12.40]) by postgresql.org (Postfix) with ESMTP id 576C44758DC for ; Wed, 5 Feb 2003 12:49:45 -0500 (EST) Received: (from smtp@localhost) by ll.mit.edu (8.11.3/8.8.8) id h15HnnH28055 for ; Wed, 5 Feb 2003 12:49:49 -0500 (EST) Received: from sty.llan.ll.mit.edu( ), claiming to be "sty.llan" via SMTP by llpost, id smtpdAAA4va4qX; Wed Feb 5 12:49:33 2003 Date: Wed, 5 Feb 2003 13:51:37 -0500 From: george young To: pgsql-sql@postgresql.org Subject: design question: status table+log table, indexes, triggers Message-Id: <20030205135137.4abc14b5.gry@ll.mit.edu> Reply-To: gry@ll.mit.edu Organization: MIT Lincoln Laboratory X-Mailer: Sylpheed version 0.8.5 (GTK+ 1.2.10; i686-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200302/99 X-Sequence-Number: 11497 [postgresql-7.2, pgsql, linux] Here's a schema-design problem I've hit a few times -- it seems like there should be a better way: I have a machine table (140 rows), currently very static: machine(machine_name text NOT NULL, machine_id smallint NOT NULL, area text NOT NULL, text text NOT NULL); and a machine_log table (8400 rows), appended to ~4 times/hour: machine_log(machine_name text,date timestamp, status text, usr text, comment text); This schema seemed logical at the outset, but the most common query is: select m.machine_name, m.text, ml.status, ml.date from machine m, machine_log ml where m.machine_name=ml.machine_name and ml.date=(select max(date)from machine_log where machine_name=ml.machine_name); This takes ~25 seconds which is way too long for interactive status check. The max(date) subselect kills me -- I've tried various indexes but to no avail. It looks like I need to put status and date_changed columns into the machine table, even though that info is implicit in the machine_log table. Is there some other schema that I'm just not thinking of which neatly stores some static info about each of a list of things as well as log info of status changes of those things? Or is there some index I could create on machine_log that would do the above query fast? Finally, I've never used triggers or rules -- what's the best way to maintain the status and date_changed columns in "machine" automatically when "machine_log" is appended to? Thanks, George -- I cannot think why the whole bed of the ocean is not one solid mass of oysters, so prolific they seem. Ah, I am wandering! Strange how the brain controls the brain! -- Sherlock Holmes in "The Dying Detective"