mysql flush

编程入门 行业动态 更新时间:2024-10-08 02:29:01

<a href=https://www.elefans.com/category/jswz/34/1771279.html style=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

本文发布于:2024-03-04 11:36:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1709168.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:mysql   flush

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!