Thursday, June 22, 2023

Deadlock

Örnek
Şöyle yaparız
-- Transaction A
BEGIN;
UPDATE Orders SET Quantity = Quantity - 1 WHERE OrderID = 1;
-- Now Transaction A needs to update Customers
UPDATE Customers SET TotalOrders = TotalOrders + 1 WHERE CustomerID = 1;
COMMIT;

-- Transaction B
BEGIN;
UPDATE Customers SET TotalOrders = TotalOrders - 1 WHERE CustomerID = 1;
-- Now Transaction B needs to update Orders
UPDATE Orders SET Quantity = Quantity + 1 WHERE OrderID = 1;
COMMIT;
Açıklaması şöyle
In this example, if Transaction A and Transaction B are executed concurrently and the timing is such that Transaction A locks the Orders table and Transaction B locks the Customers table before either has a chance to commit, a deadlock will occur.

CREATE NONCLUSTERED INDEX

Örnek Şöyle  yaparız CREATE NONCLUSTERED INDEX IDX_MYINDEX ON Orders(OrderDate, ProductID);