The manual indicates that “SQL_NO_CACHE” prevents a query from being added to the cache. But it doesn’t say anything about using the cache, that I can see.
I’m trying to benchmark two versions of a query, but they’ve already been run without “SQL_NO_CACHE”. So I don’t know if my tests are using the query cache or not.
Does the “SQL_NO_CACHE” directive prevent a query from using the cache, or just from adding to it?
The MySQL query cache is indexed based on the full, literal text of the query, so there is functionally no difference: even if the directive were ignored, the results of a query with
SQL_NO_CACHEwould be stored in a different cache “slot” from the query withoutSQL_NO_CACHE. (Indeed, simply adding a space to the query somewhere, or changing “SELECT” to “select” — for instance — would also “bust” the query cache, making it run again. The MySQL query cache is not particularly sophisticated.)That being said, a quick test by running a query with
SQL_NO_CACHEand watching theQCACHE_QUERIES_IN_CACHEstatus confirms thatSQL_NO_CACHEdoes prevent variables from being stored in the cache, and a look at the source makes it look as though the presence ofSQL_NO_CACHEwill also keep MySQL from checking the cache for results.