﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
36475	Optimize F-expression slice to MySQL column-prefix index	JaeHyuckSa		"Since Django 5.1 you can do this in your model

{{{
class MyModel(models.Model):
    field = models.CharField(max_length=200)

    class Meta:
        indexes = [
            Index(F('field')[:10], name='prefix_idx'),
        ]
}}}


However, on all backends this still creates:


{{{
CREATE INDEX prefix_idx ON myapp_mymodel (SUBSTRING(field, 1, 10));
}}}
MySQL supports a more efficient “column prefix” syntax:


{{{
CREATE INDEX prefix_idx ON myapp_mymodel (field(10));
}}}
This syntax is more efficient and directly supports LIKE and startswith queries on long VARCHAR or TEXT columns.
MySQL docs: https://dev.mysql.com/doc/refman/9.3/en/create-index.html#create-index-column-prefixes

This has come up before — see Ticket #35777 — and was also discussed in django-mysql # https://github.com/adamchainz/django-mysql/pull/1151#issuecomment-2995608422 where Adam Johnson suggested that Django could handle this directly instead of needing custom index subclasses.



"	Cleanup/optimization	new	Database layer (models, ORM)	5.1	Normal				Unreviewed	0	0	0	0	0	0
