Django:为索引列指定HASH而不是BTREE [英] Django: Specifying HASH instead of BTREE for indexed column

查看:90
本文介绍了Django:为索引列指定HASH而不是BTREE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django模型中是否有一种很好的方法来指定特定的索引存储类型?



例如,MySQL的默认存储类型是BTREE,对于我的特定列,使用HASH(哈希表)作为存储类型可能更有效。 / p>

如果不创建自定义字段或修改django核心,我找不到一个好的方法,这将为我做到这一点。我也可以通过在创建表后修改索引来完成此操作。



这种情况对于大多数事情来说可能并不重要,但是在某些情况下,哈希表是一种更有效的查找机制,当然,在该列上进行排序是不必要的还是没有意义的。例如,具有随机生成的数据的列通常不会对信息进行合理的排序(除非您正在寻找可重复的随机排序-但这不重要)。

解决方案

查看 django.db.backends.mysql.creation.sql_indexes_for_field()的代码后,无法在Django本身中指定索引类型(除非您想对后端进行子类化,否则无法在查询中获取 USING 参数)。



解决此限制的最佳方法是按照表建议在创建表后修改索引。您可以使用Django的后端特定的初始值SQL数据功能。只需在您的应用程序中创建一个 sql目录,然后在其中创建一个名为< your_modelname> .mysql.sql 的SQL文件。然后在其中放置用于修改索引类型的SQL,然后Django将在影响模型表的 syncdb或 reset调用之后执行它。


Is there a good way within Django models to specify a specific index storage type?

For example, the default storage type for MySQL is BTREE, when for my particular column it may be more efficient to have HASH(hash table) as the storage type.

I can't find a good way without creating a custom field, or modifying django core, that will do this for me. I can also accomplish this by modifying the index after the table is created.

This situation probably doesn't matter for most things, but there are situations where a hash table is a more efficient lookup mechanism, and of course where sorting on the column is either not necessary or it doesn't make sense. For example a column with randomly generated data usually doesn't make a sensible ordering of information(unless you're looking for a repeatable random sort-- but that's beside the point).

解决方案

After a look at the code for django.db.backends.mysql.creation.sql_indexes_for_field(), it appears that there is no way to specify an index type in Django itself (there's no way to get a USING parameter into the query, unless you want to subclass the backend).

The best way around this limitation is to modify the index after the table has been created, as you suggested. You can use Django's backend-specific initial SQL data functionality for that. Simply create an "sql" directory in your app, and create an SQL file called <your_modelname>.mysql.sql inside it. Then put the SQL for modifying the index type in there, and Django will execute it after "syncdb" or "reset" calls affecting your model's table.

这篇关于Django:为索引列指定HASH而不是BTREE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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