3
SELECT SQ_CACHE * FROM TABLE where ID=1

问题

  • 如果向表中添加新行,是否仍会保留缓存?
  • 如果更新以外的行ID=1仍然保持我的缓存?
  • 我知道如果同一行更新缓存丢失。
4

1 回答 1

4
  1. Read up on cache-invalidation (here's the first hit I found). MySQL (or any RDBMS) will invalidate (parts of) cached data whenever it needs to. When it actually does this should * not be of your concern; let MySQL (or any RDBMS) handle it. The actual process is (or can be) very complex; that's why this stuff is abstracted away and handled by the RDBMS for you. Same goes for actual storage of data on disk; do you care? Or do you let the RDBMS handle it for you? See? ;-)
  2. See 1.
  3. There you go. (And how is this a question? :P )

* Aside from some corner-cases or bugs maybe.

The "short" answer (note: specific to MySQL) would be: AFAIK MySQL caches on table level granularity; so any change on that table would invalidate all cached results for that table. Other than that, only deterministic queries will get cached, transactions might bypass/invalidate caches, the query cache doesn't support prepared statements, queries must be absolutely exactly be the same for the cache to kick in at all (so an extra space in a query would cause a cache miss) and subqueries aren't supported. And then there are still other factors involved. Maybe some things have changed in more recent versions but these are, some (not all!) of the factors that used to be involved. Correct me if I'm wrong.

于 2012-07-19T17:00:49.980 回答