简体   繁体   English

如何提高Java中的选择查询性能?

[英]How to improve the select query performance in Java?

I am using BerkeleyDB Database and I am performing select query that require 409 ms. 我正在使用BerkeleyDB数据库,并且正在执行需要409毫秒的select查询。 How to improve the select query performance? 如何提高选择查询的性能?

I am using the following code: 我正在使用以下代码:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class ReadData {

Connection con=null;
ResultSet rs=null;
Statement smt = null;

public void readData() 
{
     try
     {
         Class.forName("SQLite.JDBCDriver");
         con = DriverManager.getConnection("jdbc:sqlite:/D:\\DB\\Mediation.db");
         smt = con.createStatement();

         long startTime = System.currentTimeMillis();
         rs = smt.executeQuery("select * from CDRData");             
         while(rs.next())
         {
             System.out.println(rs.getString(1)+" , "+rs.getString(2)+" , "+rs.getString(3)+" , "+rs.getString(4)+" , "+rs.getString(5)+" , "+rs.getString(6)+" , "+rs.getString(7)+" , "+rs.getString(8)+" , "+rs.getString(9)+" , "+rs.getString(10)+" , "+rs.getString(11)+" , "+rs.getString(12)+" , "+rs.getString(13)+" , "+rs.getString(14)+" , "+rs.getString(15)+" , "+rs.getString(16)+" , "+rs.getString(17)+" , "+rs.getString(18)+" , "+rs.getString(19)+" , "+rs.getString(20)+" , "+rs.getString(21)+" , "+rs.getString(22)+" , "+rs.getString(23));
         }
         long finishTime = System.currentTimeMillis();
         System.out.println("The time taken by select query : "+(finishTime-startTime)+ " ms");
     }
     catch(Exception e)
     {
         System.out.println("Error ---- "+e);
     }
}

public static void main(String[] args) {
    ReadData csvread = new ReadData();
    csvread.readData();
}
}

@Dhananjay乔希我的第一recomendation它是由需要该改变的选择select * ,并把你确实需要当你做一个字段select *您带来了很多,也许你并不需要的字段,需要更多的内存

In general terms when using JDBC you may see a performance increase by 一般而言,使用JDBC时,性能可能会提高

  • using a StoredProcedure - this elimentates some pre-processing by the database engine 使用StoredProcedure-这消除了数据库引擎的一些预处理
  • using a PreparedStatement - this can elimenate some pre-processing if you are using a query multiple times. 使用PreparedStatement-如果您多次使用查询,则可以消除某些预处理。

You can also improve time by optimising your query, perhaps by adding appropriate indexes. 您还可以通过优化查询(也许可以添加适当的索引)来缩短时间。 Using explain can help you understand the actions and cost that the query will ancounter. 使用explain可以帮助您了解查询将要执行的操作和成本。

However, in your example you are just executing a simple select with no predicate, so none of these will help you. 但是,在您的示例中,您只是在执行不带谓词的简单选择,因此这些都不会对您有帮助。

I don't think there is anything to improve with your code in terms of DB access. 我认为就数据库访问而言,您的代码没有任何改进。 Selecting all rows is a trivial task for a database which basically just takes a certain amount of time. 对于数据库而言,选择所有行是一项琐碎的任务,这基本上只需要花费一定的时间。 using a PreparedStatement would give you some improvement, if you repeatedly have the same/similar queries, but not in your test scenario. 如果您反复有相同/相似的查询,但在测试场景中却没有,则使用PreparedStatement会有所改善。

You have a little overhead in your java part though which comes from allocating/concatenating string objects. 尽管您的Java部分有一些开销,但这来自分配/连接字符串对象。 You might replace your loop body by 您可以将循环体替换为

 System.out.print(rs.getString(1));
 System.out.print(", ");
 System.out.print(rs.getString(2))
 // ...

or a StringBuilder... 或StringBuilder ...

Most of the time could be spent printing the results. 大多数时间都可以花在打印结果上。

Try running the test without printing the data. 尝试运行测试而不打印数据。

You may put a printed timestamp after the "executeQuery" statement. 您可以在“ executeQuery”语句之后加上打印的时间戳。 And trying to figure out what spends more time in your program (retrive data from database or java instructions). 并尝试找出在程序上花费了更多时间的原因(从数据库或Java指令中检索数据)。

Theoretically, retrieving data from database would be the bottleneck if the data is large enough. 从理论上讲,如果数据足够大,则从数据库中检索数据将成为瓶颈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM