Solved

Mysql locking question

Posted on 2007-12-02
6
2,469 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This guide whil teach how to setup live replication (database mirroring) on 2 servers for backup or other purposes. In our example situation we have this network schema (see atachment). We need to replicate EVERY executed SQL query on server 1 to…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

867 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

16 Experts available now in Live!

Get 1:1 Help Now