使用java(netbeans)的Sqlite ExecuteQuery非常慢

时间:2015-04-12 20:38:08

标签: java performance sqlite netbeans jdbc

我使用sqlite来存储数据。我试图从sqlite表视图中获取数据并填充java中的对象数组,但查询执行需要很长时间。 我只有32个对象,22个字段,sqlite有380行。 但是对于执行类似的声明,我花了17秒钟来处理32个物体。

 sql = "SELECT "
                        + "           field1,"
                        + "           field2,"
                        ....
                        + "           field22"
                        + " from Rankedview WHERE Ranking = " + Integer.toString(RankingIndex);
 try (ResultSet rs = stmt.executeQuery(sql)) {
                    while (rs.next()) {
                        a[j].field1= rs.getString("field1");
                        ..........
                        a[j].field22 = rs.getInt("field22");
                    }

                }

我将sqlite-jdbc驱动程序从3.7.2更新到3.8.5时间从17秒降低到9秒。 image

如何改善其表现?

编辑:

视图定义(ATP是表格)

CREATE VIEW Ranked AS
SELECT p1.ID,
       p1.field2,
       ...

       p1.field21,
       (
           SELECT count() + (
                                SELECT count() + 1
                                  FROM Table AS p2
                                 WHERE p2.field21 = p1.field21 AND 
                                       p2.id > p1.id
                            )
             FROM ATP AS p2
            WHERE p2.field21 > p1.field21
       )
       AS Ranking
  FROM ATP AS p1
 ORDER BY Ranking ASC;

EXPLAIN QUERY PLAN输出:

selectid order from detail
0   0   0   SCAN TABLE ATP AS p1
0   0   0   EXECUTE CORRELATED SCALAR SUBQUERY 1
1   0   0   SCAN TABLE ATP AS p2
1   0   0   EXECUTE CORRELATED SCALAR SUBQUERY 2
2   0   0   SEARCH TABLE ATP AS p2 USING INTEGER PRIMARY KEY (rowid>?)
0   0   0   EXECUTE CORRELATED SCALAR SUBQUERY 3
3   0   0   SCAN TABLE ATP AS p2
3   0   0   EXECUTE CORRELATED SCALAR SUBQUERY 4
4   0   0   SEARCH TABLE ATP AS p2 USING INTEGER PRIMARY KEY (rowid>?)
0   0   0   EXECUTE CORRELATED SCALAR SUBQUERY 5
5   0   0   SCAN TABLE ATP AS p2
5   0   0   EXECUTE CORRELATED SCALAR SUBQUERY 6
6   0   0   SEARCH TABLE ATP AS p2 USING INTEGER PRIMARY KEY (rowid>?)
0   0   0   USE TEMP B-TREE FOR ORDER BY

1 个答案:

答案 0 :(得分:0)

要获得具有特定排名的行,您不应手动计算排名,而是使用LIMIT/OFFSET clauses

SELECT ...
FROM ATP
ORDER BY field21, id
LIMIT 1 OFFSET x

这仍然需要对所有表行进行排序以确定哪个是第x行,但是比多个嵌套表扫描更有效。