Update via a temp table

Limey

So I have a rather large table (150 million rows) that data scrub queries get run on nightly. Now these queries don't update a lot of records, but to get the records needed, that have to query that single table multiple times in sub queries, which takes some time.

So, would it be better for me to do a normal update statement, or would it be better if I put the few results I needed in a temp table, and then just did an update for those few rows, which would greatly reduce the locks during update.

I'm unsure how an update statement locks work when most of the time is spent querying. If it is going to only update 5 records, and runs for half and hour, will it release a record that it updated in the first minute, or does it wait till the end of the query?

Thanks

Marcel N.

You need to use (and look into) into the ROWLOCK table hint. You can use it with the update statement while updating in batches of 5000 rows of less. This will attempt to place row locks in the target table (or on index keys, if a covering index is present). If for some reason that fails, the lock will be escalated to a table lock.

From MSDN (as for reasons why lock escalation might occur):

When the Database Engine checks for possible escalations at every 1250 newly acquired locks, a lock escalation will occur if and only if a Transact-SQL statement has acquired at least 5000 locks on a single reference of a table. Lock escalation is triggered when a Transact-SQL statement acquires at least 5,000 locks on a single reference of a table. For example, lock escalation is not triggered if a statement acquires 3,000 locks in one index and 3,000 locks in another index of the same table. Similarly, lock escalation is not triggered if a statement has a self join on a table, and each reference to the table only acquires 3,000 locks in the table.

Actually, there's more to read in this last article. You should have a look at mixed lock type escalation section.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

mysql update table if record not in temp table

From Dev

sp_executesql to update a temp table

From Dev

Update temp table with results from WITH statement

From Dev

sp_executesql to update a temp table

From Dev

MYSQL - Update temp table with next date

From Dev

Update temp table number n+1 from another table

From Dev

Update data in a SQL Server temp table where the column names are unknown

From Dev

Mysql user can't update temp table (has all privileges)

From Dev

update SQL WHERE BOOKNO = BOOK number in temp table

From Dev

UPDATE table via join in SQL

From Dev

UPDATE - SELECT - MYSQL #1093 - You can't specify target table 'temp1' for update in FROM clause

From Dev

Complex query to update a temp table. Possibly derived table or correlated sub query?

From Dev

Why is there a big difference in latch waits for an update against a table variable vs temp table?

From Dev

Update table via join without key relation

From Dev

Update html table via servlet and jsp

From Dev

Update html table via servlet and jsp

From Dev

Update the first row of table via sql

From Dev

Temp table is not a known variable

From Dev

Select into temp table in PostgreSQL?

From Dev

Temp Table in SQL Server

From Dev

MySQL Temp table Insert

From Dev

MySQL Temp Table Location

From Dev

Collation conflict with temp table

From Dev

NHibernate JOIN to temp table

From Dev

IF statement from a temp table

From Dev

Insert into a Temp Table in a CTE

From Dev

Declaring Temp Table

From Dev

XML input into a temp table

From Dev

Inserting row to temp table