Tagged with: Action action track names free data tran span star
go to Mysql and insert 10,000 simple data. It's very slow, it's going to take about 5 minutes,
But open the business. We're done in less than a second.
Code:
#include <iostream> #include <winsock2.h> #include <string> #include "mysql.h" #pragma comment (lib, " Libmysql.lib "); using namespace Std;int main () {MySQL mysql;mysql_init (&mysql);//initialize MySQL *connstatus = Mysql_real_ Connect (&mysql, "localhost", "root", "" "," Sky ", 3306,0,0), if (connstatus = = NULL) {//connection failed int i = Mysql_errno (& MySQL); string strerror= mysql_error (&mysql); cout << "error info:" <<strerror<<endl;return 0;} cout<< "Mysql Connected ..." <<endl;string strSQL; Mysql_res *result=null;//Data Result set//insert operation strSQL = "INSERT INTO T1 values (2, ' lyb ')"; mysql_query (&mysql, "START TRANSACTION "); Turn on the transaction, assuming the transaction is not turned on, then the efficiency becomes very low!for (int i=0; i<10000; i++) {mysql_query (&mysql,strsql.c_str ());} mysql_query (&mysql, "COMMIT"); Commit TRANSACTION cout<< "Insert End" <<endl;//release result set close database Mysql_free_result (result); Mysql_close (&mysql); mysql_ Library_end (); return 0;}
Summary:
Transactions should be turned on when large amounts of data are inserted, changed, and so on. After a series of operations are completed, the transaction is committed. Can improve the efficiency of program operation
Solve C + + operation MySQL Large data insertion inefficiency problem