1

I'm just curious. What is the correct way or should I say, the efficient way in filtering data? I'm still learning. Say you have thousands of records then you want to retrieve all active records.

Should it be done like this.

Records.objects.all().exclude(active=False)

Or

Records.objects.filter(active=True)

Do they have a difference or just the same?

Boy Pasmo
  • 8,021
  • 13
  • 42
  • 67

1 Answers1

1

These two expressions produce different queries.

You always can inspect them by looking at the query attribute of a queryset:

>>> print Records.objects.all().exclude(active=False).query

Records.objects.all().exclude(active=False) produces:

SELECT 
    <list_of_fields> 
FROM 
    `<table_name>` 
WHERE 
    NOT (`<table_name>`.`active` = False)

Records.objects.filter(active=True) produces:

SELECT 
    <list_of_fields> 
FROM 
    `<table_name>` 
WHERE 
    `<table_name>`.`active` = True

See also:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Between the two, which do you prefer? – Boy Pasmo Apr 04 '14 at 15:22
  • @BoyPasmo for me, making `filter(active=True)` is more explicit and clear. – alecxe Apr 04 '14 at 15:24
  • @BoyPasmo plus, it produces a bit more straightforward query, no inversions. In terms of speed, I think database "engines" like mysql are smart enough to optimize both of them and run in a same way. – alecxe Apr 04 '14 at 15:27
  • @BoyPasmo you can also take a look at the `EXPLAIN` plan and see if there is a difference. – alecxe Apr 04 '14 at 15:27
  • @BoyPasmo in case of mysql, take a [look](https://dev.mysql.com/doc/refman/5.0/en/explain.html). – alecxe Apr 04 '14 at 15:29