问题
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)
But now what? How do I make sure it is a btree
index and not a hash
. Or is this all done automatically for us and there is some large table that django uses for choosing the "optimal index type"
回答1:
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
来源:https://stackoverflow.com/questions/45125741/how-do-i-specify-type-of-index-in-django-btree-vs-hash-etc