views:

65

answers:

2

Hi

In my case I have a Mysql Table with 2,976,977 records. This table has indexes on two columns, and I used these two columns while updating and inserting records from multiple clients. The problem is that it is taking too much time for updating or inserting the values. Is any Idea to speed up this?

+2  A: 

Indexes may not make inserting and updating faster - they could make them slower because the indexes need to be updated.

Andy West
Thanks,Long time back it was very slow and then I have created Indexes by referring Google for that table it gets improved but now it getting slow day by day as record counts are increased.
Palani
You may want to run ANALYZE TABLE to update statistics if your instance has been running for a while. http://dev.mysql.com/doc/refman/5.0/en/analyze-table.html
Andy West
Yes, indexes do slow dml down. But seriously, doing 20000 updates should not be a big deal.
Roland Bouman
@Roland Bouman - I tend to agree. ~3M rows isn't that many, either. Seems like it must be something else.
Andy West
@Roland Bouman: I am doing the update and insert by using JDBC transaction concepts.
Palani
A: 

From the comments, i get the impression it is mainly doing the 20000 update statments that is taking too long. Are you running the updates in autocommit mode (default)? if so, then you can improve performance a lot by doing:

START TRANSACTION;
...update statements go here...
COMMIT;

please note that if one of the updates does not succeed due to a constraint violation, you cannot COMMIT. And your data will not be stored permanently until you do COMMIT.

In java, you could do something like:

java.sql.Connection conn = DriverManager.getConnection(connectString, userName, passWord);
//explicit transaction by disabling autocommit.
conn.setAutocommmit(false);
java.sql.Statement stmt = conn.createStatement();

//add all your statements to the batch, f.e. in a loop
for (...) {
    stmt.addBatch(sql);
}

int results[] = stmt.executeBatch();
...check the results here...

//store results permanently in db
conn.commit();
Roland Bouman
I am doing the update and insert by using JDBC transaction concepts.
Palani