The world's most popular open source database
A consistent read means that InnoDB uses
multi-versioning to present to a query a snapshot of the
database at a point in time. The query sees the changes made by
transactions that committed before that point of time, and no
changes made by later or uncommitted transactions. The exception
to this rule is that the query sees the changes made by earlier
statements within the same transaction. This exception causes
the following anomaly: if you update some rows in a table, a
SELECT will see the latest
version of the updated rows, but it might also see older
versions of any rows. If other users simultaneously update the
same table, the anomaly means that you may see the table in a
state that never existed in the database.
If you are running with the default
REPEATABLE READ isolation
level, all consistent reads within the same transaction read the
snapshot established by the first such read in that transaction.
You can get a fresher snapshot for your queries by committing
the current transaction and after that issuing new queries.
Consistent read is the default mode in which
InnoDB processes
SELECT statements in
READ COMMITTED and
REPEATABLE READ isolation
levels. A consistent read does not set any locks on the tables
it accesses, and therefore other users are free to modify those
tables at the same time a consistent read is being performed on
the table.
Note that consistent read does not work over
DROP TABLE and over
ALTER TABLE. Consistent read does
not work over DROP TABLE because
MySQL cannot use a table that has been dropped and
InnoDB destroys the table. Consistent read
does not work over ALTER TABLE
because ALTER TABLE works by
making a temporary copy of the original table and deleting the
original table when the temporary copy is built. When you
reissue a consistent read within a transaction, rows in the new
table are not visible because those rows did not exist when the
transaction's snapshot was taken.
InnoDB uses a consistent read for select in
clauses like INSERT INTO ... SELECT and
UPDATE ... (SELECT) that do not specify
FOR UPDATE or IN SHARE
MODE if the
innodb_locks_unsafe_for_binlog option is set
and the isolation level of the transaction is not set to
serializable. Thus, no locks are set on rows read from selected
table. Otherwise, InnoDB uses stronger locks
and the SELECT part acts like
READ COMMITTED, where each
consistent read, even within the same transaction, sets and
reads its own fresh snapshot.


User Comments
Add your own comment.