如何在Django中指定索引类型? (btree vs哈希等) [英] How do I specify type of index in django? (btree vs hash, etc)

查看:145
本文介绍了如何在Django中指定索引类型? (btree vs哈希等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像标题中所说的那样,如何在Django模型中的字段上指定所需的索引类型.

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)

但是现在呢?如何确保它是一个btree索引而不是一个hash.还是全部为我们自动完成,并且django使用了一些大表来选择最佳索引类型"

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"

推荐答案

Django默认在您指定index=True时创建btree索引:

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

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

我注意到您正在使用MySQL;但是如果您使用的是PostgreSQL,则可以在某些类型的字段上指定其他某些索引类型:

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/

此答案提供了有关如何在需要时覆盖MySQL中的btree默认值的信息:

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

这篇关于如何在Django中指定索引类型? (btree vs哈希等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆