美文网首页
Django 索引

Django 索引

作者: Sunnky | 来源:发表于2017-12-05 17:58 被阅读0次

    索引:

    索引可以极大的提高数据的查询速度,但是会降低插入、删除、更新表的速度,因为在执行这些写操作时,还要操作索引文件

    Django中建立索引

    class PressureSensor(models.Model):
        store = models.ForeignKey(Store, verbose_name='门店ID', on_delete=models.CASCADE)
        barcode = models.CharField(verbose_name='货架码', max_length=12, db_index=True)
    

    当然,除了这一种方式外,还有以下两种,分别是:

    • unique_together 联合主键,包含 index_together
    • index_together 组合索引
        class Meta:
            unique_together = [["store", "barcode"], ["store", "mac_addr_no"]]
    
        class Meta:
            index_together = ["store", "sensor"]
    

    Django索引原则:

    • 主键必定是索引
    • Django默认会为每个Foreginkey创建一个索引

    相关文章

      网友评论

          本文标题:Django 索引

          本文链接:https://www.haomeiwen.com/subject/oswcixtx.html