MySQL SELECT FOR UPDATE is locking whole table - mysql

MySQL Version 5.7.16
Process 1:
START TRANSACTION;
SELECT * from statistic_activity WHERE activity_id = 1 FOR UPDATE;
Process 2:
START TRANSACTION;
INSERT INTO `statistic_activity` (`activity_id`) values (2678597);
If Process 1 SELECT statement returns results, Process 2 is not blocked (as you will expect)
But If Process 1 returns empty set (no rows exists with activity_id = 1) then whole table is locked and all INSERTS are blocked until Process 1 transaction ends.
Is this expected behavior ?

Related

Mysql gap locking repetable read not working

I have table routes with 3 records with route_id is 1,2,3.
I began run transaction 1
start transaction;
select * from routes where route_id>0 LOCK IN SHARE MODE;
do sleep(5);
delete from routes where route_id=1;
select * from routes where route_id>0;
rollback ;
When trans 1 is executing, I had run tran 2
start transaction;
delete from routes where route_id=2;
rollback ;
And as the result, I saw 2 different result for 2 same select query (same transaction).
So why gap lock can not lock record with route_id > 0? Its violate repetable read.
Isolation Level is repetable read

Benchmark for a rowlock in mysql

I am trying to benchmark a sql transaction with a select..for update statement which uses an exclusive lock on the row and then insert a row into another table as shown below.
START TRANSACTION;
SELECT CurrentSize
FROM testtable
WHERE id = {id} FOR UPDATE;
-- update current size in testtable
UPDATE testtable
SET currentsize = currentsize + 1
WHERE id = {id} ;
-- insert into a different table
insert into testtable2 values(1,2);
COMMIT;
I am getting 2K tps for the above transaction and I am assuming each transaction takes 0.5ms to complete so giving me 2K tps .
Is it even possible to scale the system beyond this point? If yes is there any implementation that I could try and use.
I am using a 16x.large machine of AWS RDS Aurora MySQL.

MySQL (stored procedure) update rows status column then select updated rows Limit 100

Writing QUERY.
First Update 100 rows then pull same 100 records.
I have multiple processes working all are looking same database I don't want duplication because of very fast & heavy transections, some time it get duplicated because of multiple processes pull same records, trying to prevent duplication issue.
First I want to update 100 row's column - (status) from 0 to 1 so other process will not see these records then I want to pull same updated records.
CREATE DEFINER=`devdba`#`%` PROCEDURE `getQueueRows`()
BEGIN
SET autocommit = 0;
SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE;
START TRANSACTION;
UPDATE Queue
SET status = 1
WHERE status = 0
ORDER BY id ASC
LIMIT 100;
SELECT * FROM Queue
where
status = 1 AND
UPDATED < now();
COMMIT;
END
Please advise.

Is it possible to create a Lost Update with MySQL Workbench

I want to create a Lost Update with MySQL Workbench. Therefore, I have 2 connections to my database and 2 transactions. I also changed the transaction isolation level to read uncommitted but transaction A uses the current data when the update statement starts. It never uses the data from the first select statement and with select ... for update the transaction b is blocked.
Transaction A (starts first):
Start transaction;
SELECT * FROM table;
Select sleep(10); -- <- Transaction B executes in this 10 seconds
UPDATE table SET Number = Number + 10 WHERE FirstName = "Name1";
COMMIT;
Transaction B:
Start transaction;
UPDATE table SET Number = Number - 5 WHERE FirstName = "Name1";
COMMIT;
Is it possible to create this failure with MySQL Workbench. What´s wrong with my code?
Thanks for your help
The update in A work with data after the sleep is executed. Select before does nothing in the transaction.

Misunderstanding rollback in MySQL

I not familiar with mysql but i just think about trouble if i using START TRANSACTION and ROLLBACK for 2 client or more then i have a error in one time, for example:
AT ONE TIME
Client 1:
START TRANSACTION -> insert query 1 -> insert query 2 - insert query 3
Client 2:
START TRANSACTION -> insert Query 4 -> error and rollback
if client 2 rollback (i just think) query 4 rollback and query 1 too, correct (I just assumed)?
i can't checkout that, how we can check this?
UPDATE :
IF that SITUATION:
AT ONE TIME, ID is PRIMARY (ai)
Client 1:
START TRANSACTION -> insert tbl1 (ID : 1) -> error and rollback
Client 2:
START TRANSACTION -> insert tbl1 (ID : 2) -> insert tbl2 (ID : 2)
(i just think) this transaction from client 1 is Pasted and ID 1 if lost? and other transaction continue using id 3,4,5 etc..., true or not?