pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feeddesign question: status table+log table, indexes, triggers
2+ messages / 2 participants
[nested] [flat]
* design question: status table+log table, indexes, triggers
@ 2003-02-05 18:51 george young <gry@ll.mit.edu>
2003-02-05 19:07 ` Re: design question: status table+log table, indexes, triggers Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 2+ messages in thread
From: george young @ 2003-02-05 18:51 UTC (permalink / raw)
To: pgsql-sql
[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"
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: design question: status table+log table, indexes, triggers
2003-02-05 18:51 design question: status table+log table, indexes, triggers george young <gry@ll.mit.edu>
@ 2003-02-05 19:07 ` Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 2+ messages in thread
From: Tom Lane @ 2003-02-05 19:07 UTC (permalink / raw)
To: gry@ll.mit.edu; +Cc: pgsql-sql
george young <gry@ll.mit.edu> writes:
> 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);
You can do this a lot better with SELECT DISTINCT ON --- see the "weather
report" example in the SELECT reference page. Given a suitable index
it should even be pretty quick.
regards, tom lane
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2003-02-05 19:07 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-02-05 18:51 design question: status table+log table, indexes, triggers george young <gry@ll.mit.edu>
2003-02-05 19:07 ` Tom Lane <tgl@sss.pgh.pa.us>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox