可以看到query cache是开启了的。
mysql> show status like '%com_select%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Com_select | 5 |
+---------------+-------+
1 row in set (0.00 sec)
执行查询,对比统计信息的变化.执行查询之后:
mysql> select id,account from account where id= '19922730';
+----------+-----------+
| id | account |
+----------+-----------+
| 19922730 | 694595671 |
+----------+-----------+
1 row in set (0.00 sec)
mysql> show status like '%com_select%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Com_select | 6 |
+---------------+-------+
1 row in set (0.00 sec)
select的统计变成了6.发现Com_select增加了
第二次在运行该查询 :
mysql> select id,account from account where id= '19922730';
+----------+-----------+
| id | account |
+----------+-----------+
| 19922730 | 694595671 |
+----------+-----------+
1 row in set (0.00 sec)
mysql> show status like '%com_select%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Com_select | 6 |
+---------------+-------+
1 row in set (0.00 sec)
可以看到com_select并没有起变化,这是因为第二次使用了QC,并没有parse,execution,explan等操作,没有进行统计。
同样我们来看看QC相关的状态变化(第一次执行之前):
执行之前:
mysql> show status like '%QC%';
+-------------------------+----------+
| Variable_name | Value |
+-------------------------+----------+
| Qcache_free_blocks | 1 |
| Qcache_free_memory | 16762264 |
| Qcache_hits | 30 |
|
Qcache_inserts | 6 |
| Qcache_lowmem_prunes | 0 |
| Qcache_not_cached | 52826 |
|
Qcache_queries_in_cache | 5 |
| Qcache_total_blocks | 13 |
+-------------------------+----------+
8 rows in set (0.00 sec)
执行之后:
mysql> select min(id) from account;
+---------+
| min(id) |
+---------+
| 1 |
+---------+
1 row in set (0.01 sec)
mysql> show status like '%QC%';
+-------------------------+----------+
| Variable_name | Value |
+-------------------------+----------+
| Qcache_free_blocks | 1 |
| Qcache_free_memory | 16761240 |
|
Qcache_hits | 30 |
|
Qcache_inserts | 7 |
| Qcache_lowmem_prunes | 0 |
| Qcache_not_cached | 52827 |
|
Qcache_queries_in_cache | 6 |
| Qcache_total_blocks | 15 |
+-------------------------+----------+
8 rows in set (0.00 sec)
可以看到在使用了QC之后,第一次会导致该query插入QueryCache.统计参数Qcache_inserts +1/Qcache_queries_in_cache+1.
第二次执行:
mysql> select min(id) from account;
+---------+
| min(id) |
+---------+
| 1 |
+---------+
1 row in set (0.00 sec)
mysql> show status like '%QC%';
+-------------------------+----------+
| Variable_name | Value |
+-------------------------+----------+
| Qcache_free_blocks | 1 |
| Qcache_free_memory | 16761240 |
|
Qcache_hits | 31 |
| Qcache_inserts | 7 |
| Qcache_lowmem_prunes | 0 |
| Qcache_not_cached | 52868 |
| Qcache_queries_in_cache | 6 |
| Qcache_total_blocks | 15 |
+-------------------------+----------+
8 rows in set (0.00 sec)
可以看到第二次命中,所以Qcache_hits +1.Qcache_inserts 和Qcache_queries_in_cache 并没有增加.
通过profile来跟踪发生的变化:
检查profiling是否打开
mysql> show variables like 'profiling';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| profiling | OFF |
+---------------+-------+
1 row in set (0.00 sec)
默认没有打开,打开profiling:
mysql> set session profiling=1;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'profiling';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| profiling | ON |
+---------------+-------+
1 row in set (0.00 sec)
mysql> select id,account from account order by account limit 1;