• 【java/oracle】千万级表创建程序新版


    代码:

    package com.hy.lab.tenmillion;
    
    import java.sql.*;
    import java.util.ArrayList;
    import java.util.List;
    
    public class BigTbBuilder {
        private static final int BATCH_SIZE=100000;
    
        public static Connection getConnection() {
            Connection conn = null;
            try {
                Class.forName("oracle.jdbc.driver.OracleDriver");
                String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
                String user = "luna";
                String pass = "1234";
                conn = DriverManager.getConnection(url, user, pass);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return conn;
        }
    
        /**
         * 创建一张大表
         * @param tableName 表名
         * @param colCnt 除id之外的列数
         * @param rowCnt 记录数
         * @return
         */
        public boolean buildTable(String tableName,int colCnt,int rowCnt){
            List<String> columns=new ArrayList<>();
            for(int i=0;i<colCnt;i++){
                String columnName="F"+(i+1);
                columns.add(columnName);
            }
    
            // Create Table sql
            StringBuilder sb=new StringBuilder();
            sb.append(String.format("create table %s(",tableName));
            sb.append("    id number(12),");
            for(String column:columns){
                sb.append(String.format("%s nvarchar2(20),",column));
            }
            sb.append("    primary key(id)");
            sb.append(")");
            String createTableSql=sb.toString();
            //System.out.println(createTableSql);
    
            // Create Insert Sql
            String[] asks=new String[colCnt];
            for(int i=0;i<colCnt;i++){
                asks[i]="?";
            }
            String insertSql=String.format("insert into %s(id,%s) values(?,%s)",tableName,String.join(",",columns),String.join(",",asks));
    
            try(Connection conn =getConnection();
                Statement stmt =conn.createStatement();
                PreparedStatement pstmt =conn.prepareStatement(insertSql)){
    
                // drop table if exists
                if(isTableExist(tableName,conn)){
                    System.out.println(String.format("发现表%s已存在",tableName));
                    String dropSql=String.format("drop table %s",tableName);
                    stmt.execute(dropSql);
                    System.out.println(String.format("表%s已删除",tableName));
                }else{
                    System.out.println(String.format("发现表%s不存在,无需删除",tableName));
                }
    
                // create table
                stmt.execute(createTableSql);
                System.out.println(String.format("表%s已建成",tableName));
    
                conn.setAutoCommit(false);
                final String FIX_STRING="ABCDEFGHIJ0123456789";
    
                int count=0;
                int submitCount=0;
                for(int i=0;i<rowCnt;i++){
                    pstmt.setLong(1,i);
                    for(int j=0;j<colCnt;j++){
                        pstmt.setString(j+2,FIX_STRING);
                    }
    
                    pstmt.addBatch();
    
                    count++;
                    if(count>BATCH_SIZE){
                        pstmt.executeBatch();
                        conn.commit();
                        count=0;
    
                        submitCount++;
                        System.out.println(String.format("第%-3d次提交",submitCount));
                    }
                }
    
                pstmt.executeBatch();
                conn.commit();
                submitCount++;
                System.out.println(String.format("第%-3d次提交",submitCount));
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return true;
        }
    
        private boolean isTableExist(String tablename,Connection conn) throws Exception{
            DatabaseMetaData meta= conn.getMetaData();
    
            ResultSet set=meta.getTables(null,null,tablename.toUpperCase(),null);
            if(set.next()){
                return true;
            }
    
            return false;
        }
    
        public static void main(String[] args){
            long startMs=System.currentTimeMillis();
    
            BigTbBuilder injecter=new BigTbBuilder();
            injecter.buildTable("emp67_30",30,10000000);
    
            long endMs=System.currentTimeMillis();
            System.out.println("Time elapsed:"+ ms2DHMS(startMs,endMs));
        }
    
        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 if(seconds > 0) {
                retval = seconds + "s";
            }else {
                return ms;
            }
    
            return retval + ms;
        }
    }

    输出:

    发现表emp615_30已存在
    表emp615_30已删除
    表emp615_30已建成
    第1  次提交
    第2  次提交
    第3  次提交
    第4  次提交
    第5  次提交
    第6  次提交
    第7  次提交
    第8  次提交
    第9  次提交
    第10 次提交
    第11 次提交
    第12 次提交
    第13 次提交
    第14 次提交
    第15 次提交
    第16 次提交
    第17 次提交
    第18 次提交
    第19 次提交
    第20 次提交
    第21 次提交
    第22 次提交
    第23 次提交
    第24 次提交
    第25 次提交
    第26 次提交
    第27 次提交
    第28 次提交
    第29 次提交
    第30 次提交
    第31 次提交
    第32 次提交
    第33 次提交
    第34 次提交
    第35 次提交
    第36 次提交
    第37 次提交
    第38 次提交
    第39 次提交
    第40 次提交
    第41 次提交
    第42 次提交
    第43 次提交
    第44 次提交
    第45 次提交
    第46 次提交
    第47 次提交
    第48 次提交
    第49 次提交
    第50 次提交
    第51 次提交
    第52 次提交
    第53 次提交
    第54 次提交
    第55 次提交
    第56 次提交
    第57 次提交
    第58 次提交
    第59 次提交
    第60 次提交
    第61 次提交
    第62 次提交
    第63 次提交
    第64 次提交
    第65 次提交
    第66 次提交
    第67 次提交
    第68 次提交
    第69 次提交
    第70 次提交
    第71 次提交
    第72 次提交
    第73 次提交
    第74 次提交
    第75 次提交
    第76 次提交
    第77 次提交
    第78 次提交
    第79 次提交
    第80 次提交
    第81 次提交
    第82 次提交
    第83 次提交
    第84 次提交
    第85 次提交
    第86 次提交
    第87 次提交
    第88 次提交
    第89 次提交
    第90 次提交
    第91 次提交
    第92 次提交
    第93 次提交
    第94 次提交
    第95 次提交
    第96 次提交
    第97 次提交
    第98 次提交
    第99 次提交
    第100次提交
    Time elapsed:38m53s203ms
    
    Process finished with exit code 0

    END

    END

  • 相关阅读:
    phpstrom+xdebug+chrome+postman调试工具搭建
    Docker常用命令
    使用nginx+docker配置https负载均衡
    LVS三种模式的区别及负载均衡算法
    ELK Betas 6.0安装及使用
    Excel从低级到中级
    我回来了,哈哈
    刷电信版s710d卡在htc开机画面的解决办法
    基于cocos2d-android-1的FlyppyBird
    利息?hii
  • 原文地址:https://www.cnblogs.com/heyang78/p/16353865.html
Copyright © 2020-2023  润新知