MySQL SQL_NO_CACHE not working

I have a table (InnoDB) that gets inserted, updated and read frequently (usually in a burst of few milliseconds apart). I noticed that sometimes the SELECT statement that follows an INSERT/UPDATE would get stale data. I assume this was due to cache, but after putting SQL_NO_CACHE
in front of it doesn’t really do anything.

How do you make sure that the SELECT always wait until the previous INSERT/UPDATE finishes and not get the data from cache? Note that these statements are executed from separate requests (not within the same code execution).

Maybe I am misunderstanding what SQL_NO_CACHE actually does…


@Uday, the INSERT, SELECT and UPDATE statement looks like this:

INSERT myTable (id, startTime) VALUES(1234, 123456)

UPDATE myTable SET startTime = 123456 WHERE id = 1234


I tried using transactions with no luck.


I think this is actually a problem with INSERT, not UPDATE. The SELECT statement always tries to get the latest row sorted by time. But since INSERT does not do any table-level locking, it’s possible that SELECT will get old data. Is there a way to force table-level locking when doing INSERT?

Problem courtesy of: pixelfreak


The query cache isn’t the issue. Writes invalidate the cache.

MySQL gives priority to writes and with the default isolation level (REPEATABLE READ), your SELECT would have to wait for the UPDATE to finish.

INSERT can be treated differently if you have CONCURRENT INSERTS
enabled for MyISAM, also InnoDB uses record locking, so it doesn’t have to wait for inserts at the end of the table.

Could this be a race condition then? Are you sure your SELECT occurs after the UPDATE? Are you reading from a replicated server where perhaps the update hasn’t propagated yet?

If the issue is with the concurrent INSERT, you’ll want to disable CONCURRENT INSERT on MyISAM, or explicitly lock the table with LOCK TABLES during the INSERT. The solution is the same for InnoDB, explicitly lock the table on the INSERT with LOCK TABLES.

Solution courtesy of: Marcus Adams

稿源:Node.js Recipes (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 后端存储 » MySQL SQL_NO_CACHE not working

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录