What is the best way to update data in a table during use without locking the table?

I have a table in a SQL Server 2005 database that is used a lot. It contains product availability information. We receive updates every hour from our warehouse, and over the past few years we have launched a procedure that cuts the table and updates the information. It only takes a few seconds and still has not been a problem. We have many more people who use our systems, who request this information now, and as a result we see many timeouts due to blocking processes.

... So...

We explored our options and came up with the idea of ​​mitigating the problem.

  • We would have two tables. Table A (active) and Table B (inactive).
  • We would create a view pointing to the active table (table A).
  • All things that need this tabular information (4 objects) will now need to view the view.
  • The hourly procedure trims the inactive table, updates it with the latest information, and then updates the view, pointing to the inactive table, making it active.
  • This procedure will determine which table is active and basically switch the view between them.

What happened to this? Problems with a call in the middle of a request will cause problems? Could this work?

Thanks for your experience.

Additional Information

  • the subroutine is an SSIS package that displays many steps and ultimately trims / updates the table in question.

  • Lock processes are two other stored procedures that query this table.

+5
10

. SSIS .

, .

+6

, - , .

, . .

, , - .

with (nolock) SQL View. .

+2

, , , /. , .

, (, ), , , . (BatchProcessingEvent), , , .

$0,02,

+2

, .

Truncate , .

, .

, , , . , .

+1

, .

.

", "

, :

Table Products:
    ProductId       int
    QuantityOnHand  Int

QuantityOnHand .

, :

Table Prodcuts_WareHouse
    ProductId       int
    QuantityOnHand  Int

"" :

Table Prodcuts_Actions
    ProductId       int
    QuantityOnHand  Int
    Action          Char(1)

:

1.Truncate table Prodcuts_WareHouse

2.Truncate table Prodcuts_Actions

3. Prodcuts_WareHouse

4. Prodcuts_Actions :

:

INSERT INTO Prodcuts_Actions (ProductId, QuantityOnHand,Action)
SELECT     SRC.ProductId, SRC.QuantityOnHand, 'I' AS ACTION
FROM         Prodcuts_WareHouse AS SRC LEFT OUTER JOIN
                      Products AS DEST ON SRC.ProductId = DEST.ProductId
WHERE     (DEST.ProductId IS NULL)

INSERT INTO Prodcuts_Actions (ProductId, QuantityOnHand,Action)
SELECT     DEST.ProductId, DEST.QuantityOnHand, 'D' AS Action
FROM         Prodcuts_WareHouse AS SRC RIGHT OUTER JOIN
                      Products AS DEST ON SRC.ProductId = DEST.ProductId
WHERE     (SRC.ProductId IS NULL)

INSERT INTO Prodcuts_Actions (ProductId, QuantityOnHand,Action)
SELECT     SRC.ProductId, SRC.QuantityOnHand, 'U' AS Action
FROM         Prodcuts_WareHouse AS SRC INNER JOIN
                      Products AS DEST ON SRC.ProductId = DEST.ProductId AND SRC.QuantityOnHand <> DEST.QuantityOnHand

.

5. :

BEGIN TRANS

DELETE Products FROM Products INNER JOIN
Prodcuts_Actions ON Products.ProductId = Prodcuts_Actions.ProductId
WHERE     (Prodcuts_Actions.Action = 'D')

INSERT INTO Prodcuts (ProductId, QuantityOnHand)
SELECT ProductId, QuantityOnHand FROM Prodcuts_Actions WHERE Action ='I';

UPDATE Products SET QuantityOnHand = SRC.QuantityOnHand 
FROM         Products INNER JOIN
Prodcuts_Actions AS SRC ON Products.ProductId = SRC.ProductId
WHERE     (SRC.Action = 'U')

COMMIT TRAN

, , .

, .

+1

SQL Server, SQL Server.

, "Live" "" ( , ).

, "" , "LIVE" OUT "" IN, .

, ( Secondary).

, , / .

SQL Server . :

http://msdn.microsoft.com/en-us/library/ms345146(SQL.90).aspx

: -)

EDIT:

, , SQL Server Row Versioning.

http://msdn.microsoft.com/en-us/library/ms345124(SQL.90).aspx

+1

. , , , dev, . , SSIS, .

0

, . , , .

0

It might make sense to do some analysis of the processes that are being blocked, as they seem to be part of your landscape that has changed. To create the blocks you see, only one poorly written query is required. A ban on a poorly written query, maybe the table needs one or more coverage indexes to speed up these queries and get you back without processing existing code.

Hope this helps,

Bill

0
source

All Articles