Great advices, which I wish I had known some 5 years ago when i started developing a django app.
Recently, I went through the process of having to optimise some of the queries and its a total nightmare having to figure it all afterwards. So, if you want to keep your sanity while optimising the queries better do it from the very beginning! :)
What I found most useful:
Using the django debug toolbar to easily track queries execution
Settings up the db logs (as explained above)
Creating custom managers for the models I wanted to query
Trying to write queries returning .values() as much as possible
Using .only() as much as possible, to limit the data returned from the database
Fetching bulk data one time, and sorting/filtering it accordingly in python
p.s. For some reason the two first block codes appear duplicated.
Great advices, which I wish I had known some 5 years ago when i started developing a django app.
Recently, I went through the process of having to optimise some of the queries and its a total nightmare having to figure it all afterwards. So, if you want to keep your sanity while optimising the queries better do it from the very beginning! :)
What I found most useful:
.values()
as much as possible.only()
as much as possible, to limit the data returned from the databasep.s. For some reason the two first block codes appear duplicated.
Happened to me also :), thanks for your comment!! and great advices too!
I'll check the duplicated code :)