41から始めました

文字通り41歳から始めたブログです。DB(MySQL)を使ってお仕事してるので、DB周りの話を中心に最近始めたこととかをTwitterのノリで書いています。なお、本サイトにおいて示されている見解は私個人の見解であり、所属団体や組織を代表するものではありません。

クエリーキャッシュを有効にしたけりゃサーバー再起動

え?何当たり前のこと言ってるの?

そう思われた貴方、クエリキャッシュをちゃんと使うか使わないか最初に考えている偉い人ですね。

query_cache_typeをON(1)にするとクエリーキャッシュが有効になるんですが、これ、ドキュメントを読むと少なくともMySQL5.6以降は動的に変更(SET GLOBAL)できそうに見えます。

f:id:next4us-ti:20200719174422p:plain

5.5でもできそうに見える)

ところが、有効なのを無効にはできるんですが、無効な状態を有効にはできないんです。

試してみた

my.cnfでまず有効にした状態で起動してみます。

$ grep query_cache_type ./msb_5_7_30/my.sandbox.cnf
query_cache_type=1
$ ./msb_5_7_30/restart
stop /home/takiida/sandboxes/msb_5_7_30
. sandbox server started
$ mysql -uroot -pxxxxx -h127.0.0.1 -P5730
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.30-enterprise-commercial-advanced MySQL Enterprise Server - Advanced Edition (Commercial)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

有効な状態から無効に変更することはSET GLOBAL句で可能です。

(root@127.0.0.1) [(none)]> show global variables like 'query_cache_type';
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| query_cache_type | ON    |
+------------------+-------+
1 row in set (0.01 sec)

(root@127.0.0.1) [(none)]> set global query_cache_type = 0;
Query OK, 0 rows affected, 1 warning (0.00 sec)

(root@127.0.0.1) [(none)]> show global variables like 'query_cache_type';
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| query_cache_type | OFF   |
+------------------+-------+
1 row in set (0.01 sec)

(root@127.0.0.1) [(none)]> ^DBye

ところが

クエリキャッシュを無効にして再起動します。

$ grep query_cache_type ./msb_5_7_30/my.sandbox.cnf
query_cache_type=0
$ ./msb_5_7_30/restart
stop /home/takiida/sandboxes/msb_5_7_30
. sandbox server started
$ mysql -uroot -pxxxxx -h127.0.0.1 -P5730
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.30-enterprise-commercial-advanced MySQL Enterprise Server - Advanced Edition (Commercial)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

無効な状態から有効にしようとすると怒られる!

(root@127.0.0.1) [(none)]> show global variables like 'query_cache_type';
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| query_cache_type | OFF   |
+------------------+-------+
1 row in set (0.00 sec)

(root@127.0.0.1) [(none)]> set global query_cache_type = 1;
ERROR 1651 (HY000): Query cache is disabled; restart the server with query_cache_type=1 to enable it

query_cache_typeを1にするんならサーバー再起動しろや!と。

クエリキャッシュ、最初から有効か、無効の環境しか使ったこと無いから知らんかった。

ドキュメントが微妙ですね…

ドキュメント的に優しくないので直るといいなぁ、と思うのでちょっとプッシュしておこうかと思います。 (まあ、MySQL8.0で廃止されたクエリーキャッシュをあとから有効にしたいとかそんなにニーズが無いから放置されてたのかもしれませんが…)