Stringsql="insrtintotstdb.tusr(nam,rmark,crattim,updattim)valus(?,?,?,?)";for(inti=0;im;i++){//从池中获取连接Connctionconn=myBrokr.gtConnction();PrpardStatmntpstmt=conn.prparStatmnt(sql);for(intk=0;kn;k++){pstmt.stString(1,RandomToolkit.gnratString(12));pstmt.stString(2,RandomToolkit.gnratString(24));pstmt.stDat(,nwDat(Systm.currntTimMillis()));pstmt.stDat(4,nwDat(Systm.currntTimMillis()));//加入批处理pstmt.addBatch();}pstmt.xcutBatch();//执行批处理pstmt.clos();myBrokr.frConnction(conn);//连接归池}多值插入SQL普通插入SQL:INSERTINTOTBL_TEST(id)VALUES(1)多值插入SQL:INSERTINTOTBL_TEST(id)VALUES(1),(2),()使用多值插入SQL,SQL语句的总长度减少,即减少了网络IO,同时也降低了连接次数,数据库一次SQL解析,能够插入多条数据。事务(N条提交一次)在一个事务中提交大量INSERT语句可以提高性能。1、将表的存储引擎修改为myisam2、将sql拼接成字符串,每条左右提交事务。
///summary///执行多条SQL语句,实现数据库事务。////summarymysql数据库///paramnam="SQLStringList"多条SQL语句/parampublicvoidExcutSqlTran(ListstringSQLStringList){using(MySqlConnctionconn=nwMySqlConnction(connctionString)){if(DBVariabl.flag){conn.Opn();MySqlCommandcmd=nwMySqlCommand();cmd.Connction=conn;MySqlTransactiontx=conn.BginTransaction();cmd.Transaction=tx;try{for(intn=0;nSQLStringList.Count;n++){stringstrsql=SQLStringList[n].ToString();if(strsql.Trim().Lngth1){cmd.CommandTxt=strsql;cmd.ExcutNonQury();}//后来加上的if(n0(n%==0
n==SQLStringList.Count-1)){tx.Commit();tx=conn.BginTransaction();}}//tx.Commit();//原来一次性提交}catch(Systm.Data.SqlClint.SqlExcptionE){tx.Rollback();thrownwExcption(E.Mssag);}}}}10w条数据大概用时10s!参考资料:原文: