I have a Place model:
class Place(models.Model):
... some not interesting fields
distances = models.JSONField()
In JSONField (distances) stores something like:
{
'1': 10
'2': 20
'3': 30
'4': 40
...
'1000': 10000
}
where key is 'id' of some Location model, and value - 'time in road' to this Location from this Place
If website user selects Location, I show Places in order from closest to further to selected Location:
places = Place.objects.all().order_by(
RawSQL('distances->%s', (str(selected_location_id),))
)
And everything is cool, except performance.
But, PostgreSQL support indexing for JSONB fields (which use Django for JSONField):
GIN, btree and hash
- What differences between them? Which is suitable for my example?
- Does django support db_index on JSONField? If yes, how to specify which index to use?
Big thx for help!