I have a Python program that executes a large MySQL statement. How do I tell MySQL to keep the cache even after the Python program terminates so that the next time I run the Python program the results will be cached?
Right now, every time I run the Python script it takes forever to retrieve the results even though I’m running the exact same query. I’ve set my query_cache_size and query_cache_limit to be greater than the result set already.
There is no option to instruct MySQL engine to keep some specific query result in cache, unfortunately. You need to implement one by yourself.
You could save your query result in a “cache” table (but not in a
TEMPORARYtable, because such tables are accessible from the creating thread only and are dropped when the connection is closed).Then you would set
TRIGGERSon the underlying tables, which would clear the cache when any such table is updated. Alternatively, you could simply add aTIMESTAMPto your cached data, and consider it obsolete after an arbitrary span of time. The latter option is cheaper, if you can afford getting slightly obsolete data from your cache.However, the solution could be that you need to optimize your query or your structure, in order to speed up the query.
[edit]
Actually, there is a (twisted) way to do that. You could set the
query_cache_typeserver option to 2 so that only queries using theSQL_CACHEkeyword are cached. This allows a very fine control over which queries are cached. Make yourquery_cache_sizelarge enough to hold every result of every query usingSQL_CACHE, and you are sure none will be overwritten. But then you must manage all caching yourself…