title: how to accelerate c++ for loop code into query using MySQL
body: <p>code of c++ sub function</p>
  <h1>insert data into MySQL</h1>
  <pre><code>bool mysqlinsert2(int sourceindex,int targetindex,string source,string target)
      {
          MYSQL *conn_ptr;
          conn_ptr = mysql_init(NULL);
  
          if(!conn_ptr)
          {
              fprintf(stderr,&quot;mysql_init failed\n&quot;);
              //return EXIT_FAILURE;
          }
  
          conn_ptr = mysql_real_connect(conn_ptr,&quot;localhost&quot;,&quot;root&quot;,&quot;nlpgroup&quot;,&quot;testdb&quot;,0,NULL,0);
  
          if(conn_ptr)
          {
              int abc;
              char sql_buffer[500] = {0};
              sprintf(sql_buffer,&quot;insert into EM2 (sourceindex,targetindex,source,target) values(\&quot;%d\&quot;,\&quot;%d\&quot;,\&quot;%s\&quot;,\&quot;%s\&quot;)&quot;,sourceindex,targetindex,source.c_str(),target.c_str());
              if((abc = mysql_query(conn_ptr,sql_buffer)))
              {
                  printf(&quot;error code:%s\n&quot;,mysql_error(conn_ptr));
              }
              mysql_close(conn_ptr);
              return true;
              //connection succees
          }
          else
          {
              printf(&quot;Error code: %d  %s %s\n&quot;,mysql_errno(conn_ptr),mysql_sqlstate(conn_ptr),mysql_error(conn_ptr));
              mysql_close(conn_ptr);
              return false;
              //return EXIT_SUCCESS;
          }
      }
  </code></pre>
  <hr />
  <h1>code for run loop and input the for loop index and value into MySQL by calling the function mysqlinsert2</h1>
  <pre><code>void write_token_to_data()
      {
          typedef double* DynamicMatrix[l+m];
          // DynamicMatrix Count;
          typedef double* DynamicMatrix2[l+m];
          //DynamicMatrix2 Prob;
          for(int i=0; i&lt;(l+m); i++)
          {
              for(int j=0; j&lt;(l+m); j++)
              {
                  if(mysqlinsert2(i,j,combine[i],combine[j]))
                  {
                      cout&lt;&lt;&quot;insert OK!!&quot;&lt;&lt;endl;
                  }
                  else
                  {
                      cout&lt;&lt;&quot;insert failed&quot;&lt;&lt;endl;
                  }
  
  
              }
          }
          
      }//end of function
  </code></pre>
  <hr />
  <p>when MySQL insert ok the program will return the &quot;insert ok!!&quot;
  so , here 's my question</p>
  <p>these query could be until 30000*30000 times
  and i want to know how to accelerate this, maybe i could optimize the MySQL setting or change the code in c++ , and i want it have same result</p>