开发者

MySQL,JDBC query execution too long

I've got database querying that has become too slow with my current implementation. I need to get all movies from a database and for each of these movies i need their file data from another table. So for each movie i am doing another query. For each candidate entry i need to do a comparison to every movie in the database. Should this be taking 5-10 seconds to execute for approximately 500 candidates?

// 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;
开发者_JAVA百科}

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?

Probably not.

There are two ways to improve this:

  • Make sure that there is an index on the movieid column of file_video.

  • Combine the two queries into one by using a JOIN.

You probably should do both.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜