Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

⚙️ SET TRANSACTION - Configuring Transaction Behavior

🌟 Overview

The SET TRANSACTION command in SQL is a TCL statement used to define properties of a transaction, such as its isolation level or read/write mode, before operations begin. It allows customization of how transactions interact with concurrent operations, ensuring consistency and performance. SET TRANSACTION is key to the ACID model’s Isolation property.

In AI/ML, SET TRANSACTION is used to control concurrency in data pipelines, optimize queries for model training, and ensure reliable reads for experiments. For freshers, it’s an advanced interview topic, often explored in questions about transaction isolation and concurrency control.


📜 Syntax

SET TRANSACTION [READ ONLY | READ WRITE]
[ISOLATION LEVEL {READ UNCOMMITTED | READ COMMITTED | REPEATABLE READ | SERIALIZABLE}];
  • Basic Example:
    SET TRANSACTION READ ONLY;
    BEGIN;
    SELECT * FROM training_data;
    COMMIT;
  • With Isolation Level:
    SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
    BEGIN;
    UPDATE models SET status = 'active' WHERE model_id = 101;
    COMMIT;

💡 Use Cases in AI/ML

  • Concurrent Pipelines: Set SERIALIZABLE for ETL jobs to avoid conflicts (e.g., updating feature_table).
  • Read Consistency: Use READ ONLY for model evaluation queries (e.g., selecting from predictions).
  • Experiment Isolation: Apply REPEATABLE READ for stable experiment data (e.g., reading training_data).
  • Performance Tuning: Set READ COMMITTED for low-conflict ML tasks (e.g., logging to logs).
  • Data Validation: Ensure consistent reads with SERIALIZABLE for critical checks (e.g., verifying models).

🔑 Key Features

  • Isolation Levels: Controls visibility of concurrent changes (READ UNCOMMITTED, READ COMMITTED, etc.).
  • Read/Write Mode: Restricts transactions to read-only or allows modifications.
  • Transaction Scope: Applies settings for the entire transaction.
  • ACID Support: Enhances Isolation for consistent data access.

✅ Best Practices

  • Choose Appropriate Isolation: Use READ COMMITTED for most ML tasks; reserve SERIALIZABLE for high consistency.
  • Use READ ONLY: Apply for queries to reduce locking and improve performance.
  • Set Early: Issue SET TRANSACTION before BEGIN for clarity.
  • Understand Trade-offs: Higher isolation levels increase consistency but may reduce concurrency.

⚠️ Common Pitfalls

  • Overly Strict Isolation: SERIALIZABLE can cause deadlocks in busy ML pipelines.
  • Ignoring Defaults: Not setting isolation may use database defaults, leading to surprises.
  • Read-Only Misuse: Applying READ ONLY to write operations causes errors.
  • Performance Hits: High isolation levels slow down concurrent transactions.

📝 Additional Notes

  • Database Variations:
    • MySQL: Limited to READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ, SERIALIZABLE.
    • PostgreSQL: Supports all standard levels; defaults to READ COMMITTED.
    • SQL Server: Adds SNAPSHOT isolation; uses READ COMMITTED by default.
  • Scope: Settings apply only to the current transaction.
  • Alternatives: Use SET SESSION for broader settings in some databases.