如何加快获取图像和文本的 jdbc sql 查询
How to speed up jdbc sql query which fetches image and text
我正在从数据库中获取图像和文本,图像类型是 longblob 当我在 SQL 查询中包含图像时,显示结果需要很长时间,我不知道出了什么问题,但是当我删除 SQL 查询结果中的照片时,用时不到一秒钟。连接正常,运行良好,问题是速度,请帮助我?
外部连接 class:
public class jconnect_registry {
public static Connection connectDb2() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn2 =
DriverManager.getConnection("jdbc:mysql://localhost:3306/registry", "root", "root");
return conn2;
}
catch (ClassNotFoundException | SQLException e) {
JOptionPane.showMessageDialog(null, "Connection problem ");
return null;
}
}
}
Main class 我用来调用到数据库的连接的地方:
// connect to database
Connection conn2 = jconnect_registry.connectDb2();
// Fetch data(text and image) from mysql
private void FindStudentInfo() {
String sql7;
Image image;
Image newImg ;
ImageIcon ic;
byte[] imagedata ;
try{
sql7 = "select Registration_No,Names,Faculty,Department,photo as 'profile' from students where Registration_No = '" + this.srchregno_txtfld.getText() + "'";
ps25 = conn2.prepareStatement(sql7);
rs25 = ps25.executeQuery();
//set image to image jlabel
if(rs25.next()) {
String add1 = rs25.getString("Registration_No");
this.Regno_jlabel.setText(add1);
String add2 = rs25.getString("Names");
this.FirstName_jlabel.setText(add2);
String add3 = rs25.getString("Faculty");
this.fclty_jlabel.setText(add3);
String add4 = rs25.getString("Department");
this.dpt_jlabel.setText(add4);
imagedata = rs25.getBytes("profile");
ic = new ImageIcon(imagedata);
image = ic.getImage();
newImg = image.getScaledInstance(this.img_jlabel1.getWidth(), this.img_jlabel1.getHeight(), 40);
ic = new ImageIcon(newImg);
this.img_jlabel1.setIcon(ic);
}}
好吧,首先让我说,在关系数据库中存储(大)图像是没有意义的,你不应该这样做。无论如何,您似乎正在处理现有系统并且无法更改它。
您遇到的问题是,与所有其他数据相比,图像数据相当大。因此,如果将此图像添加到查询中,很明显查询会慢很多,因为必须通过网络收集和发送数据。你只有一个 where 子句,所以这里没有太多需要优化的地方。您唯一可以做的就是在列 Registration_No 上设置索引或减小查询的大小。
做两个查询。第一个,使用 "fast" 数据,如字符串、数字等。然后,第二个查询使用 "slow" 数据,图像,将在单独的线程中完成。
用户将看到一个包含所有数据的对话框和一个保留的 space 图像,稍后会出现。你可以放点进度动画之类的。
这与您在互联网上浏览时看到的内容大致相同...
我得到的最好的答案是在 where 子句中使用主键作为索引。
where子句定义了SQL语句的查找条件,属于索引的核心功能域:快速查找数据。虽然 where 子句对性能有巨大的影响,但它经常被粗心大意地措辞,以至于数据库不得不扫描索引的很大一部分。结果:写得不好的 where 子句是慢查询的首要因素。
因此我的代码无法在微秒内获取图像,因为我重新制定了一个 sql 查询,如下所示:
sql7 = "select Registration_No,Names,Faculty,Department,photo as 'profile' from students where Id ='"+jLabelId.getText()+"' and Registration_No = '" + this.srchregno_txtfld.getText() + "'";
我正在从数据库中获取图像和文本,图像类型是 longblob 当我在 SQL 查询中包含图像时,显示结果需要很长时间,我不知道出了什么问题,但是当我删除 SQL 查询结果中的照片时,用时不到一秒钟。连接正常,运行良好,问题是速度,请帮助我?
外部连接 class:
public class jconnect_registry {
public static Connection connectDb2() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn2 =
DriverManager.getConnection("jdbc:mysql://localhost:3306/registry", "root", "root");
return conn2;
}
catch (ClassNotFoundException | SQLException e) {
JOptionPane.showMessageDialog(null, "Connection problem ");
return null;
}
}
}
Main class 我用来调用到数据库的连接的地方:
// connect to database
Connection conn2 = jconnect_registry.connectDb2();
// Fetch data(text and image) from mysql
private void FindStudentInfo() {
String sql7;
Image image;
Image newImg ;
ImageIcon ic;
byte[] imagedata ;
try{
sql7 = "select Registration_No,Names,Faculty,Department,photo as 'profile' from students where Registration_No = '" + this.srchregno_txtfld.getText() + "'";
ps25 = conn2.prepareStatement(sql7);
rs25 = ps25.executeQuery();
//set image to image jlabel
if(rs25.next()) {
String add1 = rs25.getString("Registration_No");
this.Regno_jlabel.setText(add1);
String add2 = rs25.getString("Names");
this.FirstName_jlabel.setText(add2);
String add3 = rs25.getString("Faculty");
this.fclty_jlabel.setText(add3);
String add4 = rs25.getString("Department");
this.dpt_jlabel.setText(add4);
imagedata = rs25.getBytes("profile");
ic = new ImageIcon(imagedata);
image = ic.getImage();
newImg = image.getScaledInstance(this.img_jlabel1.getWidth(), this.img_jlabel1.getHeight(), 40);
ic = new ImageIcon(newImg);
this.img_jlabel1.setIcon(ic);
}}
好吧,首先让我说,在关系数据库中存储(大)图像是没有意义的,你不应该这样做。无论如何,您似乎正在处理现有系统并且无法更改它。
您遇到的问题是,与所有其他数据相比,图像数据相当大。因此,如果将此图像添加到查询中,很明显查询会慢很多,因为必须通过网络收集和发送数据。你只有一个 where 子句,所以这里没有太多需要优化的地方。您唯一可以做的就是在列 Registration_No 上设置索引或减小查询的大小。
做两个查询。第一个,使用 "fast" 数据,如字符串、数字等。然后,第二个查询使用 "slow" 数据,图像,将在单独的线程中完成。
用户将看到一个包含所有数据的对话框和一个保留的 space 图像,稍后会出现。你可以放点进度动画之类的。
这与您在互联网上浏览时看到的内容大致相同...
我得到的最好的答案是在 where 子句中使用主键作为索引。 where子句定义了SQL语句的查找条件,属于索引的核心功能域:快速查找数据。虽然 where 子句对性能有巨大的影响,但它经常被粗心大意地措辞,以至于数据库不得不扫描索引的很大一部分。结果:写得不好的 where 子句是慢查询的首要因素。
因此我的代码无法在微秒内获取图像,因为我重新制定了一个 sql 查询,如下所示:
sql7 = "select Registration_No,Names,Faculty,Department,photo as 'profile' from students where Id ='"+jLabelId.getText()+"' and Registration_No = '" + this.srchregno_txtfld.getText() + "'";