Hello,

I would like to retrieve the most recently published story from the
database. The criterion date is not a field, but sits in a separate
model connected by a ManyToMany field.

# this works fine
latest_story = Story.order_by('-publish_dates__publish_date')[0]

# this also works, seems slightly faster and is nicer to read
latest_story = Story.latest('publish_dates__publish_date')

Problem is that the query takes 30s to execute with just 3000 objects
in the database.

I can not use values(...), because that only works with fields, not
with ManyToManyField attributes.

I have played with the raw SQL and noticed that there is an ANDed test
in the WHERE part that checks if the story publish date & time <=
current date & time. This seems redundant to me and when I remove it,
the execution times drop to less than a second. I would prefer not to
resort to raw SQL yet, though and I believe that there must be a way
to do this query using the Django API in an efficient manner.

Searching for 'slow' in this group and reading e.g.
http://www.ibm.com/developerworks/opensource/library/os-django-models/index.html,
I am getting the impression that it is quite common to run into
performance issues with 'naive' queries once a larger DB is used. I
could not find a solution how to speed up the query, though. Are there
any common practices how to deal with this ?

Your help would be greatly appreciated :-)

Cheers,

Steve

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

Reply via email to