Solved

Mysql locking question

Posted on 2007-12-02
6
2,465 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
  • 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now