Re: How can I reduce the number of queries to my PostgreSQL database?
- From: "SR" <shay@xxxxxxxxxxxxxxxxxxxxxx>
- Date: 11 Apr 2006 15:06:31 -0700
Martin Christensen said:
SR> Scenario: I have a python script which creates web page listing
SR> all books in the database, and all authors for each book. My
SR> python script does essentially three things:
SR> 1. retrieve a list of all book_ids and book_titles.
SR> 2. for each book_id, query the bookauthors table and retrieve all
SR> author names for that book_id.
SR> 3. display it all out as an html table on a web page.
That's one query, if you're willing to make it advanced enough,
although you need to make an aggregate to enable PostgreSQL to
concatenate and comma separate author names. However, this aggregate
will typically need more than one database function. Such an aggregate
could be as follows:
<SNIP>
This is the solution that I would use after working nearly a decade
with databases. It is neither simple nor obvious to the novice, but
it's the Right Way To Do It. For a learning exercise, this is way over
the top, but I thought you might benefit from seeing that - so long as
you only need information that would reasonably fit in one table on a
web page or the equivalent - one query is always enough. Or perhaps
that should be One Query Is Always Enough. :-) Learn at your own pace,
though, but you might want to keep this in mind for future reference.
Thanks for that... I'm not going to argue with a decade's experience!
I'd never heard of aggregates before, but I'll look into them. Perhaps
I'll be able to impress my friends with them one day.
The reason for keeping the authors separate was to wrap them with an
appropriate HTML href, but presumably your solution could be adapted
for this purpose?
Cheers,
Shay
.
- References:
- How can I reduce the number of queries to my PostgreSQL database?
- From: SR
- Re: How can I reduce the number of queries to my PostgreSQL database?
- From: Martin Christensen
- How can I reduce the number of queries to my PostgreSQL database?
- Prev by Date: Re: list.clear() missing?!?
- Next by Date: Re: Removing an item from a QListView in PyQt
- Previous by thread: Re: How can I reduce the number of queries to my PostgreSQL database?
- Next by thread: Re: How can I reduce the number of queries to my PostgreSQL database?
- Index(es):
Relevant Pages
|