Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the point to enclose select statements in a transaction?

What's the point to enclose select statements in a transaction? I think select statements are just "GET" data from the database, they don't have chance to rollback something, because you just can't change the data. So, does that to say we never need put select statements in a transaction? Am I right?

Thanks.

like image 908
Just a learner Avatar asked Jun 23 '10 02:06

Just a learner


People also ask

Why is a SELECT statement important?

The SELECT statement is probably the most important SQL command. It's used to return results from our database(s) and no matter how easy that could sound, it could be really very complex.

What is the purpose of the SELECT statement give an example?

The SELECT TOP statement is used to limit the number of rows which returns the result of the query. For example, if want to retrieve only two rows from the table we can use the following query. Therefore, we can limit the result set of the query. In the following SQL examples, we will limit the result set of the query.

Should Selects be in transaction?

In a highly concurrent application it could (theoretically) happen that data you've read in the first select is modified before the other selects are executed. If that is a situation that could occur in your application you should use a transaction to wrap your selects.

Is SELECT statement considered as transaction?

A query is a single SQL statement that does Select, Update, Insert or Delete of rows. A transaction is a consecutive sequence of SQL statements (from the application viewpoint) that have the "ACID" properties: Atomicity: All statements or none are executed. Consistency: Data integrity is always maintained.


1 Answers

You're right: at the standard isolation level, read committed, you do not need to wrap select statements in transactions. Select statements will be protected from dirty reads whether you wrap them in a transaction or not.

connection 1:                          connection 2:                                         begin transaction                                        update user set name = 'Bill' where id = 1 select name from users where id = 1                                        rollback transaction 

The select statement will not read the rolled-back update: it doesn't matter that they are not wrapped in a transaction.

If you need repeatable reads, then wrapping selects in a default transaction doesn't help:

connection 1:                          connection 2:  begin transaction select name from users where id = 1                                        update user set name = 'Bill' where id = 1 select name from users where id = 1 commit transaction 

The begin and commit statements won't help here: the second select may read the old name, or it may read the new name.

However, if you run at a higher isolation level, like serializable or repeatable read, the group will be protected from non-repeatable reads:

connection 1:                          connection 2:  set transaction isolation level     repeatable read begin transaction select name from users where id = 1                                        update user set name = 'Bill' where id = 1 select name from users where id = 1              | commit transaction                               |                                                  |--> executed here 

In this scenario, the update will block until the first transaction is complete.

Higher isolation levels are rarely used because they lower the number of people that can work in the database at the same time. At the highest level, serializable, a reporting query halts any update activity.

like image 162
Andomar Avatar answered Oct 17 '22 21:10

Andomar