sql - Update Query with select and row lock -
update mytable set node_index=0 id in ( select id mytable rownum<=10 , procs_dt null order cret_dt,prty desc)
i got query part of previous answer question. how lock rows in select query make sure no other thread on writes update.
it's not clear me have problem seem think have.
updating row in table inherently places row-level lock on row. no other session can update row until release lock ending transaction committing or rolling back. once complete transaction, other sessions free overwrite update. there no need or benefit locking rows in select
.
if trying write application avoid lost updates (i.e. want avoid session updating same rows did after committed transaction without showing other user updated data first), application need implement sort of additional locking. efficient implement optimistic locking using sort of last_update_timestamp
column in table. assuming added column table if doesn't have one, select last_update_timestamp
whenever queried data present user , specify last_update_timestamp
in update
. if update
updated 1 row, know no 1 had changed data since queried it. if update
updated 0 rows, know data had changed since had queried , application need take appropriate action (i.e. re-querying data, re-presenting user, asking user whether keep of uncommitted changes had made, etc.).
your query have problem, however. select
statement not doing think (or intend) do. query gets arbitrary 10 rows mytable
procs_dt
null , orders arbitrary 10 rows.
select id mytable rownum<=10 , procs_dt null order cret_dt,prty desc
assuming want "top 10" results, need order by
in subquery before applying rownum
predicate, i.e.
select id (select * mytable procs_dt null order cret_dt, prty desc) rownum<=10
or use analytic function, i.e.
select id (select m.*, rank() on (order cret_dt, prty desc) rnk mytable m procs_dt null) rnk<=10
Comments
Post a Comment