由于压力测试,您需要在数据库中检索大量数据,但数据库中没有太多数据。因此,对于测试,您必须快速将大量临时数据插入数据库。
有两种方法可以快速插入大量数据:一种是使用java代码实现; 另一种是使用数据库存储过程。
首先,你必须有一个数据表,注意数据表的引擎,在构建表时使用MyISAM引擎,MyISAM插入比InnoDB快得多,因为InnoDB的事务支持要好得多,并且在大多数情况下是default使用InnoDB,因此您可以在插入数据后将其修改为InnoDB。
CREATE TABLE `tb_data` (
`id` int(11) DEFAULT NULL,
`user_name` varchar(100) DEFAULT NULL,
`create_time` datetime DEFAULT NULL,
`random` double DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
使用java代码,实际插入100万条数据需要6秒。
代码示例:
package com.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class InsertDataDemo {
static Connection conn = null;
public static void initConn() throws ClassNotFoundException, SQLException {
String url = "jdbc:mysql://localhost:3306/testdb?"
+ "user=root&password=root&useUnicode=true&characterEncoding=UTF8&useSSL=false&serverTimezone=UTC";
try {
/ / Dynamically load mysql driver
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Successfully loaded MySQL driver");
conn = DriverManager.getConnection(url);
} catch (Exception e) {
e.printStackTrace();
}
}
public static String randomStr(int size) {
/ / Define an empty string
String result = "";
for (int i = 0; i < size; ++i) {
/ / Generate an int type integer between 97 ~ 122
int intVal = (int) (Math.random() * 26 + 97);
/ / Force conversion (char) intVal Convert the corresponding value to the corresponding character, and splicing the characters
result = result + (char) intVal;
}
/ / Output string
return result;
}
public static void insert(int insertNum) {
// open time
Long begin = System.currentTimeMillis();
System.out.println("Start Inserting Data...");
// sql prefix
String prefix = "INSERT INTO tb_data (id, user_name, create_time, random) VALUES ";
try {
// save the sql suffix
StringBuffer suffix = new StringBuffer();
/ / Set the transaction to non-automatic commit
conn.setAutoCommit(false);
PreparedStatement pst = conn.prepareStatement("");
for (int i = 1; i <= insertNum; i++) {
/ / Build sql suffix
suffix.append("(" + i +",'"+ randomStr(8) + "', SYSDATE(), " + i * Math.random() + "),");
}
/ / Build a complete sql
String sql = prefix + suffix.substring(0, suffix.length() - 1);
/ / Add execution sql
pst.addBatch(sql);
// perform the operation
pst.executeBatch();
// commit the transaction
conn.commit();
// close the connection
pst.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
// End Time
Long end = System.currentTimeMillis();
System.out.println("insert"+insertNum+" data data is completed!");
System.out.println("Time-consuming : " + (end - begin) / 1000 + "seconds");
}
public static void main(String[] args) throws SQLException, ClassNotFoundException {
initConn();
insert(1000000);
}
}
控制台输出:
Successfully loaded MySQL driver
Start inserting data...
Insert 1000000 data data to complete!
Time consuming : 6 seconds
注意:
适当增加mysql的max_allowed_packet参数值允许系统在客户端到服务器端传递大数据时分配更多扩展内存以进行处理。
修改mysql配置文件:
[mysqld]
net_buffer_length=512k
max_allowed_packet=500M
-- 更改引擎的语句
ALTER TABLE 表名 ENGINE=MyISAM;
-- 更改引擎的语句
ALTER TABLE 表明 ENGINE=InnoDB;