4đź‘Ť
Some pointers about optimisation for the Django ORM with Postgres:
- Use
db_index=True
on fields that will be search upon often and have some degree of repetition between entries, like “title”. - Use
values()
andvalues_list()
to select only the columns you want from a QuerySet. - If you’re doing full text search in any of those columns (like a
contains
query), bear in mind that Django has support for full text search directly on a Postgres database. - Use
print queryset.query
to check what kind of SQL query is going into your database and if it can be improved upon. - Many Postgres optimisation techniques rely in custom SQL queries that can be made in Django by using
RawSQL
expressions. - Remember that there are many, many ways to search for data in a database, be it relational or not-relational in nature. In your case, MongoDB is not “faster” than Postgres, it’s just doing a better job at querying what you really want.
👤fixmycode
Source:stackexchange.com