• mysql快速插入大数据


    说的是插入数据,这个倒像是载入数据。

    上一篇,是按照插入数据来写的,就是insert into,当时插入一万条实在是太慢了,大概是286734毫秒。

    insert into table values,这个values后面都跟上内容的话,大概会快一点,具体时间没测。

    然后在网上发现一个牛逼的方式:LOAD DATA INFILE。这个相当快,直接载入txt文本文件中的数据。

    前面写了一篇博文,是生成一个txt,向其中写入N条数据,这样就很容易获取这个数据了。写了一堆数据到txt,然后在load data infile到mysql里面。

    生成数据文章地址:java写入换行符

     

    还有一篇文章,讲load data infile的,这个是转载的,很实用。地址:mysql导入数据load data infile用法(转)

    思路就这样,瞬间很简单了,插入一万条数据,时间是156毫秒,确实很快。

    代码写的比较粗糙,如下:

    package action;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    
    public class InsertData3 {
        
    
        
        public  Connection getConnection(){
             // 驱动程序名
           String driver = "com.mysql.jdbc.Driver";
           // URL指向要访问的数据库名scutcs
           String url = "jdbc:mysql://127.0.0.1:3306/test";
           // MySQL配置时的用户名
           String user = "root";
           // MySQL配置时的密码
           String password = "12345";
           Connection conn=null;
    
           try {
            // 加载驱动程序
            Class.forName(driver);
            // 连续数据库
            conn = DriverManager.getConnection(url, user, password);
           }
           catch (Exception e) {
                 System.out.println("Sorry,can`t find the Driver!");
                e.printStackTrace();// TODO: handle exception
            }
           return conn;
        }
    
        
         boolean insertData() throws SQLException{
                InsertData3 insertData=new InsertData3();
            Connection conn=insertData.getConnection();
           if(!conn.isClosed())
            System.out.println("begin insert!");
           // statement用来执行SQL语句
           Statement statement = conn.createStatement();
           // 要执行的SQL语句
           boolean n=false;
           long a=System.currentTimeMillis();
             String sql = "load data infile 'D:\myios.txt' ignore into table t0 character set gbk fields terminated by ',' enclosed by '"' lines terminated by '\n' (`name`,`age`,`description`);";
                n=statement.execute(sql);
           long b=System.currentTimeMillis();
           long c=b-a;
           System.out.println("spend time:"+c);
           System.out.println("insert end!");
            return n;
        }
        
        public static void main(String[] args) throws FileNotFoundException, SQLException {
             // TODO Auto-generated method stub
            String pathname="d:/myios.txt";
            File file=new File(pathname);
            FileOutputStream fop=new FileOutputStream(file);
            String crlf=System.getProperty("line.separator");
            try{
                if (!file.exists()) {
                    file.createNewFile();
                }
                
                for(int i=0;i<10000;i++){
                    String content="'"+i+"','data','base'"+crlf; //直接添加换行的即可
                    byte[] contents=content.getBytes();
                    fop.write(contents);    
    
                }
          
                String endString="done";
                byte[] ends=endString.getBytes();
                fop.write(ends);
                fop.flush();
                fop.close();
                
            } catch (Exception e) {
                // TODO: handle exception
            }
            
            
            InsertData3 insertData3=new InsertData3();
            boolean m=insertData3.insertData();
            System.out.println("boolean:"+m);
        }
        
    }
  • 相关阅读:
    [Java] 编写第一个java程序
    [Java] 环境变量设置
    [ActionScript 3.0] 常用的正则表达式
    [ActionScript 3.0] 正则表达式
    Python学习之==>URL编码解码&if __name__ == '__main__'
    Python学习之==>面向对象编程(一)
    Linux下安装redis-4.0.10
    Linux下编译安装Python-3.6.5
    Python学习之==>发送邮件
    Python学习之==>网络编程
  • 原文地址:https://www.cnblogs.com/juepei/p/3741167.html
Copyright © 2020-2023  润新知