Best practices for multithreaded processing of database records

前端 未结 5 1088
小蘑菇
小蘑菇 2020-12-31 05:11

I have a single process that queries a table for records where PROCESS_IND = \'N\', does some processing, and then updates the PROCESS_IND to \'Y\'

5条回答
  •  抹茶落季
    2020-12-31 06:09

    You should enable row level locking on the table with:

    CREATE TABLE mytable (...) LOCK DATAROWS
    

    Then you:

    • Begin the transaction
    • Select your row with FOR UPDATE option (which will lock it)
    • Do whatever you want.

    No other process can do anything to this row until the transaction ends.

    P. S. Some mention overhead problems that can result from using LOCK DATAROWS.

    Yes, there is overhead, though i'd hardly call it a problem for a table like this.

    But if you switch to DATAPAGES then you may lock only one row per PAGE (2k by default), and processes whose rows reside in one page will not be able to run concurrently.

    If we are talking of table with dozen of rows being locked at once, there hardly will be any noticeable performance drop.

    Process concurrency is of much more importance for design like that.

提交回复
热议问题