PDA

View Full Version : Fast insert record into MySql



weixj2003ld
15th July 2010, 07:57
I have many records,If I use "Insert(...)" in for(int i=0;i<n;i++),I find that it is very slow,how to insert many records into Mysql fast?

saa7_go
15th July 2010, 08:05
You can use transaction.



dbConnection.transaction();

for(int i=0;i<n;i++)
{
// insert record here
}

dbConnection.commit();

Lykurg
15th July 2010, 08:43
transactions are the best way, but if your DBS doesn't support them, then you (maybe) can gain speed by joining all inserts and only execute one INSERT statement.

saa7_go
15th July 2010, 09:08
transactions are the best way, but if your DBS doesn't support them, then you (maybe) can gain speed by joining all inserts and only execute one INSERT statement.

Yes, you are right. I suggest weixj2003ld to use transaction because he/she uses mysql database. But, thanks for your information.