【java/oralce/sql】往一张仅有id,名称,创建时间三个字段的表中插入百万数据需要多久?1分26秒
代码下载:https://files.cnblogs.com/files/xiandedanteng/fastfilltable20191222.rar
表testtb18的结构如下:
CREATE TABLE testtb18
(
id NUMBER not null primary key,
name NVARCHAR2() not null,
createtime ) not null
)
三个字段,正好是常用的number,nvarcha2,timestamp类型。
用java程序创建这表表的代码如下:
/**
* 数据库连接参数
* @author 逆火
*
* 2019年11月16日 上午8:09:24
*/
public final class DBParam {
public final static String Driver = "oracle.jdbc.driver.OracleDriver";
::orcl";
public final static String User = "ufo";
";
}
package com.hy.fastfilltable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.log4j.Logger;
// Used to create a table in oracle
public class TableCreater {
private static Logger log = Logger.getLogger(TableCreater.class);
private final String table="testtb18";
public boolean createTable() {
Connection conn = null;
Statement stmt = null;
try{
Class.forName(DBParam.Driver).newInstance();
conn = DriverManager.getConnection(DBParam.DbUrl, DBParam.User, DBParam.Pswd);
stmt = conn.createStatement();
String createTableSql=getCreateTbSql(table);
stmt.execute(createTableSql);
if(isTableExist(table,stmt)==true) {
log.info("Table:'"+table+"' created.");
return true;
}
} catch (Exception e) {
System.out.print(e.getMessage());
} finally {
try {
stmt.close();
conn.close();
} catch (SQLException e) {
System.out.print("Can't close stmt/conn because of " + e.getMessage());
}
}
return false;
}
/**
* Get a table's ddl
* @param table
* @return
*/
private String getCreateTbSql(String table) {
StringBuilder sb=new StringBuilder();
sb.append("CREATE TABLE "+table);
sb.append("(");
sb.append("id NUMBER not null primary key,");
sb.append("name NVARCHAR2(60) not null,");
sb.append("createtime TIMESTAMP (6) not null");
sb.append(")");
return sb.toString();
}
// Execute a sql
//private int executeSql(String sql,Statement stmt) throws SQLException {
// return stmt.executeUpdate(sql);
//}
// If a table exists
private boolean isTableExist(String table,Statement stmt) throws SQLException {
String sql="SELECT COUNT (*) as cnt FROM ALL_TABLES WHERE table_name = UPPER('"+table+"')";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
int count = rs.getInt("cnt");
return count==1;
}
return false;
}
// Entry point
public static void main(String[] args) {
TableCreater tc=new TableCreater();
tc.createTable();
}
}
现在我想就往这张表里添值,到一百万条记录,可以这么做:
package com.hy.fastfilltable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DecimalFormat;
import org.apache.log4j.Logger;
import com.hy.DBParam;
public class FastTableFiller {
private static Logger log = Logger.getLogger(FastTableFiller.class);
private final String Table="testtb18";
private final int Total=1000000;
public boolean fillTable() {
Connection conn = null;
Statement stmt = null;
try{
Class.forName(DBParam.Driver).newInstance();
conn = DriverManager.getConnection(DBParam.DbUrl, DBParam.User, DBParam.Pswd);
conn.setAutoCommit(false);
stmt = conn.createStatement();
long startMs = System.currentTimeMillis();
clearTable(stmt,conn);
fillDataInTable(stmt,conn);
long endMs = System.currentTimeMillis();
log.info("It takes "+ms2DHMS(startMs,endMs)+" to fill "+toEastNumFormat(Total)+" records to table:'"+Table+"'.");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
stmt.close();
conn.close();
} catch (SQLException e) {
System.out.print("Can't close stmt/conn because of " + e.getMessage());
}
}
return false;
}
private void clearTable(Statement stmt,Connection conn) throws SQLException {
stmt.executeUpdate("truncate table "+Table);
conn.commit();
log.info("Cleared table:'"+Table+"'.");
}
private void fillDataInTable(Statement stmt,Connection conn) throws SQLException {
StringBuilder sb=new StringBuilder();
sb.append(" Insert into "+Table);
sb.append(" select rownum,dbms_random.string('*',50),sysdate from dual ");
sb.append(" connect by level<="+Total);
sb.append(" order by dbms_random.random");
String sql=sb.toString();
stmt.executeUpdate(sql);
conn.commit();
}
// 将整数在万分位以逗号分隔表示
public static String toEastNumFormat(long number) {
DecimalFormat df = new DecimalFormat("#,####");
return df.format(number);
}
// change seconds to DayHourMinuteSecond format
private static String ms2DHMS(long startMs, long endMs) {
String retval = null;
long secondCount = (endMs - startMs) / 1000;
String ms = (endMs - startMs) % 1000 + "ms";
long days = secondCount / (60 * 60 * 24);
long hours = (secondCount % (60 * 60 * 24)) / (60 * 60);
long minutes = (secondCount % (60 * 60)) / 60;
long seconds = secondCount % 60;
if (days > 0) {
retval = days + "d" + hours + "h" + minutes + "m" + seconds + "s";
} else if (hours > 0) {
retval = hours + "h" + minutes + "m" + seconds + "s";
} else if (minutes > 0) {
retval = minutes + "m" + seconds + "s";
} else {
retval = seconds + "s";
}
return retval + ms;
}
// Entry point
public static void main(String[] args) {
FastTableFiller f=new FastTableFiller();
f.fillTable();
}
}
执行效果还不错:
2019-12-22 15:21:02,412 INFO[main]-Cleared table:'testtb18'. 2019-12-22 15:22:28,669 INFO[main]-It takes 1m26s268ms to fill 100,0000 records to table:'testtb18'.
注意:插一千万数据就会报oom异常,怎么解决请大家自行考虑,我暂时没这样的需求。
再看看表中情况;

--END-- 2019年12月22日15:35:27
【java/oralce/sql】往一张仅有id,名称,创建时间三个字段的表中插入百万数据需要多久?1分26秒的更多相关文章
- 【Oracle/Java】向三张表各插入百万数据,共用时18分3秒,平均每张表6分钟
三张表DDL如下: CREATE TABLE tb01 ( "ID" ,) not null primary key, "NAME" NVARCHAR2() n ...
- 【Oracle/Java】以Insert ALL方式向表中插入百万条记录,耗时9分17秒
由于按一千条一插程序长期无反应,之后改为百条一插方式,运行完发现插入百万记录需要9m17s,虽然比MySQL效率差,但比单条插入已经好不少了. 对Oracle的批量插入语法不明的请参考:https:/ ...
- EF Core中,通过实体类向SQL Server数据库表中插入数据后,实体对象是如何得到数据库表中的默认值的
我们使用EF Core的实体类向SQL Server数据库表中插入数据后,如果数据库表中有自增列或默认值列,那么EF Core的实体对象也会返回插入到数据库表中的默认值. 下面我们通过例子来展示,EF ...
- sql 所有数据表中 插入字段
declare @tablename varchar(200)declare @sql varchar(2000)declare cur_t cursor forselect name from sy ...
- SQL Server批量向表中插入多行数据语句
因自己学习测试需要,需要两个有大量不重复行的表,表中行数越多越好.手动编写SQL语句,通过循环,批量向表中插入数据,考虑到避免一致问题,设置奇偶行不同.个人水平有限,如有错误,还望指正. 语句如下: ...
- MySQL_(Java)使用JDBC向数据库中插入(insert)数据
MySQL_(Java)使用JDBC向数据库发起查询请求 传送门 MySQL_(Java)使用JDBC向数据库中插入(insert)数据 传送门 MySQL_(Java)使用JDBC向数据库中删除(d ...
- SQL 在表中插入
SQL INSERT INTO 语句(在表中插入) INSERT INTO 语句用于向表中插入新记录. SQL INSERT INTO 语句 INSERT INTO 语句用于向表中插入新记录. SQL ...
- SQL语句 在一个表中插入新字段
SQL语句 在一个表中插入新字段: alter table 表名 add 字段名 字段类型 例: alter table OpenCourses add Audio varchar(50)alter ...
- SQL语句更新时间字段的年份、月份、天数、时、分、秒
SQL语句更新时间字段的年份.月份.天数.时.分.秒 --修改d表日期字段的年份update dset birth=STUFF(convert(nvarchar(23),birth,120),1,4, ...
随机推荐
- JavaScript 数组(二)数组练习
1.求一组数中的最大值和最小值,以及所在位置(最大值和最小值在这组数中的位置) var numbers = [120, 13, 101, 88, 10, 25]; var max = numbers[ ...
- Mac安装vscode IDE 撸nodejs代码
1. vscode官网地址:https://code.visualstudio.com 找到mac对应的下载地址,下载后的文件是 zip压缩包,解压后将文件拖到Application目录下即可. ...
- linux cgroups简介(上)
Linux CGroups简介 1.CGroups是什么 与Linux namespace对比来看,Linux namespace用来限制进程的运行范围或者运行环境的可见性,比如:uts限制进程读取到 ...
- centos源码编译安装新版本内核
在工作中,很多时候由于需要使用新的技术方案,需要较新版本的内核来支持新的功能,而centos系统自带的内核版本普遍都比较旧,因此我们需要对系统的内核进行升级,安装新版的内核.本文以centos7系 ...
- Haproxy 让后端RS记录真实IP
一.修改haproxy.cfg配置文件,在defaults中加入如下两行,并重启haproxy. vim /etc/haproxy/haproxy.cfg defaults option http-s ...
- flutter 中的样式
flutter 中的样式 样式 值 width 320.0 height 240.0 color Colors.white,Colors.grey[300] textAlign TextAlign.c ...
- tomcat学习之路
一:介绍目录文件夹 bin文件夹:放置可执行文件 startup.bat:脚本文件,windows环境,起服务 shutdown.bat:脚本文件,windows环境,关闭 startup.sh:脚本 ...
- ie6下标签定义的高失效,显示的高不受设定的height值影响
今天又碰到一个奇葩的ie6兼容bug,忍不住抱怨下这个后妈生的鬼东西!! 看图这个是在非ie6下的浏览器效果
- 31 Game-Based Learning Resources for Educators
https://www.legendsoflearning.com/resource/31-game-based-learning-resources-for-educators/ Game base ...
- 在IAR平台建立STC8ASK64S4A12单片机工程
转载:http://www.51hei.com/bbs/forum.php?mod=viewthread&tid=168481&page=1#pid737250 一般我们使用STC单 ...