您可以执行两次相同的查询并检查他们的个人资料,例如
set global query_cache_size = 16777216;
set profiling = 1;
两次执行您的查询
select * from sbtest.sbtest1 limit 100;
select * from sbtest.sbtest1 limit 100;
然后检查配置文件,您将看到第二个查询比第一个查询花费更少的时间
mysql> show profiles;
+----------+------------+----------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+----------------------------------------+
| 7 | 0.20415325 | set global query_cache_size = 16777216 |
| 8 | 0.26780725 | select * from sbtest.sbtest2 limit 100 |
| 9 | 0.00007350 | select * from sbtest.sbtest2 limit 100 |
+----------+------------+----------------------------------------+
使用“显示查询 ID 的配置文件”与 query_ID=8(第一个查询)和 query_ID=9(第二个查询)进行比较
mysql> show profile for query 8;
+--------------------------------+----------+
| Status | Duration |
+--------------------------------+----------+
| starting | 0.000023 |
| Waiting for query cache lock | 0.000004 |
| checking query cache for query | 0.032402 |
| checking permissions | 0.000088 |
| Opening tables | 0.000035 |
| System lock | 0.000014 |
| Waiting for query cache lock | 0.049689 |
| init | 0.075707 |
| optimizing | 0.000015 |
| statistics | 0.000021 |
| preparing | 0.000012 |
| executing | 0.000004 |
| Sending data | 0.108332 |
| Waiting for query cache lock | 0.000013 |
| Sending data | 0.000581 |
| end | 0.000012 |
| query end | 0.000062 |
| closing tables | 0.000020 |
| freeing items | 0.000013 |
| Waiting for query cache lock | 0.000003 |
| freeing items | 0.000741 |
| Waiting for query cache lock | 0.000006 |
| freeing items | 0.000002 |
| storing result in query cache | 0.000005 |
| logging slow query | 0.000003 |
| cleaning up | 0.000004 |
+--------------------------------+----------+
26 rows in set (0.49 sec)
可以看到开表、关表等一长串,说明这个查询需要从硬盘查询数据,第二个是
mysql> show profile for query 9;
+--------------------------------+----------+
| Status | Duration |
+--------------------------------+----------+
| starting | 0.000023 |
| Waiting for query cache lock | 0.000004 |
| checking query cache for query | 0.000008 |
| checking privileges on cached | 0.000003 |
| checking permissions | 0.000010 |
| sending cached result to clien | 0.000019 |
| logging slow query | 0.000003 |
| cleaning up | 0.000003 |
+--------------------------------+----------+
8 rows in set (0.00 sec)
证明查询被缓存