[R language]foreach and Doparallel package implements multiple database queries simultaneously

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.