Skip to content
Advertisement

How to replace IN in an SQL query containing a lot of parameters with Postgresql?

I am trying to retrieve information from a database using a Python tuple containing a set of ids (between 1000 and 10000 ids), but my query uses the IN statement and is subsequently very slow.

and then I query the database using PostgreSQL to charge the result in a Pandas dataframe:

I know that IN is not very efficient with a high number of parameters, but I do not have any idea to optimise this part of the query. Any hint?

Advertisement

Answer

You could debug your query using explain statement. Probably you are trying to sequently read big table while needing only a few rows. Is field table1.id indexed?

Or you could try to filter table1 first and then start joining

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement