0

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;
}

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;
}
5
  • The idea of a prepared statement is that we prepare a statement once and reuse it with different values. Commented Jun 6, 2011 at 5:53
  • I don't want to sound overly harsh or flippant, but this is some really basic stuff. Maybe some sort of "introduction to relational databases" is in order? There are plenty of excellent free resources out there. I fear that even though you will get the correct answer here, you will still be missing a lot of the foundation you will eventually need. Commented Jun 6, 2011 at 6:19
  • I guess add 'patronizing', I also didn't want to sound patronizing. Commented Jun 6, 2011 at 6:20
  • @Dmitri, Based on my question what foundational material do you recommend I lookup in particular? Commented Jun 6, 2011 at 6:23
  • based on your question? I'd say anything that covers RDBMS theory, a solid SQL tutorial, and an intro book on relational design. There are lots and lots of threads on SO with great recommendations for all of those. Commented Jun 6, 2011 at 6:44

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I've got this: "SELECT M.movieid, M.title, MF.filename, MF.size, MF.hash, MF.directory FROM movies M, file_video MF WHERE M.movieid = MF.movieid" now i guess i just need to put some java logic around it to give me what i want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.