java—下面的sql语句使用高达11GB的ram,如何优化ram的使用?

xt0899hw  于 2021-08-01  发布在  Java
关注(0)|答案(0)|浏览(220)

这些是我从中获取数据的sql表

String sql = "CREATE TABLE IF NOT EXISTS term_index (\n" 
                        + " term_id integer PRIMARY KEY AUTOINCREMENT ,\n" 
                        + " term text ,\n"
                        + " multiterm integer \n"
                        + ");";
        execute(sql);

sql = "CREATE TABLE IF NOT EXISTS doc_term (\n" 
                + " term_id integer ,\n"
                + " wiki_id text ,\n"
                + " section text ,\n"
                + " freq integer ,\n"
                + " tfidf double ,\n"
                + " cvalue double ,\n"
                + " rake double ,\n"
                + " PRIMARY KEY (term_id, wiki_id, section) \n"
                + ");";
        execute(sql);

这是从上表中获取termid的sql语句

/**
     *
     * @param wikiID
     * @return
     */
    public HashSet<Integer> getWholeDocumentTermIDs(String wikiID) {
        HashSet<Integer> termIDs = new HashSet<Integer>();

        String sql= "SELECT term_id FROM doc_term WHERE wiki_id = ?";

        try {
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1,wikiID);
            ResultSet rs = stmt.executeQuery();

            while (rs.next()) {

                String sql2= "SELECT term_id, multiterm FROM term_index WHERE term_id = ?";
                PreparedStatement stmt2 = conn.prepareStatement(sql2);
                //System.out.println(rs.getInt("term_id"));

                int term_id = rs.getInt("term_id");

                if(term_id != 0) {
                    stmt2.setInt(1,term_id);

                    ResultSet rs2 = stmt2.executeQuery();
                    int multiterm = rs2.getInt("multiterm");

                    if(multiterm == 0) {

                        termIDs.add(term_id);
                    }
                    rs2.close();
                }

            }

            rs.close();

        } catch (SQLException e) {
            System.out.println(e.getMessage() + "in getWholeDocumentTermIDs");
        }

        return termIDs;
    }

函数getwholedocumenttermids(string wikid)被调用了30k次,这意味着30k个wikiid,所以在执行这个函数时,我的ide使用的内存越来越多,从大约1,3gb开始,到后来的10gb或11gb,这时进程开始缓慢地运行。我的问题是,是否以及如何减少/清理ram以获取存储在这些表中的所有必需数据。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题