Search code examples
mysqlinnodb

Which result should I expect from a long sql query from a table that is updated very often?


I do have a database with a table with more than 300mi entries. There is near of 300 inserts per minute,

Very often We need to get statistical data. (count, sum avg). Each query takes from 10 to 30 seconds to be completed.

From the start of the query to its end, the table data increases more than 100 entries.

The numbers I get from the statistic query are related to the start of the query or to its end?

I'm using mysql and innodb


Solution

  • Regardless of your transaction isolation level (e.g. repeatable-read or read-committed), any single SQL query against InnoDB tables gets a sort of "snapshot" or a consistent view of the database.

    A SELECT query will not block concurrent inserts or updates, but those inserts or updates won't influence the result of the SELECT query. It will "see" the data as it existed at the start of the SQL query.

    If you use repeatable-read transaction isolation level, all queries in the transaction, including potentially several SELECT queries (at least non-locking queries), sees a consistent snapshot of the data, even if concurrent inserts are continuing.