I have a complex select query and a huge table.
I’m running this select statement, meanwhile an Update statement arrives and tries to update the table.
IMHO – update requires an exclusive lock – so the update statement will have to wait till the select command is finished.
-
Am I right ?
-
what can I do in order to: execute the complex
select, and also let theupdatecommand run (currently I don’t care about dirty data)
Yes – to a degree.
How long a
SELECTholds on to a shared lock is depending on the isolation level of the transaction:READ UNCOMMITTED– no shared lock is acquired at all –UPDATEis not blockedREAD COMMITTED– shared lock is acquired just for the duration of reading the data –UPDATEmight be blocked for a very short period of timeREPEATABLE READandSERIALIZABLE– shared lock is acquired and held on to until the end of the transaction –UPDATEis blocked until theSELECTtransaction endsTechnically, the
UPDATEstatement first gets anUPDATElock – which is compatible with a shared lock (as used by theSELECT) – for the duration of the time while it’s reading the current values of the rows to be updated.Once that’s done, the
Updatelock is escalated to an exclusive lock for the new data to be written to the table.