mysql flush"/>
mysql flush
DROP TABLE IF EXISTS `supan`;
CREATE TABLE `supan` (
`name` varchar(30) DEFAULT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
`grade` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
java程序:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class InsertTestData
{
public static void main(String[] args)
{
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3306/test";
String user = "root";
String password = "root";
try
{
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
//故意设置自动提交为false
conn.setAutoCommit(false);
PreparedStatement pst = (PreparedStatement) conn.prepareStatement("insert into supan(name,grade) values(?,?)");
//记录开始时间
Long startTime = System.currentTimeMillis();
for (long i = 0; i < 1000000; i++) {
pst.setString(1, "张三");
pst.setLong(2,i);;
pst.addBatch();
pst.executeBatch();
//故意每次都提交一个sql语句,增加事务的开启和关闭
connmit();
pst.clearBatch();
}
// 语句执行完毕,提交本事务
Long endTime = System.currentTimeMillis();
System.out.println(endTime - startTime);
conn.close();
} catch (Exception e)
{
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
}
}
}
上面测试代码插入一百万数据:在配置为1的情况下执行时间为:1337496
在配置为2的情况下执行时间为:434137
效率提升整200%
分享到:
2015-01-17 16:30
浏览 791
分类:数据库
评论
更多推荐
mysql flush
发布评论