How to update MySQL(MyISAM) table in parallel?
I have a parallel bash script with several threads. Each thread checks if record is exists then update value otherwise insert new row. Should I care about thread concurrency? Should I lock unlock tables or mysql takes care about it?
my update goes over the command line:
#get.sh script...
# set status Downloading
mysql -uroot -pmypass -ss -e "use filestatus; call changeStatus(\"$1/$2\", 1)"
rsync -ar rsync://user@server/$2/ $1/$2/
if [ $? -eq 0 ];
then
# set status OK
mysql -uroot -pmypass -ss -e "use filestatus; call changeStatus(\"$1/$2\", 0)"
else
# set status ERROR
mysql -uroot -pmypass -ss -e "use filestatus; call changeStatus(\"$1/$2\", 2)"
fi
I call severa开发者_如何学Pythonl get.sh with different parameters.
Thank you beforehand Arman.You could implement locking, but that's going to defeat the purpose of having multiple threads. MyISAM only supports table level locking, so only 1 thread will be able to modify the table at a time.
If you can, put a unique index or primary key on a field(s). The do an INSERT ... ON DUPLICATE KEY UPDATE ... MySql will then make sure the action is atomic.
What I usually do with multi-threading/processing, is have the first thread/process get a "list" of all the jobs that need to be perform, then create a thread/fork to handle each job or batch of jobs. The parent thread/process will assure the children aren't trying to do the same thing. I'm not sure if this will work in your case.
精彩评论