What kind of overhead should I expect when replacing a single complex query with multiple simple queries?
My goal is to improve the readability and portability of all SQL code so I will be favoring simple constructs and replacing database-specific extensions with ANSI SQL whenever possible.
For example:
- Imagine the client is invoking Dynamic SQL (as opposed to stored procedures)
- Scenario 1: Client invokes:
INSERT INTO employee SELECT name FROM user - Scenario 2: Client invokes:
Statement getNames = connection.createStatement();
try (ResultSet rs = getNames.executeQuery("SELECT name FROM user"))
{
while (rs.next())
{
String name = result.getString(1);
PreparedStatement prepared = connection.prepareStatement("INSERT INTO employee SET name = ?");
prepared.setString(1, name);
prepared.executeUpdate();
}
}
Scenario 1 is not a complex query, but for the sake of argument let's pretend it is. Scenario 2 achieves the same result using multiple (simpler) queries. What kind of overhead can I expect for Scenario 2 versus Scenario 1? Is it something I should worry about or is it negligible?
UPDATE: https://stackoverflow.com/a/14408631/14731 makes a good point. Decomposing queries by hand hard-codes an execution plan instead of allowing the database's optimizer to choose. That being said, it's still not clear whether the overhead is meaningful.