Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a good practice to add indexing on a boolean field in a Django model

Suppose we have model with boolean field:

class AModel(models.Model):
    flag = models.BoleanField()

Is there any reason to add index on this field?

I think it hasn't reason because there will be small profit in search(this only split two) but large overhead with recording. But my colleague thinks different.

Is there any rule of thumb for this?

like image 942
kharandziuk Avatar asked Oct 23 '13 12:10

kharandziuk


People also ask

How do I index a field in Django?

By default, indexes are created with an ascending order for each column. To define an index with a descending order for a column, add a hyphen before the field's name. For example Index(fields=['headline', '-pub_date']) would create SQL with (headline, pub_date DESC) .

Does Django create indexes?

If you run this migration as it is, then Django will create the index on the table, and it will be locked until the index is completed. It can take a while to create an index on a very large table, and you want to avoid downtime.

What is Boolean field in Django model?

The default form widget for this field is CheckboxInput, or NullBooleanSelect if null=True. The default value of BooleanField is None when Field. default isn't defined. One can define the default value as true or false by setting the default attribute to true/false simultaneously.

Is Boolean field in Django?

BooleanField in Django Forms is a checkbox field which stores either True or False. It is used for taking boolean inputs from the user. The default widget for this input is CheckboxInput. It normalizes to: A Python True or False value.


1 Answers

It depends.

If you had data that has predominantly one or other of the boolean values (ie, almost everything is FALSE), but you want to commonly query only values that match the other (ie, query only for TRUE values), then an index on the boolean field could make a very big difference to performance: especially if you force the index to store the TRUE values first.

The trick there is that the field (and index) is selective, because you can discard most of the rows, and so the index can be used to speed this up, by storing the ones that are useful.

Alternatively, you could have an index on a different field (that you are also querying on, perhaps) that uses a WHERE <boolean-field> clause to only store the true values (or vice-versa, depending upon your needs). I haven't tried it, but I'd wager WHOLE DOLLARS that Postgres is able to use this index correctly...

like image 194
Matthew Schinckel Avatar answered Oct 22 '22 22:10

Matthew Schinckel