gpt4 book ai didi

java - MySQL、JDBC查询执行时间过长

转载 作者:太空宇宙 更新时间:2023-11-03 11:14:17 24 4
gpt4 key购买 nike

我的数据库查询在我当前的实现中变得太慢了。我需要从数据库中获取所有电影,对于这些电影中的每一部电影,我都需要从另一个表中获取它们的文件数据。所以对于每部电影我都在做另一个查询。对于每个候选条目,我需要与数据库中的每部电影进行比较。对大约 500 名候选人执行此操作是否需要 5-10 秒?

// get movies with all their versions
private ArrayList<Movie> getDatabaseMovies(Connection conn) throws Exception {
PreparedStatement getMoviesStmt = conn.prepareStatement("SELECT movieid, title FROM movies", Statement.RETURN_GENERATED_KEYS);
ArrayList<Movie> movies = new ArrayList<Movie>();
try {
ResultSet rs = getMoviesStmt.executeQuery();
while (rs.next()) {
Movie movie = new Movie(rs.getString(2), getDatabaseMovieFiles(conn, rs.getInt(1)));
movies.add(movie);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
getMoviesStmt.close();
}
return movies;
}

public ArrayList<MovieFile> getDatabaseMovieFiles(Connection conn, int movieID) throws Exception {
ArrayList<MovieFile> movieFiles = new ArrayList<MovieFile>();
PreparedStatement stmt = conn.prepareStatement("SELECT filename, size, hash, directory FROM file_video WHERE movieid = ?");
try {
stmt.setInt(1, movieID);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
MovieFile movieFile = new MovieFile(rs.getString(1), rs.getLong(2), rs.getBytes(3), rs.getString(4));
movieFiles.add(movieFile);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
stmt.close();
}

return movieFiles;
}

最佳答案

Should this be taking 5-10 seconds to execute for approximately 500 candidates?

可能不会。

有两种方法可以改善这一点:

  • 确保 file_videomovieid 列上有索引。

  • 使用 JOIN 将两个查询合并为一个查询。

你可能应该两者都做。

关于java - MySQL、JDBC查询执行时间过长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6248153/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com