List Info

Thread: ORDER BY on get_object?




ORDER BY on get_object?
user name
2006-02-25 14:07:51
If I specify an 'ordering' attribute in my model's META
class, it gets 
used even for single-object selects, for example,
get_object.

So there are SQL queries like this:

SELECT field, field FROM app_model WHERE id = 50 ORDER BY
date DESC

Surely the ORDER BY clause can be omitted?  Is the
magic-removal branch 
already fixing this?

-- 
Ned Batchelder, http://nedbatchelder.com

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

ORDER BY on get_object?
user name
2006-02-25 14:26:33
On Saturday 25 February 2006 14:07, Ned Batchelder wrote:

> So there are SQL queries like this:
>
> SELECT field, field FROM app_model WHERE id = 50 ORDER
BY date DESC
>
> Surely the ORDER BY clause can be omitted?  Is the
magic-removal
> branch already fixing this?

Yep, the order by clause could be omitted.  However, in
magic-removal 
(and I guess on trunk) the same mechanism is used to get
single objects 
and multiple objects - for single objects you just filter on
the value 
of the primary key.  So removing the ORDER BY would be more
trouble 
than it's worth - you would have to add a special case for
it (and the 
code to check for it might not be completely trivial), and
it wouldn't 
have any effect on the results.  I can't imagine it would
have much 
effect on performance either, but I don't know much about
DB 
optimisation.  If you can show that it does affect
performance on a 
popular DB then there would some motivation to change this.

Regards,

Luke

-- 
Parenthetical remarks (however relevant) are unnecessary

Luke Plant || L.Plant.98 (at) cantab.net || http://lukeplant.me.uk/

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

ORDER BY on get_object?
user name
2006-02-25 15:59:34
On 2/25/06, Ned Batchelder <nednedbatchelder.com>
wrote:
> If I specify an 'ordering' attribute in my model's
META class, it gets
> used even for single-object selects, for example,
get_object.
>
> So there are SQL queries like this:
>
> SELECT field, field FROM app_model WHERE id = 50 ORDER
BY date DESC
>
> Surely the ORDER BY clause can be omitted?  Is the
magic-removal branch
> already fixing this?

Thanks for pointing this out. You can remove the ORDER BY
clause
entirely by passing an empty tuple or list as the order_by
argument to
get_object():

    someapp.get_object(pk=50, order_by=())

But that's a pretty lame hack.

It would be easy to change the behavior for get_object() so
that it
doesn't use the ordering clause from the model. My only
hesitation is
that it would slightly change the meaning of get_object(),
because
some people might be using, for example,
get_object(limit=1), relying
on the fact that get_object() would be using the model's
ordering.
However, I think this is an acceptable change, as long as we
document
the change on the backwards-incompatible changes page. Any
objections
before I check in that change to trunk?

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

ORDER BY on get_object?
user name
2006-02-25 17:29:07
As of revision 2392 in trunk, I've made this change. ORDER
BY is no
longer added for get_object() queries, unless you explicitly
specify
it. Thanks again for suggesting, Ned!

Adrian


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )