Django Model Fields Indexing

后端 未结 2 1193
孤街浪徒
孤街浪徒 2021-01-31 15:10

I only know that indexing is helpful and it queries faster.

What is the difference between following two?

1. class Meta:
       indexes = [
           mo         


        
2条回答
  •  耶瑟儿~
    2021-01-31 15:27

    Django Model Index was introduced in Django 1.11

    what is Model.indexes:

    By default, indexes are created with an ascending order for each column. To define an index with a descending order for a column, add a hyphen before the field’s name.

    For your query, models.Index(fields=['last_name', 'first_name','-date_of_birth',]), would create SQL with (last_name, first_name, date_of_birth DESC).

    Lets move to your question,

    you asked difference between 2 queries,

    both will take models.Index(fields=['-date_of_birth',]),

    because least one will override the assigned variables. from your question least is dateofbirth so it will override above two lines.

    so as per documentation preferable method is, because indexing field should be in single list.. so django will prepare SQL indexing from list of fields...

    models.Index(fields=['last_name', 'first_name', '-date_of_birth']),
    

提交回复
热议问题