Reputation: 6494
I'm having a performance issue with MySQL (v5.5) while running a local Java application using mysql-connector v5.1.30
I have 2 tables called A and B. I run something like this:
PreparedStatement stmtA = conn.prepareStatement("select x, y, z, u, v, w from A");
PreparedStatement stmtB = conn.prepareStatement("select val from B where something = ?");
ResultSet result = stmtA.executeQuery();
while ( resultA.next() ){
// do something with result row
if (some condition) { // true approx. 80% of the time
ResultSet resultB = stmtB.executeQuery(some_value_from_resultA);
// do something with result row
resultB.close();
}
}
stmtA.close();
The problem is that this takes ~12 minutes to complete every time. Here's a few relevant bits of information:
mysqld
process is using 95-99% CPU for the 12 minute durationAside from the fact that the CPU usage of the mysql process is crazy, the reason I think there is a problem is because the application has additional functionality that runs an almost identical method against a different database table. This table has a similar number of columns to table A (also using MYISAM) but one big difference is that there is no table B here. So it works the same as above but without having to query a second table while iterating the rows of the first table.
This other table has 4,500 rows and the iteration takes less than 1 second.
So it would appear that the nested query of table B is causing the problem, but I'm not sure. I have limited experience with MySQL. If you need any more info, please ask.
Upvotes: 0
Views: 279
Reputation: 393821
You might want to pre-load or cache the results of the queries you run on table B. Since table B doesn't have many records, you can afford to load it into a map.
You have two options :
something
has many unique values and your B queries search for many of them.something
values in your B query.Adding an index on table B would also help.
Upvotes: 1