general question about how best to cache expensive query results
- From: "j.k." <spaecious@xxxxxxxxx>
- Date: 2 May 2007 17:12:33 -0700
I asked this question over in mailing.database.pgsql-sql a couple of
days ago and got no answer. Perhaps the question was ill-phrased, or
perhaps that was not the best forum.
I'm reposting my question in here in the hope that someone can give me
some general advice or suggestions for further research.
--
I have a general question about how best to cache query results that
are too expensive to calculate each time they're needed.
The question is that when I have a query like:
SELECT
e.id, e.title
FROM
entries e
JOIN feeds f ON e.feed_id = f.id
WHERE
f.url = 'http://example.com'
AND e.id > (select id from entries where pub_date <
current_timestamp - interval '5 days' order by pub_date desc limit 1);
The last restriction in the query is the one that is too expensive to
calculate every time its needed. It's purpose is to restrict the
entries to those that are newer than a certain date, except that some
entries don't specify a date, so I use the fact that ids are in
ascending order in order of creation and find the highest id that's at
least 5 days old.
In actuality, I don't need the relevant e.id to be updated more than
once a day or so. It definitely does not need to be realtime, and is
prohibitively expensive to calculate the actual value.
What are general approaches to this kind of problem?
One obvious solution to me is to create a table for storing just this
value, but something seems wrong to me about potentially lots of
little 1-column, 1-row tables.
If there are multiple of this values that need to periodically
updated, then they could all be in 1 miscellaneous table, but then I
would have the problem of deciding what to make the primary key for
each row, and it would have to be hard-coded in the update and select
queries to distinguish this cached value from other cached values in
the same table, as well as dealing with values having different types.
I hope my question is clear enough. Thanks for any suggestions about
best strategies for dealing with this kind of issue.
.
- Follow-Ups:
- Re: general question about how best to cache expensive query results
- From: Ed Prochak
- Re: general question about how best to cache expensive query results
- From: Robert Klemme
- Re: general question about how best to cache expensive query results
- Prev by Date: Re: XML abuse?
- Next by Date: Re: XML abuse?
- Previous by thread: MVD and JD
- Next by thread: Re: general question about how best to cache expensive query results
- Index(es):
Relevant Pages
|