The Missing Piece in Indexing

48 views
Skip to first unread message

Behzad

unread,
Nov 20, 2019, 6:49:18 AM11/20/19
to Django developers (Contributions to Django itself)
Hey all!

I noticed a while ago that we can set indexes in the models Meta as described here. But I can't find any commands on the Queryset which helps us use the declared indexes, the equivalent of `USE INDEX` in the DB.
Is this planned to be developed?

Best
Behzad

Adam Johnson

unread,
Nov 20, 2019, 7:05:33 AM11/20/19
to django-d...@googlegroups.com
Hi Behzad

Your database will automatically use the appropriate indexes for given queries, thanks to its optimizer. See https://en.m.wikipedia.org/wiki/Query_optimization and DB specific docs such as: https://www.postgresql.org/docs/9.0/planner-optimizer.html , https://dev.mysql.com/doc/internals/en/optimizer.html .

USE INDEX is only for forcing usage of particular indexes if you are *sure* that you know better than your optimizer. You don't need it in 99% of cases, it's a bit of a sledgehammer, and prone to bitrot into bad performance if your tables change. However if you're sure you want to do this, for MySQL/MariaDB, my library Django-MySQL adds a QuerySet extension for it: https://django-mysql.readthedocs.io/en/latest/queryset_extensions.html#django_mysql.models.use_index . I've actually never ended up needing it myself.

Thanks,

Adam

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/e95a9413-1c0f-48b2-ae25-5417c3a742e2%40googlegroups.com.


--
Adam
Reply all
Reply to author
Forward
0 new messages