What I did is on the website How to select efficiently in a very large table
I used RPostgreSQL package under R because I want to manipulate other files at the same time. As you can see from the above website, the table is very large. It will cost more than two hours to complete the selection using RPostgreSQL. But today I use the same SQL code under psql instead of using RpostgreSQL. It only took several minutes. Why?
The code of R is:
sql='SELECT * into new_table FROM table_1 WHERE EXISTS (SELECT 1 FROM table_2 WHERE column=table_1.column_1) AND EXISTS (SELECT 1 FROM table_2 WHERE column=table_1.column_2)'
rs=dbSendQuery(con,sql)
Updated: I used the psycopg2 under python. It also runs pretty fast.