我希望从ngrams(HashMap)中提取数据库中的所有值。我的问题是如何更好地实现提取功能。这段代码的执行很慢,因为每次在表中存在HashMap的条目时,java程序都必须检查。如果存在则必须从表中选择先前的频率并将其添加到新表中。
我可以更改三个功能(更新,插入,检查)以使此过程更好吗?
这是与数据库连接的代码
public boolean insert(String w1, String w2, String w3, String w4, Integer f, float rf) throws SQLException{
...
String sql = "INSERT INTO fourgrams " +
"VALUES("+w1 + "," + w2 + "," + w3 + "," + w4 + "," + f + ',' + rf + ")"
...
}
public boolean update(String w1, String w2, String w3, String w4, Integer f) throws SQLException{
...
String sql = "UPDATE fourgrams " +
"SET frequency =" + f + " WHERE gram1=" + w1 +
" AND gram2=" + w2 + " AND gram3=" + w3 +
" AND gram3=" + w3;
...
}
public int check(String w1, String w2, String w3, String w4) throws SQLException{
...
String sql = "SELECT frequency FROM fourgrams " +
"WHERE gram1=" + w1 +
" AND gram2=" + w2 + " AND gram3=" + w3 +
" AND gram3=" + w3;
...
}
这是我遇到问题的代码
public void extract(Database myDB) throws SQLException{
Iterator it = ngrams.entrySet().iterator();
while(it.hasNext()){
...
int fr=myDB.check(...);
if (fr == -1)
{
myDB.insert(...);
}else{
myDB.update(...);
}
}
...
}
答案 0 :(得分:0)
1)插入。在你的循环中构建一个表单
的插入INSERT INTO fourgrams VALUES
(w1..),
(w1..),
(w1..)..
2)更新。以相同的方式创建临时表并从该表更新
UPDATE fourgrams
JOIN temp ON fourgrams.gram1= temp.gram1 AND ...
SET frequency = temp.frequency
如果您的数据集太大,您可能希望将它们作为CSV传输到服务器计算机并加载INFILE http://dev.mysql.com/doc/refman/5.7/en/load-data.html