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

发布于 2024-11-16 19:12:31 字数 1610 浏览 3 评论 0原文

我正在使用 BerkeleyDB 数据库,并且正在执行需要 409 毫秒的 select 查询。如何提高select查询性能?

我正在使用以下代码:

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();
}
}

I am using BerkeleyDB Database and I am performing select query that require 409 ms. 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();
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

别把无礼当个性 2024-11-23 19:12:31

@Dhananjay Joshi 我的第一个建议是需要更改 select * 中的选择,并在执行 select * 时放置您真正需要的字段,您会带来很多字段也许你不需要并且需要更多内存

@Dhananjay Joshi my first recomendation it's that need the change the select from select * and put the fields that you really need when you do a select * you bring a lot of fields that maybe you don't need and requires more memory

镜花水月 2024-11-23 19:12:31

一般来说,使用 JDBC 时,您可能会发现使用

  • StoredProcedure 可以提高性能 - 这消除了数据库引擎
  • 使用PreparedStatement 进行的一些预处理 - 如果您使用的是多次查询。

您还可以通过优化查询(也许通过添加适当的索引)来缩短时间。使用 explain 可以帮助您了解查询将遇到的操作和成本。

但是,在您的示例中,您只是执行一个没有谓词的简单选择,因此这些都不会帮助您。

In general terms when using JDBC you may see a performance increase by

  • using a StoredProcedure - this elimentates some pre-processing by the database engine
  • using a PreparedStatement - this can elimenate some pre-processing if you are using a query multiple times.

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.

However, in your example you are just executing a simple select with no predicate, so none of these will help you.

牛↙奶布丁 2024-11-23 19:12:31

我认为您的代码在数据库访问方面没有任何需要改进的地方。对于数据库来说,选择所有行是一项微不足道的任务,基本上只需要一定的时间。使用 PreparedStatement 会给你一些改进,如果你反复相同/相似的查询,但不在您的测试场景中。

尽管您的 java 部分有一点开销,但这些开销来自分配/连接字符串对象。您可以将循环体替换为

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

StringBuilder...

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.

You have a little overhead in your java part though which comes from allocating/concatenating string objects. 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...

尽揽少女心 2024-11-23 19:12:31

大部分时间都花在打印结果上。

尝试运行测试而不打印数据。

Most of the time could be spent printing the results.

Try running the test without printing the data.

浪漫人生路 2024-11-23 19:12:31

您可以在“executeQuery”语句之后放置打印的时间戳。并试图找出程序中哪些部分花费了更多时间(从数据库或 java 指令检索数据)。

理论上,如果数据足够大,从数据库检索数据将成为瓶颈。

You may put a printed timestamp after the "executeQuery" statement. And trying to figure out what spends more time in your program (retrive data from database or java instructions).

Theoretically, retrieving data from database would be the bottleneck if the data is large enough.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文