I have a dataframe in R which contains the output of previous queries. Unfortunately, I cannot do this directly in SQL since it is too slow so I am using the data.table package. The output from the data.table package is a data frame of 50,000 ids. I need to pull all records from the database for each id.
# x is a dataframe containing 50,000 ids.
Usually, I would do something like,
dbGetQuery(con, "Select * from data where id in x")
but that won't work. An alternative is to do 50,000 queries in a for loop, but I am thinking that there must be a more efficient method to do this.
What is the most efficient way to do this?
INclause are, but suspect (??) that it's pretty large. Are you sure you can't just put all or most of them in oneINclause? (Another option of course is to push the ids to a temporary table in the db and do a join.)but that wont work?INclause queries like this from R, you'll need to explicitly build the query via string concatenation usingpaste()orsprintf().