如何提高Java中的select查询性能?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
@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 aselect *
you bring a lot of fields that maybe you don't need and requires more memory一般来说,使用 JDBC 时,您可能会发现使用
您还可以通过优化查询(也许通过添加适当的索引)来缩短时间。使用
explain
可以帮助您了解查询将遇到的操作和成本。但是,在您的示例中,您只是执行一个没有谓词的简单选择,因此这些都不会帮助您。
In general terms when using JDBC you may see a performance increase by
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.
我认为您的代码在数据库访问方面没有任何需要改进的地方。对于数据库来说,选择所有行是一项微不足道的任务,基本上只需要一定的时间。使用 PreparedStatement 会给你一些改进,如果你反复相同/相似的查询,但不在您的测试场景中。
尽管您的 java 部分有一点开销,但这些开销来自分配/连接字符串对象。您可以将循环体替换为
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
or a StringBuilder...
大部分时间都花在打印结果上。
尝试运行测试而不打印数据。
Most of the time could be spent printing the results.
Try running the test without printing the data.
您可以在“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.