My team’s running large aggregations queries in our Postgres database, the result of which don’t need to be retrieved immediately.
Since we want to avoid creating model instances we don’t need for upwards of 500k result rows and, instead, retrieve the results later, we’ve decided to use the SELECT INTO feature of postgres which stores the result of the query in a temporary table and does not return anything to the Django app.
So far we’ve only managed to manually write these queries using the .raw()
method of a queryset as we couldn’t find a way to implement SELECT INTO with the QuerySet API. I’d like to us to use a queryset instead to reduce security risks and improve maintainability.
Is there a way to implement a SELECT INTO query using the queryset api that we’ve missed? Has anyone attempted something similar before?
Thanks!