1吻谋,硬件環(huán)境:win7 64位內(nèi)存10g,ssd(win7開(kāi)機(jī)9秒)數(shù)據(jù)庫(kù)mysql5.6x64
2建立測(cè)試表
create table test_user(
name varchar(20),
age int,
birthday DATETIME
)
java啟動(dòng)類
package index;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class InsertDate {
static String driverClass = "com.mysql.jdbc.Driver";
static String url="jdbc:mysql://127.0.0.1:3306/test";
static String user="root";
static String password="root";
static int lock = 0;
static void reduceLock() {lock--;}
public static void main(String[] args) throws InterruptedException, ClassNotFoundException {
Class.forName(driverClass);
System.out.println(new java.util.Date());
for(int i =0;i<150;i++) {
Connection conn = null;
try {
conn = DriverManager.getConnection(url,user,password);
conn.setAutoCommit(false);
} catch (SQLException e) {
e.printStackTrace();
}
if(conn!=null) {
new Thread(new InserThread(conn,i)).start();
lock++;
}
}
while(lock>0) {
Thread.sleep(100);
}
System.out.println("主線程結(jié)束");
System.out.println(new java.util.Date());
}
}
package index;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Random;
public class InserThread implements Runnable{
PreparedStatement ps = null;
Connection conn = null;
static String sql = "insert into test_user(name,age,birthday) values(?,?,?)";
int threadId = 0;
static final char[] ku = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
static Random random = new Random();
public InserThread(Connection c,int id) {
conn=c;
threadId = id;
}
@SuppressWarnings("deprecation")
@Override
public void run() {
System.out.println("線程:"+threadId+"開(kāi)始");
try {
ps = conn.prepareStatement(sql);
for(int i =0;i<10000;i++) {
ps.setString(1,getRandomString());
ps.setInt(2, (int)(random.nextInt(80)+10));
ps.setDate(3, new Date(random.nextInt(2016),random.nextInt(12),random.nextInt(30)));
ps.execute();
}
conn.commit();
} catch (SQLException e1) {
try {
conn.rollback();
} catch (SQLException e) {
e.printStackTrace();
}
e1.printStackTrace();
}finally {
try {
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("線程:"+threadId+"結(jié)束");
InsertDate.reduceLock();
}
}
static String getRandomString() {
int length = random.nextInt(16)+3;
StringBuilder sb = new StringBuilder();
while(length>1) {
sb.append(ku[random.nextInt(ku.length-1)+1]);
length--;
}
return sb.toString();
}
}
我這邊mysql最大連接數(shù)超過(guò)170就報(bào)錯(cuò)现横,我選擇150
這樣一次能插入150萬(wàn)條數(shù)據(jù)漓拾,分十次插入
現(xiàn)在是1350萬(wàn)條數(shù)據(jù)
select count() from test_user
第一次24秒第二次20秒
select * from test_user where name = 'spB58dvvpJ'
一條記錄,用時(shí)22秒(垃圾戒祠,同一臺(tái)電腦sqlserver四千萬(wàn)記錄全表掃描7秒)
select count(),age
from test_user
group by age
26秒骇两,基本人為全表掃描就是這個(gè)時(shí)間吧
name字段添加索引
ALTER TABLE test_user ADD INDEX name_index ( name)