MySQL Execution Plan--使用Query Rewrite

Posted 笑东风

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL Execution Plan--使用Query Rewrite相关的知识,希望对你有一定的参考价值。

安装Query Rewrite Plugin

mysql的安装目录的share文件夹下,有两个文件用来安装和卸载Query Rewrite Plugin:

install_rewriter.sql: 安装脚本
uninstall_rewriter.sql: 卸载脚本
install_rewriter.sql文件中脚本为:
/* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */

CREATE DATABASE IF NOT EXISTS query_rewrite;

CREATE TABLE IF NOT EXISTS query_rewrite.rewrite_rules (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  pattern VARCHAR(10000) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  pattern_database VARCHAR(20) CHARACTER SET utf8 COLLATE utf8_bin,
  replacement VARCHAR(10000) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
  enabled ENUM(YES, NO) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL
    DEFAULT YES,
  message VARCHAR(1000) CHARACTER SET utf8 COLLATE utf8_bin,
  pattern_digest VARCHAR(32),
  normalized_pattern VARCHAR(100)
) DEFAULT CHARSET = utf8 ENGINE = INNODB;

INSTALL PLUGIN rewriter SONAME rewriter.so;
CREATE FUNCTION load_rewrite_rules RETURNS STRING
SONAME rewriter.so;

DELIMITER //

CREATE PROCEDURE query_rewrite.flush_rewrite_rules()
BEGIN
  DECLARE message_text VARCHAR(100);
  COMMIT;
  SELECT load_rewrite_rules() INTO message_text;
  RESET QUERY CACHE;
  IF NOT message_text IS NULL THEN
    SIGNAL SQLSTATE 45000 SET MESSAGE_TEXT = message_text;
  END IF;
END //

DELIMITER ;

RESET QUERY CACHE;

安装完成后,可以使用下面脚本查看功能是否启用:

SHOW GLOBAL VARIABLES LIKE rewriter_enabled;

+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| rewriter_enabled | ON    |
+------------------+-------+

 

演示Demo

1、插入重写规则

INSERT INTO query_rewrite.rewrite_rules
(pattern, replacement, pattern_database) 
VALUES 
(
"SELECT * FROM TB001 WHERE C1=?",
"SELECT *,NEW AS C11 FROM TB001 WHERE C1=?",
"demodb"
);

2、加载规则

## 加载重写规则
CALL query_rewrite.flush_rewrite_rules();


## 查看当前重写规则
SELECT * FROM query_rewrite.rewrite_rules G

*************************** 1. row ***************************
                id: 7
           pattern: SELECT * FROM TB001 WHERE C1=?
  pattern_database: demodb
       replacement: SELECT *,NEW AS C11 FROM TB001 WHERE C1=?
           enabled: YES
           message: NULL
    pattern_digest: cf177a9a728143a27502f890698316e5
normalized_pattern: select `*` from `demodb`.`tb001` where (`C1` = ?)
1 row in set (0.00 sec)

3、测试重写:

SELECT  *  FROM     TB001    
WHERE      C1=2;

+--------+------+-----+
| ID     | C1   | C11 |
+--------+------+-----+
| AAA102 |    2 | NEW |
| AAA112 |    2 | NEW |
| AAA12  |    2 | NEW |
| AAA122 |    2 | NEW |
| AAA132 |    2 | NEW |
+--------+------+-----+


SELECT  *  FROM     TB001    
WHERE      C1=2 LIMIT 1;

+--------+------+
| ID     | C1   |
+--------+------+
| AAA102 |    2 |
+--------+------+

可以发现SQL语句中的换行或空格对重写规则无影响,但不能在重写模板基础上增加其他内容如LIMIT 字句。

以上是关于MySQL Execution Plan--使用Query Rewrite的主要内容,如果未能解决你的问题,请参考以下文章

MySQL Execution Plan--COUNT相关测试

mysql官方文档之Optimization(8.8 Understanding the Query Execution Plan)

sql server 执行计划(execution plan)介绍

sql server 执行计划(execution plan)介绍

Oracle SQL explain/execution Plan

Execution Plan 执行计划介绍