djangonauts / django-hstore

PostgreSQL HStore support for Django.
http://django-hstore.readthedocs.io/
Other
517 stars 142 forks source link

Why contains does not uses @> operator #96

Open rodo opened 9 years ago

rodo commented 9 years ago

Hi,

I probably miss something, I'm a end user of django-hstore and just discover that when doing

Cerise.objects.filter(data__contains={'org': 'foo'})

the query executed use the = operator instead of @> that I presume

WHERE ("cerise_cerise"."data"->'soc') = 'breme'

When I would see

WHERE data @> 'soc=>"breme"'::hstore;

If I do not miss anything, will you be open for a path to use @> operator, that is really more efficient with right indexes (not in this obvious case)

nemesifier commented 9 years ago

hi @rodo, sorry for the delayed answer.

We are open for improvements.

@niwibe @fabiocorneti any thoughts on this?

assiotis commented 9 years ago

Looking at the code in query.py (verified), this behavior seems to kick in when you are querying on more than one key

nemesifier commented 9 years ago

Another place to look at is: https://github.com/djangonauts/django-hstore/blob/master/django_hstore/lookups.py

Unfortunately there is some code duplication because for django >= 1.7 the new lookups are used.

nemesifier commented 9 years ago

BTW @rodo we are open for improvements

wolever commented 9 years ago

This is a fairly significant issue, as GIN/GiST indexes are only used with @>:

> explain select * from people where fields->'first_name' = 'David'
Seq Scan on people  (cost=0.00..44.84 rows=5 width=113)
  Filter: ((fields -> 'first_name'::text) = 'David'::text)

> explain select * from people where fields @> 'first_name=>David'
Index Scan using people_fields_gist on people  (cost=0.00..8.27 rows=1 width=113)
  Index Cond: (fields @> '"first_name"=>"David"'::hstore)

Ideally there would be syntax which would make the operator (== versus @>) explicit so the query could be tailored to the underlying indexes.

nemesifier commented 9 years ago

Practical proposals still welcome.