django - 如何在 django (1.8) 迁移中删除索引 varchar_pattern_ops?

标签 django postgresql migration django-migrations database-indexes

使用 models.varchar(...) 字段创建模型时,会创建一个 varchar_pattern_ops 索引。

这是在postgresql中生成的表

              Table "public.logger_btilog"
      Column      |           Type           | Modifiers 
------------------+--------------------------+-----------
 md5hash          | text                     | 
 id               | integer                  | not null
Indexes:
    "logger_btilog_pkey" PRIMARY KEY, btree (id)
    "logger_btilog_md5hash_6454d7bb20588b61_like" btree (md5hash varchar_pattern_ops)

我想删除迁移中的 varchar_pattern_ops 索引,并在该字段中添加哈希索引。

我试过这样做:

# models.py
class Btilog(models.Model):
    md5hash = models.TextField(db_index=False)
    [...]

并且在迁移中还强制添加db_field=False

# 0013_migration.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

    dependencies = [
        ('logger', '0014_btilog_id'),
    ]

    operations = [
        # this should remove all indexes for md5hash, but it does not work
        migrations.AlterField(
            model_name='btilog',
            name='md5hash',
            field=models.TextField(null=True, blank=True, db_index=False),
        ),
        migrations.RunSQL(
            "create index logger_btilog_md5hash_hashindex on logger_btilog using hash(md5hash);",
            "drop index logger_btilog_md5hash_hashindex;"
        ),
]

运行迁移后,这是数据库中的索引

                              relation                              |  size   
--------------------------------------------------------------------+---------
 public.logger_btilog                                               | 7185 MB
 public.logger_btilog_md5hash_6454d7bb20588b61_like                 | 1442 MB
 public.logger_btilog_md5hash_hashindex                             | 1024 MB
 public.logger_btilog_pkey                                          | 548 MB

请注意,public.logger_btilog_md5hash_6454d7bb20588b61_like 是我要删除的索引。此索引由 django 自动添加,请参阅 this

关于该指数的更多信息

vtfx=# \d logger_btilog_md5hash_6454d7bb20588b61_like
Index "public.logger_btilog_md5hash_6454d7bb20588b61_like"
 Column  | Type | Definition 
---------+------+------------
 md5hash | text | md5hash
btree, for table "public.logger_btilog"

脚注:我对哈希索引的用法并不感到困惑,我只想做 = (严格等于) wheremd5hash< 中搜索 字段,然后(随便)一个 hash 索引将是最快的,并且比 btree 索引(django 的默认)占用更少的空间

最佳答案

Answer update notice:

  • django < 1.11: Use this answer

  • django >= 1.11: Use @Cesar Canassa's answer

好的,我在这里找到了一些信息 https://docs.djangoproject.com/en/1.8/_modules/django/db/backends/base/schema/#BaseDatabaseSchemaEditor.alter_field

并使用 SchemaEditor 进行手动 RunPython 迁移以删除 varchar_pattern_ops 索引

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


import re
def drop_md5hash_varchar_pattern_ops_index(apps, schemaEditor):
    # code based on https://docs.djangoproject.com/en/1.8/_modules/django/db/backends/base/schema/#BaseDatabaseSchemaEditor.alter_field
    model = apps.get_model("logger", "Btilog")
    index_names = schemaEditor._constraint_names(model, index=True)
    for index_name in index_names:
        if re.search('logger_btilog_md5hash_.+_like', index_name):
            print 'dropping index {}'.format(index_name)
            schemaEditor.execute(schemaEditor._delete_constraint_sql(schemaEditor.sql_delete_index, model, index_name))


class Migration(migrations.Migration):
    dependencies = [
        ('logger', '0012_auto_20150529_1745'),
    ]

    operations = [
        # Remove the annoying index using a hack
        migrations.RunPython(
            drop_md5hash_varchar_pattern_ops_index
        ),
    ]

关于django - 如何在 django (1.8) 迁移中删除索引 varchar_pattern_ops?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32773049/

相关文章:

python - 从 WSGI 或 shell 调用时,Django 反向返回不同的值

postgresql - PostgreSql 中的 abstime 和 reltime 有什么区别

c++ - 无法在 VS2015 下启动 64 位应用程序

sql - 使用从列表中随机选择的值更新每一行

MySQL 根据另一个表的结果批量更新新表

SSL 证书从一台服务器迁移到另一台服务器

django - 如何在 Django 中创建一个 numpy 数组字段?

django - 为什么 Django (1.5.1) 会调用 Loader.load_template_source() 69 次?

python - 具有多种表示/格式的 Django REST API

sql - 如何使用 select 查询的输出作为 insert 查询的输入?