Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Preventing JOIN while checking if a self referencing FK is null
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
John Boxall  
View profile  
 More options Nov 4, 2:06 pm
From: John Boxall <bisha...@gmail.com>
Date: Wed, 4 Nov 2009 11:06:05 -0800 (PST)
Local: Wed, Nov 4 2009 2:06 pm
Subject: Preventing JOIN while checking if a self referencing FK is null
Hey Djangonauts,

I'd like to optimize the performance of a query I've got running on a
page - I've got a model which has a self referencing nullable foreign
key and I'd like to select all instances of that model that which have
a value for that column.

Imagine a class Person with a nullable FK to Person "Best Friends
Forever" - I'd like to select everyone who has a BFF! This is a
straight forward query operation and I would expect the ORM to avoid
using a JOIN to find it out - it would seem you could just check
whether the bff_id isnull - but in all the queries I've tried, the ORM
wants to do a JOIN on Person, which is expensive and unnecessary.

So my question is - how do you prevent a JOIN while checking if a self
referencing FK is null?

# Begin example codes #

class Person(model.Models):
   bbf = models.ForeignKey("self", null=True)

...

# Returns a queryset which causes a join
people_with_bffs = Person.objects.filter(bbf__isnull=False)

# Returns a queryset which causes a join
people_with_bffs = Person.objects.filter(bbf__pk__isnull=False)

# Returns a queryset which doesn't cause a join?
people_with_bffs = Person.objects.filter( ??? )

Cheers,

John


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Javier Guerra  
View profile  
 More options Nov 4, 2:13 pm
From: Javier Guerra <jav...@guerrag.com>
Date: Wed, 4 Nov 2009 14:13:59 -0500
Local: Wed, Nov 4 2009 2:13 pm
Subject: Re: Preventing JOIN while checking if a self referencing FK is null
what about

people_with_bffs = Person.objects.filter(bbf_id__isnull=False)

?

(disclaimer, i haven't tried it)

--
Javier


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Boxall  
View profile  
 More options Nov 4, 2:22 pm
From: John Boxall <bisha...@gmail.com>
Date: Wed, 4 Nov 2009 11:22:34 -0800 (PST)
Local: Wed, Nov 4 2009 2:22 pm
Subject: Re: Preventing JOIN while checking if a self referencing FK is null
Both of these will raise a FieldError:

people_with_bffs = Person.objects.filter(bbf_id__isnull=False)
people_with_bffs = Person.objects.filter(bbf_pk__isnull=False)

Cannot resolve keyword 'bbf_pk' into field. Choices are 'bbf'.

Thanks for the suggestion though!

jb

On Nov 4, 11:13 am, Javier Guerra <jav...@guerrag.com> wrote:


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel Roseman  
View profile  
 More options Nov 4, 2:32 pm
From: Daniel Roseman <dan...@roseman.org.uk>
Date: Wed, 4 Nov 2009 11:32:19 -0800 (PST)
Local: Wed, Nov 4 2009 2:32 pm
Subject: Re: Preventing JOIN while checking if a self referencing FK is null
On Nov 4, 7:22 pm, John Boxall <bisha...@gmail.com> wrote:

> Both of these will raise a FieldError:

> people_with_bffs = Person.objects.filter(bbf_id__isnull=False)
> people_with_bffs = Person.objects.filter(bbf_pk__isnull=False)

> Cannot resolve keyword 'bbf_pk' into field. Choices are 'bbf'.

> Thanks for the suggestion though!

> jb

Try Person.objects.exclude(bbf_id=None)
--
DR.

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Boxall  
View profile  
 More options Nov 4, 2:40 pm
From: John Boxall <bisha...@gmail.com>
Date: Wed, 4 Nov 2009 11:40:42 -0800 (PST)
Local: Wed, Nov 4 2009 2:40 pm
Subject: Re: Preventing JOIN while checking if a self referencing FK is null
Thanks for the suggestion Daniel, but that doesn't help either :\

This will raise a field error (bff_id is not a valid field, only bff
is)
Person.objects.exclude(bff_id=None)

And these both result in JOINs:
Person.objects.exclude(bff=None)
Person.objects.exclude(bff__id=None)

Perhaps there is an optimization to be made at the Django ORM level?

jb

On Nov 4, 11:32 am, Daniel Roseman <dan...@roseman.org.uk> wrote:


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google