Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Mysql locking question

Posted on 2007-12-02
6
Medium Priority
?
2,474 Views
Last Modified: 2012-06-21
Hi Guys,

I'm using mysql 5.0.42 with InnoDB tables and I'm looking for the best locking methods....

I have a simple table like this:

CREATE TABLE `tblaccess` (
  `accessid` int(11) NOT NULL auto_increment,
  `stateid` int(11) default 0,
  PRIMARY KEY  (`accessid`),
  KEY `tblaccess_stateid_idx` (`stateid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

I have about 1.000.000 rows in the table.
There are about 30 clients which connects to the databease and issue the same query and select a simple row from the table, works with tha data, and after some minutes, it request another row to work with...

Initially the stateid of all rows are 0 = unprocessed, the processed rows should have a stateid=1 means processed.

the query could look like this:

CREATE DEFINER = 'root'@'localhost' PROCEDURE `sp_getaccessid`()
    NOT DETERMINISTIC
    CONTAINS SQL
    SQL SECURITY DEFINER
    COMMENT ''
begin
declare    _accessid int default 0;
start transaction;
      select _accessid into _accessid from tblaccess where stateid=0 order by accessid limit 1 for update;
      if _accessid >0 then # we ha a macthing row
            update tblaccess set stateid=1 where acceesid=_accessid;
      end if;
commit;
select _accessid;
end;

This metchod works but seems to lock the whole table instead of only one row....
And sometimes I got an erro in my client:
Deadlock found when trying to get lock; try restarting transaction

How to perform the same action with better performance and without deadlock???

I would like to select a row from a large table in the client and update is't state to processed...
I have 30 simulteanous clients and one row could be processed by only one client...
In other words every row could be processed by only one client...

How do I have to perform the locking???





CREATE DEFINER = 'root'@'localhost' PROCEDURE `sp_getaccessid`()
    NOT DETERMINISTIC
    CONTAINS SQL
    SQL SECURITY DEFINER
    COMMENT ''
begin
declare    _accessid int default 0;
start transaction;
	select _accessid into _accessid from tblaccess where stateid=0 order by accessid limit 1 for update;
	if _accessid >0 then # we ha a macthing row
		update tblaccess set stateid=1 where acceesid=_accessid;
	end if;
commit;
select _accessid;
end;

Open in new window

0
Comment
Question by:cosie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 3

Expert Comment

by:blckholehorizon
ID: 20417345
You should try using 'advisory locks' with MySQL.  The lock names could be the row identifier, and from within the application check for a lock before you try and modify a row.  This also would not actually lock your tables.
0
 

Author Comment

by:cosie
ID: 20421182
Check for a lock within the application????
I use VB6 and ODBC on client side...
Please explain what are you talking about a bit more in details...
0
 
LVL 3

Expert Comment

by:blckholehorizon
ID: 20424982
Step 1:  Read this web page.  http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html

Step 2: write some utility functions that call get_lock(name_here, timeout), is_lock_used, and release_lock(name_here).

Calling these functions is no different than calling a query.  They return a result just like a query.  They are a query.

In your code, before you need to do a transaction, check for a lock, acquire it if necessary, to your processing, and then release it.  If the lock is already being used, wait until its available.

locking could be one way to do it, but I would almost take a different approach to solving the problem.  Develop a FIFO Queue for items that need to be updated. Have multiple threads in the application.  All threads will share the FIFO Queue.  One thread will write to the front of the queue, adding to the list of transactions, and one thread will pop off the end of the queue the transaction to be done.  You wont have collisions then.

If you aren't experienced in writing threaded applications, then use locking.

I can't say which one will be faster unless its tested.
0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 

Author Comment

by:cosie
ID: 20426042
hi blckholehorizon,

Maybe you didn't understand my problem...
The FIFO quee is a bad idea, since using it only one transaction can run at the same time...
This would meen while one client is waiting for selecting a row from the large table all other clients have to wait for it... Anyway implementing this behaviour is easy usind a dummy update on a specific record at the start of the transaction... I've tested it, but the problem is that the query response time on the 2.5 million records table is about 500msec...
When 10-15 clients try to lock a new row at the same time this result 5-6 seconds wait times in the client which is too much...

I need a row lewel locking mechanism, which allows simultaneous querries (from different clients = sessions) perform locking different (and unique) rows in the SAME time...

I know how to use functions, of course...
The problem is how to put on locks paralell to the same table?











0
 
LVL 3

Expert Comment

by:blckholehorizon
ID: 20427581
Read more about advisory locks and you can effectively have row level locking at the application level.

InnoDB supports row level locking, http://dev.mysql.com/doc/refman/5.0/en/internal-locking.html

but the performance overhead of such row level locks may not make your application any faster.
0
 

Accepted Solution

by:
cosie earned 0 total points
ID: 20427754
hey blckholehorizon,

Thanks for the link,but I can also find and read mysql documentation. Which is not too informative...
I need a working sample, mysql documantation is a piece of sh***t and not too informative...
Anyway I can't see how could you implement row level locking with advisory locks...
Can you give me an idea what are you thinking on in details???
How would you implement the sample code: sp_getaccessid using advisory locking???

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question