Difference in ordering a model

I have a question regarding how to organize a model. What is faster and more optimized, perform a search for the form the 1º method or the 2º?

1º method:

    datos_independiente=item.objects.order_by('-qualification')

or 2º method: use in the model

 class Meta:
        ordering = ["-qualification"]

I ask this because if I use the first method, the database will be ordered every time I execute the sequence and if I use the second method, it will be ordered when it is created and the new rows that are added will do so in an ordered way? If this is correct, it will be more process optimal to use Meta class. Right?

No, there’s actually no difference. The second method does not “add the data in an ordered way”. In the relational model, rows are unordered. If you want to retrieve the data in a specific sequence, you need to specify that in the query.

The only difference between the two methods is that the second method makes the application of the order by clause the default.

You can create indexes such that the database maintains a sequenced list of the identified column(s) to improve performance of a query at the expense of slightly more time spent during inserts and updates.

1 Like