Should I be using SQL transactions, while reading records?

后端 未结 7 1668
离开以前
离开以前 2021-01-02 09:14

SQL transactions is used for insert, update, but should it be used for reading records?

相关标签:
7条回答
  • 2021-01-02 09:48

    Transactions are meant to avoid concurrency issues when one logical transaction actually maps to several SQL queries. For example, for a bank account, if you are transferring money from one account to another, you will 1st subtract the amount from account and then add it to other(or vice versa). But, if some error occurs in between your database would be in a invalid state (You may have subtracted the amount from one account but not added it to other). So, if you are reading all your data in one query, you dont need a transaction.

    0 讨论(0)
  • 2021-01-02 09:56

    If you are querying all the records in a single query, and pulling them back in one go, there is no need. Everything is wrapped up in an implicit transaction. That is to say, even if you get back one million records, and even if other processes are changing the records, you'll see what all one million records looked like at the same point in time.

    The only times you would really need a transaction (and, often, a specific locking hint) in a read only process are:
    - You read the records "piece-meal" and need nothing else to alter the values while you itterate though. [Such as a connected recordset in ADO that you then cursor through.]
    - You read some data, do some calculations, then read some related data, but on the assumption nothing changed in the mean time.


    In short, you need transactions when you want other processes to be stopped from interfering with your data between SQL statements.

    0 讨论(0)
  • 2021-01-02 09:58

    No, transactions are not generally needed to read data and it will slow down your data reads as well.

    I would suggest you read up on the term ATOMIC. This will help you understand what transactions are for.

    0 讨论(0)
  • 2021-01-02 09:58

    It's posssible to to do transactions but what is purpose of it?

    You can set the appropriate isolation level for an entire SQL Server session by using the SET TRANSACTION ISOLATION LEVEL statement.

    This is the syntax from SQL Server Books Online:

    SET TRANSACTION ISOLATION LEVEL 
        {
            READ COMMITTED 
            | READ UNCOMMITTED 
            | REPEATABLE READ 
            | SERIALIZABLE
        }
    

    Locking in Microsoft SQL Server.

    0 讨论(0)
  • 2021-01-02 10:00

    Transaction wrapping is not needed for pure reads.

    Within your SQL statement, Lock Hints should take care returning proper data to you (http://msdn.microsoft.com/en-us/library/aa213026%28SQL.80%29.aspx).

    On a server level, you can set Transaction Isolation levels (http://msdn.microsoft.com/en-us/library/ms173763.aspx).

    Edit

    Explaining pure reads

    If all your SQL statement has these kinds of reads then you do not need to wrap in a transaction

    SELECT Col1, Col2
    From Table1
        INNER JOIN Table2
            ON Table1.Id = Table2.Table1Id
    

    If you are reading results that can be affected by other transactions in parallel then you must wrap in a transaction. For eg:

    BEGIN TRANSACTION
    
    INSERT INTO AccountTransactions (Type, Amount) Values ('Credit', 43.21)
    UPDATE AccountSummary SET Balance = Balance + 43.21
    
    SELECT @Balance = Balance FROM AccountSummary
    
    COMMIT TRANSACTION
    

    Really, you are just returning the balance, but the entire monetary transaction has to work in two places.

    0 讨论(0)
  • 2021-01-02 10:03

    When you modified something in a transaction, you can use read statement to check if the operation takes effect, just before you commit.

    0 讨论(0)
提交回复
热议问题