简体   繁体   中英

Cache database query and result set

I am creating a java desktop application.

I am using mysql database as back end.

In my application some query was get executed more than one time, so i want to cache the query result and use it for the next time.

For caching i try prepared statement but i don't get any positive result.

Is any other way to cache the query and query result set ?

Please check the version of mysql that you are using. It seems Before MySQL 5.1.17, prepared statements do not use the query cache. http://dev.mysql.com/doc/refman/5.1/en/query-cache-operation.html

It also mentions some conditions under which query cache will not work. I think the alternative to that is to use memcache, but I don't know how good it is. But one thing for sure all these caching methods donot work well if you have frequent updates. You don't really have much control on the cache I guess, it gets invalidated when ever there is an update on actual table(to avoid dirty reads) So I suppose yours is not so frequently updated table while in production. And if that is the case you can very well think of using application level caching with ofcourse cache invalidation service/criteria in place.

If you want to get optimized and speedy response from your MySQL server then you need to add following two configurations directive to your MySQL server:

query_cache_size=SIZE
The amount of memory (SIZE) allocated for caching query results. The default value is 0, which disables the query cache.

Now setup cache size 16Mb:
    mysql> SET GLOBAL query_cache_size = 16777216;

OR

Append config directives as follows into my.cnf file:

query_cache_size = 268435456
query_cache_type=1
query_cache_limit=1048576

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM