Search code examples
mysqldjangodjango-modelsmodel

How do I specify type of index in django? (btree vs hash vs etc)


Like the title says, how do I specify the type of index I want on a field in a model in django?

class Person:
     ...
     age = models.IntegerField(db_index=True)

What now? How do I ensure that it is a btree index and not a hash index? Is this process automated by using some large table that Django uses to determine the optimal index type internally? Ideally, I would like a manual way to change these settings.


Solution

  • Django defaults to creating btree indexes whenever you specify index=True:

    https://docs.djangoproject.com/en/1.11/ref/models/indexes/

    I note you're using MySQL; but if you're using PostgreSQL, you can specify certain other index types on certain types of fields:

    https://docs.djangoproject.com/en/1.11/ref/contrib/postgres/indexes/

    This answer gives information on how you can override the btree default in MySQL, should you ever need to:

    https://stackoverflow.com/a/3288059/1394697