mariadb cache1

http://www.percona.com/blog/2006/07/27/mysql-query-cache/

MySQL Query Cache

July 27, 2006 by Peter Zaitsev 74 Comments

Tweet

inShare1

PREVIOUS POST

NEXT POST

MySQL has a great feature called “Query Cache” which is quite helpful for MySQL Performance optimization tasks but there are number of things you need to know.

First let me clarify what MySQL Query Cache is – I’ve seen number of people being confused, thinking MySQL Query Cache is the same as Oracle Query Cache – meaning cache where execution plans are cached. MySQL Query Cache is not. It does not cache the plan but full result sets. This means it is much more efficient as query which required processing millions of rows now can be instantly summoned from query cache. It also means query has to be exactly the same and deterministic, so hit rate would generally be less. In any case it is completely different.

Query cache is great for certain applications, typically simple applications deployed on limited scale or applications dealing with small data sets. For example I’m using Query Cache on server which runs this blog. Updates are rather rare so per-table granularity is not the problem, I have only one server and number of queries is small so cache duplication is not the problem. Finally I do not want to hack wordpress to support eaccelerator cache or memcached. Well honestly speaking if performance would be problem I should have started with full page caching rather than MySQL level caching but it is other story.

Lets talk a bit about features and limitations of Query Cache:

Transparent Caching – Caching is fully transparent to the application, and what is very important it does not change semantics of the queries – you always get actual query results. Really there are some chevats – if you’re not using query_cache_wlock_invalidate=ON locking table for write would not invalidate query cache so you can get results even
if table is locked and is being prepared to be updated. So if you’re
using query cache in default configuration you can’t assume locking
table for write will mean no one will be able to read it – results still
can come from query cache unless you enable
query_cache_wlock_invalidate=ON.

Caching full queries only – Meaning it does not work for subselects, inline views, parts of the UNION. This is also common missunderstanding.

Works on packet level
– This is one of the reason for previous item. Query cache catches
network packets as they sent from client to the server, which means it
can serve responses very fast doing no extra conversion or processing.

Works before parsing
– One more reason for high performance is Query Cache performs query
lookup in the cache before query parsing, so if result is served from
query cache, query parsing step is saved.

Queries must be absolutely the same
As no parsing is done before lookup queries are not normalized (would
require parsing) before cache lookup, so they have to match byte by
byte for cache hit to happen. This means if you would place dynamic
comments in the query, have extra space or use different case – these
would be different queries for query cache.

Only SELECT queries are cached
SHOW commands or stored procedure calls are not, even if stored
procedure would simply preform select to retrieve data from table.

Avoid comment (and space) in the start of the query
– Query Cache does simple optimization to check if query can be cached.
As I mentioned only SELECT queries are cached – so it looks at first
letter of the query and if it is “S” it proceeds with query lookup in
cache if not – skips it.

Does not support prepared statements and cursors
Query Cache works with query text and want full result set at once. In
prepared statements there is query with placeholders and additional
parameter values which would need extra care – it is not implemented.
Cursors get data in chunks so it is even harder to implement.

Might not work with transactions
– Different transactions may see different states of the database,
depending on the updates they have performed and even depending on
snapshot they are working on. If you’re using statements outside of
transaction you have best chance for them to be cached.

Query must be deterministic
– Query might provide same result no matter how many times it is run,
if data remains the same. So if query works with current data, uses
non-deterministic functions such as UUID(), RAND(), CONNECTION_ID() etc
it will not be cached.

Table level granularity in invalidation
– If table gets modification all queries derived from this table are
invalidated at once. Most of them quite likely would not have change
their result set but MySQL has no way to identify which one of them
would so it gets rid of all of them. This is one of the main features
which limits query cache effectiveness – if you have high write
application such as forums, query cache efficiency might be pretty low
due to this. There is also way to set minimal TTL or anything like it
which is allowed by other caching systems. Also note – all queries are
removed from cache on table modifications – if there are a lot of
queries being cached this might reduce update speed a bit.

Fragmentation over time – Over time Query Cache might get fragmented, which reduces performance. This can be seen as large value of Qcache_free_blocks relatively to Qcache_free_memory. FLUSH QUERY CACHE
command can be used for query cache defragmentation but it may block
query cache for rather long time for large query caches, which might be
unsuitable for online applications.

Limited amount of usable memory
– Queries are constantly being invalidated from query cache by table
updates, this means number of queries in cache and memory used can’t
grow forever even if your have very large amount of different queries
being run. Of course in some cases you have tables which are never
modified which would flood query cahe but it unusual. So you might want
to set query cache to certain value and watch Qcache_free_memory and Qcache_lowmem_prunes
– If you’re not getting much of lowmem_prunes and free_memory stays
high you can reduce query_cache appropriately. Otherwise you might wish
to increase it and see if efficiency increases.

Demand operating mode
If you just enable qury cache it will operate in “Cache everything”
mode. In certain caches you might want to cache only some of the
queries – in this case you can set query_cache_type to “DEMAND” and use only SQL_CACHE hint for queries which you want to have cached – such as SELECT SQL_CACHE col from foo where id=5.
If you run in default mode you can also use SQL_NO_CACHE to block
caching for certain queries, which you know do not need to be cached.

Counting query cache efficiency There are few ways you can look at query_cache efficiency. First looking at number of your selects – Com_select and see how many of them are cached. Query Cache efficiency would be Qcache_hits/(Com_select+Qcache_hits).
As you can see we have to add Qcache_hits to Com_select to get total
number of queries as if query cache hit happens Com_select is not
incremented. But if you have just 20% Cache hit rate does it mean
it is not worth it ? Not really it depends on which queries are cached,
as well as overhead query cache provides. One portion of query cache
overhead is of course inserts so you can see how much of inserted
queries are used: Qcache_hits/Qcache_inserts Other portion of overhead comes from modification statements which you can calculate by (Com_insert+Com_delete+Com_update+Com_replace)/Qcache_hits
.
These are some numbers you can play with but it is hard to tell what
is good or bad as a lot depends on statement complexity as well as how
much work is saved by query cache.

Now lets speak a bit about Query Cache configuration and mainance. MySQL Manual is pretty good on this: Query Cache Query Cache Status Query Cache Configuration

I would just mention couple of points – as protection from one query wiping your all query cache option query_cache_limit
was implemented which limits result set which can be stored in query
cache. If you need larger queries to be cached you might increase it,
if you most important queries are smaller you can decrease it. The
other one is Qcache_lowmem_prunes – This one is used to
identify if you have enough memory for query cache. Note however due to
fragmentation lowmem_prunes can be triggered even if there is some
free space, just badly fragmented.

Looking at performance I’ve
seen query cache offering about double performance for simple queries
with select done by primary key, obviously there is no upper boundary –
Very complex queries producing small result set will be offering best
speed up.

So when it is good idea to use query cache ?

Third party application – You can’t change how it works with MySQL to add caching but you can enable query cache so it works faster.

Low load applications
– If you’re building application which is not designed for extreme
load, like many personal application query cache might be all you need.
Especially if it is mostly read only scenario.

Why Look for alternatives ?

There are few reasons why Query Cache might be not cache for your application:

It caches queries
Application objects might need several queries to compose so it is
efficient to cache whole objects rather than individual queries.

No control on invalidation
Table granularity is often too bad. With other caches you may
implement version based or timeout based invalidation which can offer
much better hit ratio for certain application.

It is not that fast Query Cache is fast compared to running the queries but it is still not as fast as specially designed systems such as memcached or local shared memory.

It can’t retrieve multiple objects at the same time
You have to query cache object by object which adds latency, there is
no way you can request all objects you need to be retrieved at the same
time (again memcached has it)

It is not distributed
If you have 10 slaves and use query cache on all of them cache content
will likely be the same, so you have multiple copies of the same data in
cache effectively wasting memory. Distirbuted caching systems can
effectively use memory on multiple systems so there is no duplication.

Memcached
is probably the most popular distributed caching system and it works
great. I should write an article comparing performance of various
caching systems some time.

时间: 2024-12-30 07:48:25

mariadb cache1的相关文章

MariaDB(MySQL)创建、删除、选择及数据类型使用详解

一.MariaDB简介(MySQL简介略过) MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可 MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品.在存储引擎方面,使用XtraDB(英语:XtraDB)来代替MySQL的InnoDB. MariaDB由MySQL的创始人Michael Widenius(英语:Michael Widenius)主导开发,他早前曾以10亿美元的价格,将自己创建的公司MySQL A

安装MariaDB phpMyAdmin

1 安装设置MariaDB5.5 [[email protected] ~]# yum -y install mariadb-server [[email protected] ~]# vi /etc/my.cnf [mysqld] character-set-server=utf8 [[email protected] ~]# systemctl enable  mariadb.service[[email protected] ~]# systemctl start  mariadb.ser

mysql(mariadb)

========== yum install mariadb-server mysqladmin -uroot -predhat password westos #修改本地mysql root密码 mysqladmin -uroot -predhat -h 192.168.0.188 password westos #修改远程192.168.0.188 mysql服务器 root密码 mysql_secure_installation #第一次安装mysql以后通过这条命令可以对mysql进行设

mariadb 实用功能2 delete from table returning ... 数据恢复

做为一名dba,每天都是和数据大交到,每天也都会从后台来执行一些sql,原则应该是在前端来操作, 但是总是有很多很多的特殊情况.每次执行sql的时候都需要做备份,防止误操作. 看看mariadb的一个小功能吧,又是超级贴心的,我是非常喜欢的.删除数据的时候,可以把删除 显示出来.不多说了,看操作,敲起来!! MariaDB [test]> select version(); +-----------------+ | version()       | +-----------------+ |

Centos7+Mariadb集群-主从配置介绍

近期一直在恶补Linux相关的知识,主要是就是学Linux下的基本日常应用服务器的配置及优化,今天我们主要介绍一下在Centos7下安装及配置Mysql 集群,说到集群,其实就是为了提高服务的高可用性.对于高可用的相关服务今天不是主要介绍内容,今天主要介绍MYSQL的主从配置.对在Linux下的其他服务的介绍及服务高可用负载均衡我们将会在后期的文章中介绍.开始今天的介绍:Centos7+Mysql主从配置. 环境介绍: Hostname:A-S IP:192.168.5.21 Role:Mysq

Mariadb的基础应用

Mariadb概述: MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可.开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,因此社区采用分支的方式来避开这个风险. MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品.在存储引擎方面,10.0.9版起使用XtraDB(名称代号为Aria)来代替MySQL的InnoDB. MariaDB由MySQL的创始人麦克尔·维德纽斯主导开发

mariadb通用二进制格式安装

一.MariaDB安装介绍: 对于通用二进制格式的包,我们只需要解压缩后就能够使用数据库,听起来很容易,但必须要注意一些问题.二进制 格式的程序包是已经编译好的二进制程序,所以里边有很多脚本都是在固定的路径下执行的,所以安装过程中mysql这个路径必须安装在/usr/local目录下,并且目录名称必须叫mysql.如果不这样设置,一些脚本无法运行,更别提能成功安装了. Mariadb通用二进制格式相关文件说明 当获取到Mariadb通用二进制格式文件,解压后查看其文件如下:     [[emai

MariaDB数据库介绍之四、Galera Cluster

一.Galera Cluster Galera是一个MySQL(也支持MariaDB,Percona)的同步多主集群软件,Galera集群的复制功能基于Galeralibrary实现,为了让MySQL与Galera library通讯,特别针对MySQL开发了wsrep API. 二.Galera  Cluster的功能和优势 功能: multi-master,即所有节点可以同时读写数据库 自动的节点成员控制,失效节点自动被清除 新节点加入数据自动复制 并行复制,行级 优势: 因为是multi-

MySQL/MariaDB数据库基于SSL实现主从复制

前言 备份数据库是生产环境中的首要任务,有时候不得不通过网络进行数据库的复制,由于MySQL/MariaDB的主从复制是明文传送的,如果在生产环境中跨网络传送,数据的安全性就无法完全保证,为了解决这一问题,我们需要一种安全的方式进行传送,即基于SSL加密进行数据传输. 部署配置 实验拓扑 实验环境 系统环境:CentOS6.6 数据库版本:mariadb-5.5.36 #注意:主从服务器数据库版本须一致:主从服务器时间须同步 #此实验从服务器只做一组为例 配置主从复制 安装mariadb [[e