I've tried to use ideas from:
    http://code.djangoproject.com/attachment/wiki/CookBookPreloadRelated/db.py

so:
    oids=[obj._get_pk_val() for obj in Book.objects.all()]
    c={'co_book_set__in': oids}
    Author.objects.filter(**c).select_related(True)
but this generates:
    300 queries
and the result is not being cached.

I had thought:
    b.co_authors.all()
would now be populated, but apparently it isn't.

Is there any way I could reduce the number of DB query to 1 (or a mere
few)?

JJ.

FYI, the model is as follows:
    class Book(models.Model):
        title = CharField()
        main_author = ForeignKey(Author, related_name='main_book_set')
        co_authors = ManyToManyField(Author,
related_name='co_book_set')

    class Author(models.Model):
        initials = CharField()
        employee_id = SmallPositiveInteger()
        user = ForeignKey(User)
On Jul 4, 1:59 pm, jj <[EMAIL PROTECTED]> wrote:
> On Jul 2, 8:00 pm, "Jeremy Dunck" <[EMAIL PROTECTED]> wrote:
>
> > Sorry, I meant how many HTTP requests *per page*, as in, how many
> > external resource references?
>
> - 13 unique external images
> - 4 stylesheets (changelists.css from contrib/admin)
>
> and I just noticed I create:
> - 4000 HTML anchors
>
> > Are you serving media off a separate server, or though apache?  If
> > apache, are you using it's regular file service or running it through
> > Django?
>
> Through Apache's regular file service (as described in the tutorial).
>
> > > 1, essentially: Book.objects.select_related(). Plus access to 5
> > > foreign objects per book.
>
> > with settings.DEBUG = True:
>
> > from django.db import connection
> > print connection.queries
>
> > May give you a clue.
>
> I've now instrumented as you suggest:
>     db.reset_queries()
>     book_list = list(Book.objects.select_related())
>     l0 = len(book_list)
>     l1 = len(db.connection.queries)
>     response = render_books(request, book_list, title='All books')
>     l2 = len(db.connection.queries) - l1
>     response.write('<span class="mini quiet">(%s, %s, %s)</span>' %
> (l0, l1, l2))
>     return response
>
> and the amazing response is:
>     (l0: 225, l1: 1, l2: 2630)
> !!!
>
> I'm quite at lost here. What am I doing wrong?
>
> > >     book_list = cache.get('all_books')
> > >     if not book_list:
> > >         book_list = Book.objects.select_related()
> > >         cache.set('all_books', book_list)
>
> > This doesn't actually cache the result set.  Querysets are lazy.
> > If you want to cache the finished results, try this:
>
> >      book_list = cache.get('all_books')
> >      if not book_list:
> >          book_list = list(Book.objects.select_related())
>
> Thanks. I've now tried the above, no performance gain however. Could
> this be related to the issue above?
>
> >          cache.set('all_books', book_list)
>
> > >     book_list = [b for b in book_list if b.is_visible_by(user)]
>
> >      Concur with Tim that this may be worth making a DB function and
> > using extra() on.
>
> > Depends which DB you're using, really?
>
> Initially, sqlite3, but I've now switched to MySQL 5.
>
> JJ.
>
> On Jul 2, 8:00 pm, "Jeremy Dunck" <[EMAIL PROTECTED]> wrote:
>
> > On 7/2/07, jj <[EMAIL PROTECTED]> wrote:
>
> > > > How many HTTP requests?
>
> > > It's only me testing at the moment, and it's already slow enough.
>
> > Sorry, I meant how many HTTP requests *per page*, as in, how many
> > external resource references?
>
> > Are you serving media off a separate server, or though apache?  If
> > apache, are you using it's regular file service or running it through
> > Django?
>
> > > 1, essentially: Book.objects.select_related(). Plus access to 5
> > > foreign objects per book.
>
> > with settings.DEBUG = True:
>
> > from django.db import connection
> > print connection.queries
>
> > May give you a clue.
>
> > >     book_list = cache.get('all_books')
> > >     if not book_list:
> > >         book_list = Book.objects.select_related()
> > >         cache.set('all_books', book_list)
>
> > This doesn't actually cache the result set.  Querysets are lazy.
> > If you want to cache the finished results, try this:
>
> >      book_list = cache.get('all_books')
> >      if not book_list:
> >          book_list = list(Book.objects.select_related())
> >          cache.set('all_books', book_list)
>
> > >     book_list = [b for b in book_list if b.is_visible_by(user)]
>
> >      Concur with Tim that this may be worth making a DB function and
> > using extra() on.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to