MySQL Django:创建索引:非唯一,多列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1578195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:17:24  来源:igfitidea点击:

Django: create Index: non-unique, multiple column

mysqldjangoindexing

提问by jbastos

Given the following model, I want to index the fields (sequence,stock)

鉴于以下模型,我想索引字段(序列,库存)

class QuoteModel(models.Model):  
    quotedate =  models.DateField()  
    high = models.FloatField() #(9,2) DEFAULT NULL,  
    low  = models.FloatField() #(9,2) DEFAULT NULL,  
    close  = models.FloatField() #(9,2) DEFAULT NULL,  
    closeadj  = models.FloatField() #(9,2) DEFAULT NULL,  
    volume  = models.IntegerField() #(9,2) DEFAULT NULL,  
    stock  = models.IntegerField(db_index=True) #(9,2) DEFAULT NULL,  
    open  = models.FloatField() #(9,2) DEFAULT NULL,  
    sequence = models.IntegerField() #(9,2) DEFAULT NULL,  

This index should be non-unique - in mysql it should be something like:

这个索引应该是非唯一的——在 mysql 中它应该是这样的:

create index ndx_1 on model_quotemodel(sequence,stock);

The only Django workaround I know of is creating an "sql" file that will be executed by django upon table creation. So, I created a "stockmodel.sql" containing the following query (same as above:)

我所知道的唯一 Django 解决方法是创建一个“sql”文件,该文件将由 django 在创建表时执行。所以,我创建了一个包含以下查询的“stockmodel.sql”(同上:)

create index ndx_1 on model_quotemodel(sequence,stock);

Is there any "cleaner" way of doing it?

有什么“更干净”的方法吗?

采纳答案by excieve

As of Django 1.5, you can use the Meta.index_togetheroption:

从 Django 1.5 开始,您可以使用Meta.index_together选项

class QuoteModel(models.Model):
    # ... fields ...

    class Meta:
        index_together = [
            ("sequence", "stock"),
        ]

(note: the original answer from 2009 said it was not possible to index multiple fields; it has since been replaced)

(注意:2009 年的原始答案说不可能索引多个字段;它已被替换)

回答by Ivan Virabyan

There is a ticket for this feature. Take a look http://code.djangoproject.com/ticket/5805

此功能有一张票。看看http://code.djangoproject.com/ticket/5805

You may apply the patch from this ticket yourself.

您可以自己应用此票证中的补丁。

UPDATE

更新

It's now in Django: https://docs.djangoproject.com/en/1.5/ref/models/options/#django.db.models.Options.index_together

它现在在 Django:https: //docs.djangoproject.com/en/1.5/ref/models/options/#django.db.models.Options.index_together

回答by George Lund

To follow on from the accepted answer, if you are using South, you can easily add a composite key as follows:

从接受的答案继续,如果您使用South,您可以轻松添加复合键,如下所示:

manage.py schemamigration your_app_name name_for_migration --add-index ModelName.first_field_in_index

manage.py schemamigration your_app_name name_for_migration --add-index ModelName.first_field_in_index

You can then edit the generated migration file to add the additional fields into the one index (you'll see it's just a list of field names that's needed).

然后,您可以编辑生成的迁移文件以将其他字段添加到一个索引中(您将看到它只是所需的字段名称列表)。

Don't forget to update the reverse migration as well as the forward one.

不要忘记更新反向迁移和正向迁移。

回答by cashmere

It is index_together in django 1.5

它是 django 1.5 中的 index_together

See here https://docs.djangoproject.com/en/dev/ref/models/options/#index-together

请参阅此处https://docs.djangoproject.com/en/dev/ref/models/options/#index-together

回答by craesh

unique_togethermight be what you are looking for. Just put it in your Metaclass inside your model.

unique_together可能是你正在寻找的。只需将它Meta放在模型中的类中即可。

回答by Lingster

I just wanted to add that as of Django 1.11 there is a new feature, Options.indexes, which will allow you to specify the indexes to create:

我只想补充一点,从 Django 1.11 开始,有一个新功能 Options.indexes,它允许您指定要创建的索引:

Django Options.indexes

Django Options.indexes

回答by Samuel Dauzon

The index_togetherfeature could be deprecated in the futur.

index_together功能可以在未来的危机被弃用。

You should use indexesoption instead of index_together.

您应该使用索引选项而不是index_together

Example of indexesoption :

索引选项示例:

from django.db import models

class Customer(models.Model):
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)

    class Meta:
        indexes = [
            models.Index(fields=['last_name', 'first_name']),
            models.Index(fields=['first_name'], name='first_name_idx'),
        ]