The R language blocks every execution of a statement when it queries a database. The next statement is not made until the result is returned by the query statement.
In order to be able to query multiple databases simultaneously, in order to save time in sequential execution, first consider the database query through multithreading.
However, multiple databases at the same time, many results merged too large, the local machine's memory may be a risk point, in the heart to be able to need to grasp the memory.
R is a multi-threaded dependency with the Doparallel package and the foreach package.
> install.packages ('doparallel')> Install.packages (' foreach')> Library (doparalled)>Library (rmysql)
# build 2 clusters, how many clusters combined with local machine hardware configuration and what you need
> Cl <-makecluster (2
# Register multi-threading, personal understanding, parallel package should be declared on the backend to turn on multicore processing mode, let hardware prepare environment, allocate resources
> Registerdoparallel (CL)
# % dopar% is the syntax format for the foreach package, which indicates multithreaded operation.
# foreach Returns a list by default, or you can specify a function that rbind,sum the result directly after the thread has finished processing. More view? foreach
> Qdata <-foreach (i=1:2)%dopar% { if (i = = 1) conn <- connectdb_1 () Else Conn <- connectdb_2 ()
<- dbgetquery (conn, query) Dbdisconnect (conn)
return (re)} # turn off cluster > Stopcluster (CL)
[R language]foreach and Doparallel package implements multiple database queries simultaneously