【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, ...
随机推荐
- Linux系统MySQL的常用操作命令
安装好MySQL服务后添加环境变量: #vi /etc/profile export MYSQL_HOME=/usr/local/mysql export PATH=$PATH:$MYSQL_HOME ...
- linux route路由
网关(Gateway)又称网间连接器.协议转换器.网关在网络层以上实现网络互连 就好像一个房间可以有多扇门一样,一台主机可以有多个网关.默认网关的意思是一台主机如果找不到可用的网关,就把数据包发给默认 ...
- 使用composer命令加载vendor中的第三方类库
1.首先下载需要的第三方类库,放在TP框架下的vendor文件夹下 2.给第三方类库SDK写上命名空间,命名空间为该类库的文件夹对应的名字 3.在composer.json文件中添加需要加载的第三方类 ...
- “GIS DICTIONARY A-Z” 查询页面开发(3)—— 基础知识之服务器、IP地址、域名、DNS、端口以及Web程序的访问流程
今天补一补基础知识: 一.服务器:能够提供服务的机器,取决于机器上安装的软件(服务软件).服务器响应服务请求,并进行处理. Web服务器:提供Web服务,即网站访问.常见Web服务软件:Apache( ...
- Prometheus学习笔记(4)什么是pushgateway???
目录 一.pushgateway介绍 二.pushgateway的安装运行和配置 三.自定义脚本发送pushgateway 四.使用pushgateway的优缺点 一.pushgateway介绍 pu ...
- js定时器的应用
定时器分为两种 一种是一次性的,时间到就执行 var timer=setTimeout(fun,毫秒数); 清除的方法 clearTimeout(timer) 第二种是周期性的,根据设定的时间周期进行 ...
- python-Arduino串口传输数据到电脑并保存至excel表格
起因:学校运河杯报了个项目,制作一个天气预测的装置.我用arduino跑了BME280模块,用蓝牙模块实现两块arduino主从机透传.但是为了分析,还需要提取出数据.因此我用python写了个上位机 ...
- HTTP状态码面试必知
typora-root-url: ./HTTPCODE HTTP状态码必知必会 这里主要介绍运维过程中经常遇到的状态码.并通过业界流行的Nginx进行模拟实现,让大家能有一种所见即所得的感觉.希望大家 ...
- JUnit 5.x 知识点
出处:https://blinkfox.github.io/2018/11/15/hou-duan/java/dan-yuan-ce-shi-zhi-nan/#toc-heading-14 表面上来看 ...
- 20180527模拟赛T1——新田忌赛马
[问题描述] (注:此题为d2t2-难度) 田忌又在跟大王van赛马的游戏 田忌与大王一共有2n匹马,每个马都有一个能力值x,1<=x<=2n且每匹马的x互不相同.每次田忌与大王放出一匹马 ...