简体   繁体   English

缓存数据库查询和结果集

[英]Cache database query and result set

I am creating a java desktop application. 我正在创建一个java桌面应用程序。

I am using mysql database as back end. 我使用mysql数据库作为后端。

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. 请检查您正在使用的mysql版本。 It seems Before MySQL 5.1.17, prepared statements do not use the query cache. 看来在MySQL 5.1.17之前,预备语句不使用查询缓存。 http://dev.mysql.com/doc/refman/5.1/en/query-cache-operation.html 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. 我认为替代方法是使用memcache,但我不知道它有多好。 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: 如果您想从MySQL服务器获得优化和快速响应,那么您需要将以下两个配置指令添加到MySQL服务器:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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