Just how effective is the Query Cache on performance? If you are not comfortable reading the MySQL code you can cheat very easily with the SHOW PROFILE command. This demonstration will show you at a high level the relative impact without and with the Query Cache. First let us confirm the Query Cache is not used.
mysql> SHOW GLOBAL VARIABLES LIKE 'query_cache%';
+------------------------------+---------+
| Variable_name | Value |
+------------------------------+---------+
| query_cache_limit | 1048576 |
| query_cache_min_res_unit | 4096 |
| query_cache_size | 0 |
| query_cache_type | ON |
| query_cache_wlock_invalidate | OFF |
+------------------------------+---------+
5 rows in set (0.00 sec)
We now enable profiling.
mysql> SET PROFILING=1;
We run our sample query.
mysql> select name,id,competitions from olympic_games where host_city='Sydney'\G
We can then enable the query cache and re-run the query.
mysql> SET GLOBAL query_cache_size=1024*1024*16;
mysql> select name,id,competitions from olympic_games where host_city='Sydney'\G
mysql> select name,id,competitions from olympic_games where host_city='Sydney'\G
NOTE: We run the query twice after enabling the cache, the first time, the query is cached, the second time it is retrieved from the cache. Now let us look at the profiling information.
mysql> SHOW PROFILES;
+----------+------------+-------------------------------------------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+-------------------------------------------------------------------------+
| 1 | 0.00096100 | select name,id,competitions from olympic_games where host_city='Sydney' |
| 2 | 0.00029700 | SET GLOBAL query_cache_size=1024*1024*16 |
| 3 | 0.00837900 | select name,id,competitions from olympic_games where host_city='Sydney' |
| 4 | 0.00009500 | select name,id,competitions from olympic_games where host_city='Sydney' |
+----------+------------+-------------------------------------------------------------------------+
mysql> SHOW PROFILE SOURCE FOR QUERY 3;
+--------------------------------+----------+---------------------------+---------------+-------------+
| Status | Duration | Source_function | Source_file | Source_line |
+--------------------------------+----------+---------------------------+---------------+-------------+
| starting | 0.000033 | NULL | NULL | NULL |
| checking query cache for query | 0.000088 | send_result_to_client | sql_cache.cc | 1262 |
| Opening tables | 0.000025 | open_tables | sql_base.cc | 4482 |
| System lock | 0.000006 | mysql_lock_tables | lock.cc | 258 |
| Table lock | 0.000081 | mysql_lock_tables | lock.cc | 269 |
| init | 0.000037 | mysql_select | sql_select.cc | 2350 |
| optimizing | 0.000016 | optimize | sql_select.cc | 772 |
| statistics | 0.000021 | optimize | sql_select.cc | 954 |
| preparing | 0.000021 | optimize | sql_select.cc | 964 |
| executing | 0.000005 | exec | sql_select.cc | 1648 |
| Sending data | 0.000500 | exec | sql_select.cc | 2190 |
| end | 0.000008 | mysql_select | sql_select.cc | 2395 |
| query end | 0.000005 | mysql_execute_command | sql_parse.cc | 4821 |
| freeing items | 0.007489 | mysql_parse | sql_parse.cc | 5827 |
| storing result in query cache | 0.000028 | query_cache_end_of_result | sql_cache.cc | 813 |
| logging slow query | 0.000007 | log_slow_statement | sql_parse.cc | 1628 |
| cleaning up | 0.000009 | dispatch_command | sql_parse.cc | 1595 |
+--------------------------------+----------+---------------------------+---------------+-------------+
17 rows in set (0.00 sec)
mysql> SHOW PROFILE SOURCE FOR QUERY 4;
+--------------------------------+----------+-----------------------+--------------+-------------+
| Status | Duration | Source_function | Source_file | Source_line |
+--------------------------------+----------+-----------------------+--------------+-------------+
| starting | 0.000035 | NULL | NULL | NULL |
| checking query cache for query | 0.000014 | send_result_to_client | sql_cache.cc | 1262 |
| checking privileges on cached | 0.000010 | send_result_to_client | sql_cache.cc | 1346 |
| sending cached result to clien | 0.000026 | send_result_to_client | sql_cache.cc | 1441 |
| logging slow query | 0.000005 | log_slow_statement | sql_parse.cc | 1628 |
| cleaning up | 0.000005 | dispatch_command | sql_parse.cc | 1595 |
+--------------------------------+----------+-----------------------+--------------+-------------+
6 rows in set (0.00 sec)
It does not take a rocket scientist to determine that 6 steps within the MySQL kernel is better then 17, regardless of what those steps are, and how different in timing they may be.
I’m not wanting to represent how much saving you may have here, there are many factors such as a realistic example, a loaded warmed up environment etc. You should try this in your own environment with your own queries.
This information was to provide an introduction into looking a little deeper at the Query Cache path within MySQL.
PlanetMySQL Voting:
Vote UP /
Vote DOWN"
No comments:
Post a Comment