How to use Django index_together to query with both filter and order_by?

I have a string index problem for my query.
I have a model like this:

from django.db import models  

class Record(models.Model):
    user = models.ForeignKey(User, db_index=True, related_name='records')
    action = models.ForeignKey(Action, db_index=True)
    time = models.DateTimeField(db_index=True, default=timezone.now)

    class Meta:
        index_together = (
            ('user', 'time'),
            ('action', 'user', 'time'),
        )

As you can see, there are two custom indexes for this model.

If I want to get all records related to a particular user, filtered time, I use this query: user.records.filter(time__gt=some_moment). It works fine and uses the first user index (according to

+4
source share
3 answers

, , , . 500 , .

, , appname_record_user_id_3214bab8a46891cc_idx , . ? , , , .

, , . . :

class Meta:
    index_together = (
        ('user', 'time','action'),
    )

. :

MySQL , , , , , . , .

+5

, , . - , , 2-, 3-. , . , mysql.

+4

Source: https://habr.com/ru/post/1612049/


All Articles