I have a MySQL/Slave Setup everyday we are executing queries on slave machine for auditing . Tables on that DB has more than 50 million rows . If I a execute those queries on slave , slave is running behind the master . On this time If a Insert query executes in Master DB . This insert queries executes multiple times on slave DB . Example Scenario : Each time a user will signin we insert their details for auditing . Look at the below data it will be inserted multiple times on the table . No way a user to signin multiple times on same second . Why it happens and how can i solve this ?
| Kannan | 2012-04-28 12:27:57 |
| Kannan | 2012-04-28 12:27:57 |
| Kannan | 2012-04-28 12:27:57 |
| Kannan | 2012-04-28 12:27:57 |
| Kannan | 2012-04-28 12:27:57
If you need real time data, you really need to select from the master. Insert/Selects are usually not a good idea in a master/slave environment. If you are using statement based replication, the SELECT needs to run on the slave also. If the SELECT takes 10 seconds to run, your slave is going to be behind by 10 seconds.
Inserts are often very, very fast. The SELECT slows down the INSERT considerably because of the prep time required. For the most part, MySQL replicates 1 statement at a time. Until that statement is finished, replicating to the slave is going to fall behind.
While counterintuitive, it’s often better to SELECT the data, create the INSERT statement (i.e. in the application layer), then run the INSERT.