#35777: MySQL: cannot add key limited index on TEXT columns
-------------------------------+--------------------------------------
     Reporter:  Tobias Krönke  |                    Owner:  (none)
         Type:  Uncategorized  |                   Status:  new
    Component:  Migrations     |                  Version:  5.0
     Severity:  Normal         |               Resolution:
     Keywords:                 |             Triage Stage:  Unreviewed
    Has patch:  0              |      Needs documentation:  0
  Needs tests:  0              |  Patch needs improvement:  0
Easy pickings:  0              |                    UI/UX:  0
-------------------------------+--------------------------------------
Comment (by Tobias Krönke):

 The `url__startswith='https://google.com%'` query was just an example. I
 would like to optimize all `startswith` queries on that field. I've
 created this crude workaround and implemented my own `Index`:


 {{{
 class ColumnPrefixIndex:
     max_name_length = 60
     contains_expressions = False
     fields_orders = []
     include = []
     expressions = []
     fields = []

     def __init__(self, column, prefix, name):
         self.prefix = prefix
         self.column = column
         self.name = name

     def create_sql(self, model, schema_editor, using="", **kwargs):
         table = model._meta.db_table
         sql = f'CREATE INDEX {self.name} ON {table}
 ({self.column}({self.prefix}));'
         kwargs['sql'] = sql

         return schema_editor._create_index_sql(
             model,
             name=self.name,
             using=using,
             **kwargs,
         )

     def deconstruct(self):
         kwargs = {'column': self.column, 'prefix': self.prefix, 'name':
 self.name}
         path = "%s.%s" % (self.__class__.__module__,
 self.__class__.__name__)
         return path, self.expressions, kwargs

     def clone(self):
         return self.__class__(self.column, self.prefix, self.name)
 }}}
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35777#comment:5>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107019210515d2d-51efa7b5-574d-4727-9d50-a2d6b0bc1882-000000%40eu-central-1.amazonses.com.

Reply via email to