【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(60) not null, createtime TIMESTAMP (6) 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"; public final static String DbUrl = "jdbc:oracle:thin:@127.0.0.1:1521:orcl"; public final static String User = "ufo"; public final static String Pswd = "1234"; }
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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2017-12-22 【高中数学/极值问题】一条长为L的绳子,一面靠墙,另外三边组成矩形,问此矩形最大面积能是多少?
2014-12-22 Java取得操作系统的临时目录
2014-12-22 将ByteArrayOutputStream类型变量中的数据存储到文件中
2014-12-22 【高中数学/基本不等式】已知:x,y 皆大于0,且xy-x-y=3 求:2x+y的最小值?