SNAPSHOT

SQL Server में Transactions और Error Handling

Miriam Antona

Software Engineer

SNAPSHOT

  • हर संशोधन tempDB डेटाबेस में स्टोर होता है
  • केवल SNAPSHOT ट्रांजैक्शन शुरू होने से पहले की committed changes और अपनी changes दिखती हैं
  • SNAPSHOT ट्रांजैक्शन शुरू होने के बाद अन्य ट्रांजैक्शनों की changes नहीं दिखतीं
  • Reads, writes को ब्लॉक नहीं करते और writes, reads को नहीं ब्लॉक करते
  • Update conflicts हो सकते हैं
ALTER DATABASE myDatabaseName SET ALLOW_SNAPSHOT_ISOLATION ON;
SET TRANSACTION ISOLATION LEVEL SNAPSHOT
SQL Server में Transactions और Error Handling

SNAPSHOT - isolation level तुलना

Dirty reads Non-repeatable reads Phantom reads
READ UNCOMMITTED yes yes yes
READ COMMITTED no yes yes
REPEATABLE READ no no yes
SERIALIZABLE no no no
SNAPSHOT no no no
SQL Server में Transactions और Error Handling

SNAPSHOT - उदाहरण

Transaction1

SET TRANSACTION ISOLATION LEVEL SNAPSHOT

BEGIN TRAN
    SELECT * FROM accounts;
|account_id|account_number      |...| current_balance|
|----------|--------------------|---|----------------|
|1         |55555555551234567890|...| 25000,00       |
|2         |55555555559876543210|...| 200,00         |
|...       |...                 |...| ...            |
|15        |55555555551234567890|...| 25000,00       |

Transaction2

...

...

BEGIN TRAN
    INSERT INTO accounts
    VALUES (11111111111111111111, 1, 25000);

    UPDATE accounts 
    SET current_balance = 30000 WHERE account_id = 1;

    SELECT * FROM accounts;
COMMIT TRAN

यह ब्लॉक नहीं होता!

SQL Server में Transactions और Error Handling

SNAPSHOT - उदाहरण

Transaction1

SET TRANSACTION ISOLATION LEVEL SNAPSHOT

BEGIN TRAN
    SELECT * FROM accounts;
    SELECT * FROM accounts;
|account_id|account_number      |...| current_balance|
|----------|--------------------|---|----------------|
|1         |55555555551234567890|...| 25000,00       |
|2         |55555555559876543210|...| 200,00         |
|...       |...                 |...| ...            |
|15        |55555555551234567890|...| 25000,00       |

Transaction2

...

...

BEGIN TRAN
    INSERT INTO accounts
    VALUES (11111111111111111111, 1, 25000);

    UPDATE accounts 
    SET current_balance = 30000 WHERE account_id = 1;

    SELECT * FROM accounts;
COMMIT TRAN

यह ब्लॉक नहीं होता!

SQL Server में Transactions और Error Handling

SNAPSHOT - सारांश

Pros:

  • अच्छी डेटा consistency: बिना blocking के dirty, non-repeatable और phantom reads रोकता है

Cons:

  • tempDB बढ़ती है

कब इस्तेमाल करें?:

  • जब data consistency ज़रूरी हो और blocks नहीं चाहिए
SQL Server में Transactions और Error Handling

READ COMMITTED SNAPSHOT

  • READ COMMITTED के व्यवहार को बदलता है
ALTER DATABASE myDatabaseName SET READ_COMMITTED_SNAPSHOT {ON|OFF};
  • डिफ़ॉल्ट रूप से OFF

  • ON करने के लिए:

ALTER DATABASE myDatabaseName SET ALLOW_SNAPSHOT_ISOLATION ON;
  • ON होने पर, हर READ COMMITTED स्टेटमेंट केवल उसी स्टेटमेंट के शुरू होने से पहले की committed changes देख सकता है
  • Update conflicts नहीं होते
SQL Server में Transactions और Error Handling

READ COMMITTED SNAPSHOT - उदाहरण

Transaction1

 SET TRANSACTION ISOLATION LEVEL READ COMMITTED
 BEGIN TRAN 
   UPDATE accounts
   SET current_balance = 30000
   WHERE account_id = 1;

COMMIT TRAN

Transaction2

SET TRANSACTION ISOLATION LEVEL READ COMMITTED
BEGIN TRAN

    SELECT current_balance FROM accounts 
    WHERE account_id = 1;
| current_balance |
|-----------------|
| 35000,00        |
SQL Server में Transactions और Error Handling

READ COMMITTED SNAPSHOT - उदाहरण

Transaction1

 SET TRANSACTION ISOLATION LEVEL READ COMMITTED
 BEGIN TRAN 
   UPDATE accounts
   SET current_balance = 30000
   WHERE account_id = 1;

COMMIT TRAN

Transaction2

SET TRANSACTION ISOLATION LEVEL READ COMMITTED
BEGIN TRAN

    SELECT current_balance FROM accounts 
    WHERE account_id = 1;
    SELECT current_balance FROM accounts 
    WHERE account_id = 1;
| current_balance |
|-----------------|
| 30000,00        |
SQL Server में Transactions और Error Handling

WITH (NOLOCK)

  • Uncommitted data पढ़ने के लिए इस्तेमाल होता है
  • READ UNCOMMITTED पूरी connection पर लागू, WITH (NOLOCK) किसी specific table पर लागू
  • किसी भी isolation level में उपयोग करें जब आपको सिर्फ कुछ tables से uncommitted data पढ़ना हो
SQL Server में Transactions और Error Handling

WITH (NOLOCK) - उदाहरण

मूल balance, account 5 = $35,000

Transaction1

BEGIN TRAN
  UPDATE accounts
  SET current_balance = 30000
  WHERE account_id = 5;

Transaction2

...

...

...

SELECT current_balance
FROM accounts WITH (NOLOCK) 
WHERE account_id = 5;

| current_balance |
|-----------------|
| 30000,00        |
SQL Server में Transactions और Error Handling

अभ्यास करते हैं!

SQL Server में Transactions और Error Handling

Preparing Video For Download...