我的数据库表中有一个blob。如果我选择10行表(每行包含1个blob),则需要300毫秒。 但是,如果我选择15行,则需要20秒。我不明白发生了什么。选择的文件是1 MB的图像。
public void find() throws SQLException, IOException {
ResultSet rs = stm.executeQuery();
while (rs.next()) {
FileOutputStream output = new FileOutputStream(new File(
"C:\\Users\\test\\test.jpg"));
InputStream input = rs.getBinaryStream("photo");
byte[] buffer = new byte[8192];
int count = 0;
while ((count = input.read(buffer)) > 0) {
output.write(buffer, 0, count);
}
}
}
老实说,我不知道为什么这么小的差异占用了这么多时间。 非常感谢任何帮助。
编辑额外备注:
SQL:
String query = "SELECT * FROM contact c INNER JOIN contact_address ca ON c.id=ca.contact_id INNER JOIN groups_contact gc ON gc.contact_id=c.id INNER JOIN groups gr WHERE ca.country=? AND gr.name=? GROUP by c.id";
stm = conn.prepareStatement(query);
stm.setString(1, "NL");
stm.setString(2, "Friends");
stm.addBatch();
更新
(测量方法修改请见上文)
Selecting 10 rows: 239 ms.
Selecting 15 rows: 26378 ms.
Selecting 25 rows: 34888 ms.
Selecting 50 rows: 73267 ms.
Selecting 75 rows: 81885 ms.
Selecting 100 rows: 106528 ms.
表格的创作:
String createUserTable = "CREATE TABLE User (id INTEGER not NULL AUTO_INCREMENT, email VARCHAR(255), password VARCHAR(255), PRIMARY KEY (id))";
String createGroupTable = "CREATE TABLE Groups (id INTEGER not NULL AUTO_INCREMENT , name VARCHAR(255), user_id INTEGER not NULL, PRIMARY KEY (id), FOREIGN KEY (user_id) REFERENCES User(id))";
String createContactTable = "CREATE TABLE Contact (id INTEGER not NULL AUTO_INCREMENT , firstname VARCHAR(255), lastname VARCHAR(255), note VARCHAR(255), photo MEDIUMBLOB, user_id INTEGER, PRIMARY KEY (id), FOREIGN KEY (user_id) REFERENCES User(id))";
String createGroupContactTable = "CREATE TABLE Groups_Contact (id INTEGER not NULL AUTO_INCREMENT, contact_id INTEGER not NULL, group_id INTEGER not NULL, PRIMARY KEY (id), FOREIGN KEY (contact_id) REFERENCES Contact(id), FOREIGN KEY (group_id) REFERENCES Groups(id))";
String createContactAddressTable = "CREATE TABLE Contact_Address (id INTEGER not NULL AUTO_INCREMENT , street VARCHAR(255), number INTEGER, zipcode VARCHAR(255), city VARCHAR(255), country VARCHAR(255), addresstype VARCHAR(255), contact_id INTEGER not null, PRIMARY KEY (id), FOREIGN KEY (contact_id) REFERENCES Contact(id))";
String createContactPhoneTable = "CREATE TABLE Contact_Phone (id INTEGER not NULL AUTO_INCREMENT , type VARCHAR(255), number VARCHAR(255), contact_id INTEGER not null, PRIMARY KEY (id), FOREIGN KEY (contact_id) REFERENCES Contact(id))";
String createContactEmailTable = "CREATE TABLE Contact_Email (id INTEGER not NULL AUTO_INCREMENT , type VARCHAR(255), email VARCHAR(255), contact_id INTEGER not null, PRIMARY KEY (id), FOREIGN KEY (contact_id) REFERENCES Contact(id))";
答案 0 :(得分:0)
你确定它不是连接吗?某些连接可能非常慢,尤其是多个连接。通常我们必须运行单独的查询,因为连接可能会减慢查询速度。
你还有什么样的指数?