开发者

Is an open MySQL index query result set a snapshot or does it update in real-time?

Suppose in some program using MySQL ODBC (actually it's Java using JDBC), we have a single thread running with an open MySQL transaction that does this:

  • A ResultSet is opened (but not yet iterated) via SELECT id FROM Table WHERE col = 'x', where MySQL has an index on the col column
  • A new row is inserted with col = 'x' (or, an existing row that previously had a different value is updated with col = 'x')
  • We now itera开发者_运维技巧te over the ResultSet

    Will the new (or updated) row appear in the result set?

    I.e., is an open MySQL index query result set a snapshot or does it update in real-time?

    What if there is no index on the col column? Does that change the answer?


    I think your result set will be the same as during the running query and doesn't depend on other process. Anyway you will get inconsistent data. For example, in php this is true.


    No - the result set is a snapshot in time. It's a consistency in read kind of thing.

    And no, indexes won't change that behavior.

    One logical reason for this is the potential that the result set will become invalid based on a change in the data used. If you were to SELECT qty FROM fruit WHERE name = "APPLES", followed by someone else running 'DELETE FROM fruit WHERE name = "APPLES"', what would happen to the result set before you finish reading it?


    The answer depends on how you have set the transaction isolation level, and in some cases does depend on whether an index is used. Refer to the explanation in the MySQL manual.

  • 0

    上一篇:

    下一篇:

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    最新问答

    问答排行榜