MySQL 8.0 Query Rewrite支持SELECT INSERT UPDETE DELETE REPLACE语句重写
这个功能要点赞,比如开发上线时,有个SQL查询字段索引忘记加了,直接把线上CPU打满,此时,你可以将SQL重写,让业务先报错,别打死数据库,然后马上通知开发回滚,等加完索引后再上线。
安装插件
mysql -S /tmp/mysql_hcy.sock -p123456 <./install_rewriter.sql
查看是否生效
SHOW GLOBAL VARIABLES LIKE 'rewriter_enabled';
编写重写规则
insert into query_rewrite.rewrite_rules(pattern, replacement, pattern_database) values ( "SELECT * from sbtest1 limit ?", "SELECT k,c from sbtest1 limit ?", "test");
意思为:
将以下语句
SELECT * from sbtest1 limit ?;
改写成:
SELECT k,c from sbtest1 limit ?;
注:问号?为变量
执行规则生效
CALL query_rewrite.flush_rewrite_rules();
演示
mysql> SELECT * from sbtest1 limit 1\G; *************************** 1. row *************************** k: 499284 c: 83868641912-28773972837-60736120486-75162659906-27563526494-20381887404-41576422241-93426793964-56405065102-33518432330 1 row in set, 1 warning (0.00 sec) ERROR: No query specified mysql> show warnings\G *************************** 1. row *************************** Level: Note Code: 1105 Message: Query 'SELECT * from sbtest1 limit 1' rewritten to 'SELECT k,c from sbtest1 limit 1' by a query rewrite plugin 1 row in set (0.00 sec)