views:

19

answers:

1

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).

A: 

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 funtionality 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.

Aram Dulyan