简体   繁体   中英

MySQL Transaction/Commit Query

Just a quick question which I need to ask, my MYSQL table is using MYISAM storage engine and looking at some previous questions they say queries under this engine are automatically committed (No transactions allowed only auto-commit).

Now, does that mean, if I do the following query:

UPDATE `ExampleTable` SET `ExampleField` += '50' WHERE ...; 
UPDATE `ExampleTable2` SET `ExampleField2` -= '50' WHERE ...;

It will either succeed (and autocommit/update both) or fail and update neither?

Or is my definition of query incorrect and MYISAM will only autocommit one command at once? -So I cannot do the above query reliably under MYISAM?

(Bonus question) If so, I have heard of INNODB engine which supports transactions. could I use that instead? How much speed would I be losing in return for reliable queries?

Thanks for any help.

Yes, you are right if you do this:

UPDATE `ExampleTable` SET `ExampleField` += '50' WHERE ...; 
UPDATE `ExampleTable2` SET `ExampleField2` -= '50' WHERE ...;

it is something like:

START TRANSACTION;
UPDATE `ExampleTable` SET `ExampleField` += '50' WHERE ...; 
COMMIT;

START TRANSACTION;
UPDATE `ExampleTable2` SET `ExampleField2` -= '50' WHERE ...;
COMMIT;

So the first query may be executed succesfully and the second one may return some error.

If you need to execute both queries or nothing you should choose some engine that support transactions (The most popular is InnoDB) and send this:

START TRANSACTION;
UPDATE `ExampleTable` SET `ExampleField` += '50' WHERE ...; 
UPDATE `ExampleTable2` SET `ExampleField2` -= '50' WHERE ...;
COMMIT;

Or another option is join both queries into one and in this case you can use MyISAM engine and you will not lost any data.

Yes, there are two queries here, so they will be executed as two atomic operations. MyISAM will commit every query immediately, here is the explanation :

 In transactional terms, MyISAM tables effectively always operate in 
 autocommit = 1 mode.

As for performance, INNODB will be slower for write operations , however you should test your use case, it may be not a considerable difference. There is many improvements in 5.6 version, also there is some tuning that you can do to improve performance .

Basically what i'm asking: Is the code above one query which MYISAM will autocommit, or is that SQL treated as two queries?

In MyISAM, the code you show is treated as two queries. Any concurrent client could see the data changed in progress. If you want transaction isolation so that no concurrent thread can see the data until you commit, then you need to use InnoDB.

I always recommend InnoDB as a default choice over MyISAM. InnoDB has been the default storage engine since MySQL 5.5 (circa 2010).

There are a few cases where MyISAM still wins, but they are fewer and fewer. InnoDB when tuned well performs better than MyISAM. Even this blog from 2007 shows benchmark results that InnoDB is on par with, or faster than, MyISAM under most workloads. InnoDB has continued to be improved since then, while MyISAM has been stagnant and is gradually being phased out .

Performance is important, but to me, not corrupting your data is even more important. MyISAM is susceptible to losing data in a crash. InnoDB has automatic crash recovery. MyISAM also fails to support atomic changes. For example, if you run an UPDATE that takes 5 seconds, and 2.5 seconds into it you kill the query, about half of your rows have been changed and the rest have not. With InnoDB, this will never happen.

PS The operators += and -= are not supported in MySQL (neither are they supported in any other SQL database I know of). So strictly speaking, neither of the queries you show would do anything anyway, except return an error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM