D
D
diiimonn2015-11-02 13:10:04
MySQL
diiimonn, 2015-11-02 13:10:04

How to properly block rows?

Greetings!
In general: clients come to the server and take the top line from a certain table according to the time sorting, leaving 1 in the locked field after themselves.
The problem appeared with the increase in load, clients began to receive the same lines.
How can you prevent concurrent retrieval of rows?
I've only heard of blocking, I have a hard time understanding how it works.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alex Ivanov, 2015-11-02
@diiimonn

In theory, this is how it should work. I don’t know what syntax you use, so I only give commands:

LOCK TABLES my_table WRITE;
SELECT * FROM my_table;
UNLOCK TABLES;

O
Optimus, 2015-11-02
Pyan @marrk2

I had the same problem, it works like this for me:

// Заблокировать и читать
    mysqli_query($link," LOCK TABLES `".$source_table."` WRITE ");
    mysqli_query($link," SELECT `data` FROM `".$source_table."`   LIMIT 1 ");

// Удалить только что прочитанное и разблокировать
    mysqli_query($link," DELETE FROM `".$source_table."` WHERE `id`=".$lastid."");
    mysqli_query($link," UNLOCK TABLES ");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question