sqlite3数据库的性能问题报告

2008-02-23 09:38:34来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

使用JDBC测了一下sqlite3的插入数据的效率,使用整体事务与不使用整体事务,差别非常明显。 cpu: 1G, 内存:512MB
使用整体事务:1000000条记录,需要93秒钟。
使用逐个插入提交事务:1秒钟只能处理20条记录。
启动程序时把Java堆最大空间设大一些,不然可能出现内存不够的异常。
java -Xmx256M TestConn
测试后的结果显示如下:

以下是测试代码:
import java.sql.*;

public class TestConn {
void test(){
Connection conn = null;
Statement stmt = null;
ResultSet rset = null;
System.out.println(new java.util.Date());
try {
Class.forName("SQLite.JDBCDriver");
conn = DriverManager.getConnection(
"jdbc:sqlite:/f:/test.db", //jdbc:Oracle:thin:@hex:1521:ORCL
"",
"");
conn.setAutoCommit(false);
stmt = conn.createStatement();
stmt.executeUpdate("create table hehe(id number, name varchar(32))");
System.out.println("建表hehe成功!");
for (int i=0; i<1000000; i )
{
stmt.executeUpdate("INSERT INTO hehe VALUES(" i ", '我爱中国" i "')");

}

conn.commit();
System.out.println("不建索引查询:");
System.out.println(new java.util.Date());

rset = stmt.executeQuery("SELECT id, name FROM hehe where id > 499999 and name ='我爱中国500001'");
if (rset.next()){
System.out.println(rset.getInt("id"));
System.out.println(rset.getString("name"));
}
if (rset!=null){ rset.close(); rset = null; }

System.out.println(new java.util.Date());

System.out.println("建索引:");
System.out.println(new java.util.Date());
stmt.executeUpdate("CREATE INDEX hehe_idx on hehe(id)");
stmt.executeUpdate("CREATE INDEX hehe_idx2 on hehe(name)");
conn.commit();
System.out.println(new java.util.Date());

System.out.println("建索引后的查询:");
System.out.println(new java.util.Date());
rset = stmt.executeQuery("SELECT id, name FROM hehe where id > 499999 and name = '我爱中国500001'");
if (rset.next()){
System.out.println(rset.getInt("id"));
System.out.println(rset.getString("name"));
}
System.out.println(new java.util.Date());
stmt.executeUpdate("drop table hehe");
System.out.println("删除表hehe成功!");
conn.commit();

System.out.println(new java.util.Date());

} catch(ClassNotFoundException cnfe) {
System.out.println("Can't find class for driver: " cnfe.getMessage());
System.exit(-1);
} catch (SQLException e){
System.out.println(e.getMessage());
System.exit(-1);
} finally
{
try {
if (rset!=null) rset.close();
stmt.close();
conn.close();
} catch (SQLException e) {

}
}
}

public static void main(String[] args) {
TestConn conn = new TestConn();
conn.test();
}
}

上一篇: 数据库连接之jtds介绍(待翻译)
下一篇: 最新网站挂qq代码(php,asp,js)

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:BallCanvas.java---BallGame

下一篇:J2EE Enterprise Beans(原文)