Java MySQL中的写入时间
我正在使用Java代码为6000个条目随机创建PIN。目前,写入数据库需要3分钟27秒。我想知道您是否可以减少对数据库的写入时间 数据库有一个自动递增字段和一个pin字段,pin字段是字符串Java MySQL中的写入时间,java,mysql,sql,Java,Mysql,Sql,我正在使用Java代码为6000个条目随机创建PIN。目前,写入数据库需要3分钟27秒。我想知道您是否可以减少对数据库的写入时间 数据库有一个自动递增字段和一个pin字段,pin字段是字符串 public class DonkeyInsert { /** * @param args the command line arguments * @throws java.sql.SQLException */ public static void mai
public class DonkeyInsert {
/**
* @param args the command line arguments
* @throws java.sql.SQLException
*/
public static void main(String[] args) throws SQLException {
// TODO code application logic here
Connection conn = new DBConnection().connect();
for (int i = 1; i <= 6000; i++) {
Random rand = new Random();
// create a Statement from the connection
Statement statement = conn.createStatement();
// insert the data
int k = rand.nextInt(Integer.SIZE - 1);
k = (k + 1) * 9999;
String pin = Integer.toString(k);
try {
String sql = "INSERT INTO login (login_id,pin) VALUES (" + i + "," + pin + ");";
statement.executeUpdate(sql);
} catch (SQLException se) {
System.out.println(se);
}
}
}
}
公共类DonkeyInsert{
/**
*@param指定命令行参数
*@throws java.sql.SQLException
*/
公共静态void main(字符串[]args)引发SQLException{
//此处的TODO代码应用程序逻辑
Connection conn=new DBConnection().connect();
对于(int i=1;i试试这个,看看它是否更快。我所做的更改应该会有帮助:
package persistence;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Random;
/**
* DonkeyInsert changes
* @author Michael
* @link https://stackoverflow.com/questions/32551895/write-time-in-mysql?noredirect=1#comment52959887_32551895
* @since 9/13/2015 12:49 PM
*/
public class DonkeyInsert {
public static final int DEFAULT__RECORD_COUNT = 6000;
private Random random;
public DonkeyInsert() {
this.random = new Random();
}
public DonkeyInsert(long seed) {
this.random = new Random(seed);
}
public static void main(String[] args) {
// I'd externalize these so I could change the database without recompiling.
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://hostname:3306/dbname";
String username = "username";
String password = "password";
Connection connection = null;
try {
DonkeyInsert donkeyInsert = new DonkeyInsert();
connection = createConnection(driver, url, username, password);
int numRowsInserted = donkeyInsert.insertPins(connection, DEFAULT__RECORD_COUNT);
System.out.println(String.format("# pins inserted: %d", numRowsInserted));
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(connection);
}
}
private static final String INSERT_SQL = "INSERT INTO login(pin) VALUES(?) ";
public int insertPins(Connection connection, int count) {
int numInserted = 0;
if (count > 0) {
PreparedStatement ps = null;
try {
ps = connection.prepareStatement(INSERT_SQL);
for (int i = 0; i < count; ++i) {
ps.setString(1, this.createRandomPin());
ps.addBatch();
}
int [] counts = ps.executeBatch();
for (int rowCount : counts) {
numInserted += rowCount;
}
} catch (SQLException e) {
throw new RuntimeException("SQL exception caught while inserting pins", e);
} finally {
close(ps);
}
}
return numInserted;
}
public String createRandomPin() {
// Changed it a bit. Didn't understand your requirement.
int k = this.random.nextInt(Integer.MAX_VALUE);
return Integer.toString(k);
}
public static Connection createConnection(String driver, String url, String username, String password) throws ClassNotFoundException, SQLException {
Class.forName(driver);
if ((username == null) || (password == null) || (username.trim().length() == 0) || (password.trim().length() == 0)) {
return DriverManager.getConnection(url);
} else {
return DriverManager.getConnection(url, username, password);
}
}
public static void close(Connection connection) {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(Statement st) {
try {
if (st != null) {
st.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
包持久化;
导入java.sql.Connection;
导入java.sql.DriverManager;
导入java.sql.PreparedStatement;
导入java.sql.SQLException;
导入java.sql.Statement;
导入java.util.Random;
/**
*DonkeyInsert更改
*@作者迈克尔
*@linkhttps://stackoverflow.com/questions/32551895/write-time-in-mysql?noredirect=1#comment52959887_32551895
*@自2015年9月13日12:49
*/
公共类DonkeyInsert{
公共静态最终整数默认记录计数=6000;
私有随机;
公共密钥插入(){
this.random=新的random();
}
公共驴尾(长种子){
this.random=新随机(种子);
}
公共静态void main(字符串[]args){
//我将这些外部化,这样就可以在不重新编译的情况下更改数据库。
String driver=“com.mysql.jdbc.driver”;
String url=“jdbc:mysql://hostname:3306/dbname";
字符串username=“username”;
字符串password=“password”;
连接=空;
试一试{
donkeyinter donkeyinter=新的donkeyinter();
connection=createConnection(驱动程序、url、用户名、密码);
int numRowsInserted=donkeyInsert.insertPins(连接,默认记录计数);
System.out.println(String.format(#插入的管脚数:%d),numRowsInserted));
}catch(classnotfounde异常){
e、 printStackTrace();
}捕获(SQLE异常){
e、 printStackTrace();
}最后{
关闭(连接);
}
}
私有静态最终字符串INSERT_SQL=“插入登录(pin)值(?);
公共int insertPins(连接,int计数){
int numInserted=0;
如果(计数>0){
PreparedStatement ps=null;
试一试{
ps=connection.prepareStatement(插入SQL);
对于(int i=0;i
Easy:使用PreparedStatement绑定变量,并使用批处理执行在单个网络往返中执行。您使用的访问方法是什么?(Myisam,innodb?)你要写的表的定义是什么?@duffymo你能分享一个例子吗?使用批处理语句。这里的更多信息:回答很好,但它花费了5秒。你的代码有更多的错误异常处理,因此使用起来更好。谢谢你,再多5秒?那么,杀死你的不是网络延迟。我建议在你分析代码时,看看时间花在哪里。我们在没有数据的情况下进行推测。我重新启动电脑,再次运行代码,结果好得多。