I have a requirement recently, which is as follows.
There is a table with utmost 50000 rows, each of which has to be read, run a process depending on the data (which takes approx. 30-40 secs for each row.).
Since this is a time consuming task, and also cannot run on a single thread or process (The process ends abruptly after an hour or so, with no reason whatsoever), I have employed multiple processes.
It works as follows. There is a processcounter variable set at 30. The system calls the process 30 times with a time gap of 2 seconds at once. Each process reads 30 rows from the table (LIMIT 30), and updates a flag so that no other process reads the same. This then waits for 10 minutes until another batch call is made.
I employed c# lock() method to isolate each call and works fairly well.
But last day, the NW team added a LB into the hosting system. Now there are multiple server instances created when load is high. Also all these servers take part in the above mentioned tasks. There is no way but to lock the table at each process call.
I want to Lock the table, read 30 rows, update a flag, and then unlock the Table.
I tried IsolationLevel RepeatableRead and seems to work. I there a better way to Lock the table, read, update and Unlock ?
Any help would be greatly appreciated ! Thanks
2
Answers
You can do it using LOCK TABLES statement.
This statement can be used to acquire different types of locks on one or more tables, such as read locks, write locks or a combination of both.
LOCK TABLES explicitly acquires table locks for the current client session. Table locks can be acquired for base tables or views.
You must have the LOCK TABLES privilege and the SELECT privilege for each object to be locked.
The syntax goes like this :
As per the MySQL Documentation
— If you lock a table explicitly with LOCK TABLES, any tables used in triggers are also locked implicitly.
— If you lock a table explicitly with LOCK TABLES, any tables related by a foreign key constraint are opened and locked implicitly.
— UNLOCK TABLES explicitly releases any table locks held by the current session. LOCK TABLES implicitly releases any table locks held by the current session before acquiring new locks.
— Another use for UNLOCK TABLES is to release the global read lock acquired with the FLUSH TABLES WITH READ LOCK statement, which enables you to lock all tables in all databases.
You shouldn’t lock the table especially for this long running process. It’s going to be prone to errors.
You need
A queue holding an identifier for each row, run a job for each item in the queue. In case a job fails you can re-run it. Just go through the queue 1 by 1 so you don’t have a 40 hour process. You now have 50,000 40 second processes that you can retry in case one fails.
A replication database. Your replication DB should be read only on the tables you want. That way no one can mess with the table you’re writing to and you get high availability.
Here’s what a summary of the process would look like.