简体   繁体   中英

Improve large query insert performance using MySQL

I write c++ to do 30000*30000 query MySQL insert

Example

for(i=0;i<30000*30000;i++){
// do the MySQL insert
call the function to insert code (maybe just query insert the i value)
}

and I am using mysqlsart() and mysqlclose() sub-function

void mysqlstart()
{
        //MYSQL *conn_ptr;
        conn_ptr = mysql_init(NULL);

        if(!conn_ptr)
        {
            fprintf(stderr,"mysql_init failed\n");
            //return EXIT_FAILURE;
        }

        conn_ptr = mysql_real_connect(conn_ptr,"localhost","root","nlpgroup","testdb",0,NULL,0);
}

void mysqlclose()
{
        mysql_close(conn_ptr);
}

First time I call the function like

mysqlstart();
for loop 
mysqlclose();

That will be great in the beginning and after few days I found the error : MySQL server has gone away

And find some solution from internet maybe change max_allowed_packet or some else...

And I don't know what is suitable settings for 30000*30000

And I want to is there something I can change in my code or way to speed up query

You can insert multiple rows at once in this way

http://dev.mysql.com/doc/refman/5.5/en/insert.html

INSERT statements that use VALUES syntax can insert multiple rows. To do this, include multiple lists of column values, each enclosed within parentheses and separated by commas. Example:

INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM