Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using join of temporary table as an alternative of `IN` in Django

In Django it's common to do the following

MyModel.objects.filter(id__in=[huge array])

However it's not very efficient as described in the following answer here: https://dba.stackexchange.com/questions/91247/optimizing-a-postgres-query-with-a-large-in

What would be a good way of replicating the above answer in Django given that you are using the ORM. Or would you have to drop down to raw sql for the entire query.

What I'm looking for is if you have a queryset, is there a good way of joining that queryset with a temporary table that you created (possibly in raw sql).

like image 392
Jonathan Avatar asked Oct 24 '18 17:10

Jonathan


People also ask

How do I join a query in Django?

10. Join Queries. Join can be done with select_related method: Django defines this function as Returns a QuerySet that will “follow” foreign-key relationships, selecting additional related-object data when it executes its query.

What is the difference between view and temporary table?

A view exists only for a single query. Each time you use the name of a view, its table is recreated from existing data. A temporary table exists for the entire database session in which it was created. A view is automatically populated with the data retrieved by the query that defines it.

Can you use a temporary table in a view?

No, a view consists of a single SELECT statement. You cannot create or drop tables in a view. Maybe a common table expression (CTE) can solve your problem. CTEs are temporary result sets that are defined within the execution scope of a single statement and they can be used in views.

How are temporary tables declared?

You can populate the declared temporary table by using INSERT statements, modify the table by using searched or positioned UPDATE or DELETE statements, and query the table by using SELECT statements. You can also create indexes on the declared temporary table.


1 Answers

You can use the select_related option, to filter your second table(another model) that will do the in effect.

Something like this answer: Django select_related filter

I use that for large INs when its possible.

like image 111
Walucas Avatar answered Oct 16 '22 02:10

Walucas