1

In the database, there are 2 rows that match the query condition. If we run two parallel queries, the first query locks both rows and returns one, while the second query returns null.

It would be desirable to have the ability to lock only one (returned) row to avoid such a situation.

const queryRunner : QueryRunner = this.connect.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
const identificationProcess = await queryRunner.manager
            .getRepository(IdentificationProcessEntity)
            .createQueryBuilder("identification_process")
            .orderBy("identification_process.updated_at", "ASC")
            .where("identification_process.status = :status", { status: IdentificationStatusEnum.Scanning })
            .useTransaction(true)
            .setLock("pessimistic_write")
            .setOnLocked("skip_locked")
            .take(1)
            .getOne()

I have tried changing the query parameters.

MariaDB - v10.8.4

SQL:

query: SELECT `identification_process`.`uid` AS `identification_process_uid`, `identification_process`.`user_uid` AS `identification
_process_user_uid`, `identification_process`.`status` AS `identification_process_status`, `identification_process`.`attempts` AS `id
entification_process_attempts`, `identification_process`.`expire_at` AS `identification_process_expire_at`, `identification_process`
.`created_at` AS `identification_process_created_at`, `identification_process`.`updated_at` AS `identification_process_updated_at`, 
`identification_process`.`procedure_uid` AS `identification_process_procedure_uid` FROM `identification_process` `identification_pro
cess` WHERE `identification_process`.`status` = ? ORDER BY `identification_process`.`updated_at` ASC LIMIT 1 FOR UPDATE SKIP LOCKED 
-- PARAMETERS: ["Scanning"]
4
  • Which MariaDB version? (10.6+ required) - mariadb.com/kb/en/select/#skip-locked. Can you show the SQL query JS is doing? A "limit 1" might be better as that's pushed down.
    – danblack
    Commented Sep 13, 2023 at 9:33
  • Supplemented the question Commented Sep 13, 2023 at 10:12
  • The locks are always on indexes. I suspect you haven't got a combined index for status,updated_at? or its not being used in the query explain {query}?
    – danblack
    Commented Sep 13, 2023 at 11:16
  • Thank you, adding an index for the status field helped! Commented Sep 13, 2023 at 12:59

1 Answer 1

1

Locks are always on indexes. So to ensure the minimal amount of locks are obtained for the query, the query needs to use an index in the optimal way for the least locks to be created.

While indexing is a big and important topic, that is highly recommended in support of optimal queries (and energy conservation), lets look at the query in the question.

There is a simple one table lookup, no joins, so look at the where criteria. Searching on status requires an index on status. After this is found, the results are sorted by updated_at. This forms the second part of the compound index.

As such the index in SQL is created with:

CREATE INDEX idx_status_updated_at ON identification_process (status, updated_at)

Or going by the TypeORM docs:

@Entity()
...
@Index(["status", "updated_at"])
...
export class identification_process {
3
  • Indexes really helped, thank you! But I have encountered another similar issue, when using TypeORM with relations: { subscribers: true } in the query and SQL with LEFT JOIN, the query still locks all the rows that match the condition despite the LIMIT 1. Commented Oct 3, 2023 at 4:53
  • Hmm. Depends on the query, ask a new question. EXPLAIN {query} shows what indexes are used and how the query was executed.
    – danblack
    Commented Oct 3, 2023 at 6:35
  • A new question about this issue - stackoverflow.com/questions/77221510/… Commented Oct 8, 2023 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.