class Subject(models.Model):
name = models.CharField(max_length=255)
class Meta:
indexes = [
models.Index(fields=['name', ], name='%(app_label)s_%(class)s_n_like_idx',
opclasses=('varchar_pattern_ops',)),
models.Index(fields=['name', ], name='%(app_label)s_%(class)s_n_idx')
]
Iexact operator doesn't use these indexes. Only with exact operator. Also tried to use text_pattern_ops no affects :(
I've decided to add explicitly into migrations to fix this.
operations = [
migrations.RunSQL( sql=r'CREATE INDEX "upper_name_idx" ON "subject" (UPPER("name"));',
reverse_sql=r'DROP INDEX "stats_siteuser_upper_idx";' ),
]
I' ve attached 2 screenshots with queries before add and after upper(name) index.
As you see performance boost too much to ignore it.
My propose is to add possibility to simply add upper index support to allow Django do by own
For example:
class Subject(models.Model):
name = models.CharField(max_length=255)
class Meta:
indexes = [
models.Index(fields=['name', ], name='%(app_label)s_%(class)s_n_upper_idx',
opclasses=('varchar_pattern_ops',), to_upper=['name']),
]
Thanks, Pavel