如何在 django (1.8) 迁移中删除索引 varchar_pattern_ops?
How to remove index varchar_pattern_ops in a django (1.8) migration?
使用 models.varchar(...)
字段创建模型时,正在创建 varchar_pattern_ops
索引。
这是在postgresql
中生成的table
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"
脚注:我对哈希索引的用法并不感到困惑,我只想在 md5hash
字段中进行 =
(严格等于)where
搜索,然后(随便)一个 hash
索引将是最快的,并且比 btree
索引(django 的默认值)
占用更少 space
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 1.11 中添加的新 Model Meta indexes 选项完全避免创建 varchar_pattern_ops
"LIKE" 索引。例如,不要像这样编写模型:
class MyModel(models.Model):
my_field = models.CharField(max_length=64, db_index=True)
您需要使用 Model Meta 选项设置索引:
class MyModel(models.Model):
my_field = models.CharField(max_length=64)
class Meta:
indexes = [
models.Index(fields=['my_field'])
]
这样做,Django 将不会创建重复索引。
我正在使用 Django 3.0.11 我遇到了同样的问题。我不想要主键的 _like 索引,因为它仅用于连接和精确匹配。
如果字段是 primary_key,手动定义索引将无济于事。所以我想出了类似于第一个答案的 hacky 解决方案。
from django.db import migrations, models
def drop_pattern_ops_index(schema_editor, model, field):
indexes = schema_editor._model_indexes_sql(model)
for index in indexes:
columns = index.parts["columns"].columns
opclasses = index.parts["columns"].opclasses
if field in columns and len(columns) == 1:
# Django uses varchar_pattern_ops or text_pattern_ops
if "pattern_ops" in opclasses[0]:
drop_sql = schema_editor._delete_index_sql(
model, str(index.parts["name"]).strip('"')
)
# Indexes creation is always deferred in migration
# so we have to execute remove at the end
schema_editor.deferred_sql.extend([drop_sql])
break
def drop_extra_indexes(apps, schema_editor):
model = apps.get_model("com_erp", "TestModel")
drop_pattern_ops_index(schema_editor, model, "code")
class Migration(migrations.Migration):
operations = [
migrations.CreateModel(
name="TestModel",
fields=[
("code", models.TextField(primary_key=True, serialize=False)),
("name", models.TextField()),
],
options={"db_table": "test_table",},
),
migrations.RunPython(drop_extra_indexes, migrations.RunPython.noop),
]
使用 models.varchar(...)
字段创建模型时,正在创建 varchar_pattern_ops
索引。
这是在postgresql
中生成的table 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"
脚注:我对哈希索引的用法并不感到困惑,我只想在 md5hash
字段中进行 =
(严格等于)where
搜索,然后(随便)一个 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 1.11 中添加的新 Model Meta indexes 选项完全避免创建 varchar_pattern_ops
"LIKE" 索引。例如,不要像这样编写模型:
class MyModel(models.Model):
my_field = models.CharField(max_length=64, db_index=True)
您需要使用 Model Meta 选项设置索引:
class MyModel(models.Model):
my_field = models.CharField(max_length=64)
class Meta:
indexes = [
models.Index(fields=['my_field'])
]
这样做,Django 将不会创建重复索引。
我正在使用 Django 3.0.11 我遇到了同样的问题。我不想要主键的 _like 索引,因为它仅用于连接和精确匹配。
如果字段是 primary_key,手动定义索引将无济于事。所以我想出了类似于第一个答案的 hacky 解决方案。
from django.db import migrations, models
def drop_pattern_ops_index(schema_editor, model, field):
indexes = schema_editor._model_indexes_sql(model)
for index in indexes:
columns = index.parts["columns"].columns
opclasses = index.parts["columns"].opclasses
if field in columns and len(columns) == 1:
# Django uses varchar_pattern_ops or text_pattern_ops
if "pattern_ops" in opclasses[0]:
drop_sql = schema_editor._delete_index_sql(
model, str(index.parts["name"]).strip('"')
)
# Indexes creation is always deferred in migration
# so we have to execute remove at the end
schema_editor.deferred_sql.extend([drop_sql])
break
def drop_extra_indexes(apps, schema_editor):
model = apps.get_model("com_erp", "TestModel")
drop_pattern_ops_index(schema_editor, model, "code")
class Migration(migrations.Migration):
operations = [
migrations.CreateModel(
name="TestModel",
fields=[
("code", models.TextField(primary_key=True, serialize=False)),
("name", models.TextField()),
],
options={"db_table": "test_table",},
),
migrations.RunPython(drop_extra_indexes, migrations.RunPython.noop),
]