如何从一个表中检索一百万行并将其插入另一个表?

时间:2018-08-20 16:26:31

标签: java mysql sql jdbc

我有一个SQL连接查询,返回一百万行。

SQL查询是:

 SELECT d.* 
 FROM snomed_conceptdata c, snomed_descriptiondata d 
 WHERE c.active = 1 and conceptid = c.id AND d.active = 1

我该如何检索此数据并将其插入另一个表而不会遇到内存不足堆空间错误。我目前正在使用Mysql数据库。

我当前的代码:

package Snomed.Snomed;

import java.sql.PreparedStatement;
import java.sql.ResultSet;


import catalog.Root;

public class Snomedinfo {

    public void snomedinfoinsert()
    {
    Root oRoot = null;
    ResultSet oRsSelect = null;
    PreparedStatement oPrStmt = null;
    PreparedStatement oPrStmt2 = null;
    PreparedStatement oPrStmtSelect = null;
    String strSql = null;


    ResultSet oRs = null;
    String refid = null;
    String id = null;
    String effectivetime = null;
    String active = null;
    String moduleid = null;
    String conceptid = null;
    String languagecode = null;
    String typeid = null;
    String term = null;
    String caseSignificanceid = null;

    try{
    oRoot = Root.createDbConnection(null);
    strSql = "SELECT d.* FROM snomed_conceptdata c, snomed_descriptiondata d WHERE c.active = 1 and conceptid = c.id AND d.active = 1  "; //this query returns a million rows.


    oPrStmt2 = oRoot.con.prepareStatement(strSql);

    oRs = oPrStmt2.executeQuery();

    String sql = "INSERT INTO snomedinfo_date (refid,id,effectivetime,active,moduleid,conceptid,languagecode,typeid,term,caseSignificanceid) VALUES( ?, ?, ?,?,?,?,?,?,?,?)";
    oPrStmt = oRoot.con.prepareStatement(sql);





        while (oRs.next())
        {
            refid = Root.TrimString(oRs.getString("refid")); 
            id = Root.TrimString(oRs.getString("id"));
            effectivetime = Root.TrimString(oRs.getString("effectivetime"));
            active = Root.TrimString(oRs.getString("active"));
            moduleid = Root.TrimString(oRs.getString("moduleid"));
            conceptid = Root.TrimString(oRs.getString("conceptid"));
            languagecode = Root.TrimString(oRs.getString("languagecode"));
            typeid = Root.TrimString(oRs.getString("typeid"));
            term = Root.TrimString(oRs.getString("term"));
            caseSignificanceid = Root.TrimString(oRs.getString("caseSignificanceid"));  


            oPrStmt.setString(1, refid);
            oPrStmt.setString(2, id);
            oPrStmt.setString(3, effectivetime);
            oPrStmt.setString(4, active);
            oPrStmt.setString(5, moduleid);
            oPrStmt.setString(6, conceptid);
            oPrStmt.setString(7, languagecode);
            oPrStmt.setString(8, typeid );
            oPrStmt.setString(9, term);
            oPrStmt.setString(10, caseSignificanceid);
             oPrStmt.executeUpdate();
        }






    }

    catch (Exception e) {
        e.printStackTrace();

    }

    finally {

        oRsSelect = Root.EcwCloseResultSet(oRsSelect);
        oRs = Root.EcwCloseResultSet(oRs);
        oPrStmt = Root.EcwClosePreparedStatement(oPrStmt);
        oPrStmt = Root.EcwClosePreparedStatement(oPrStmt2);
        oPrStmt = Root.EcwClosePreparedStatement(oPrStmtSelect);
        oRoot = Root.closeDbConnection(null, oRoot);
    }


}
    public static void main(String args[] ) throws Exception 
      { 

      Snomedinfo a = new Snomedinfo();
      a .snomedinfoinsert();

      }


}

1 个答案:

答案 0 :(得分:5)

在mysql中,您可以使用create select;如果表已经存在,则可以使用insert select

create my_table_copy
SELECT d.* 
FROM snomed_conceptdata c
INNER JOIN snomed_descriptiondata d ON c.active = 1 
          and conceptid = c.id 
              AND d.active = 1

insert into   your_table  (col1, col2, col3)
SELECT d.col1, d.col2, d.col3 
FROM snomed_conceptdata c
INNER JOIN snomed_descriptiondata d ON c.active = 1 
          and conceptid = c.id 
              AND d.active = 1.51

确保您有足够的磁盘空间