MySQL Execution Plan--使用Query Rewrite

Installation Query Rewrite Plugin

In the MySQL installation directory of the share folder, there are two files used to install and uninstall Query Rewrite Plugin:

install_rewriter.sql: installation script 
uninstall_rewriter.sql: uninstall script
install_rewriter.sql file script:
/* 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;

After installation is complete, you can use the following script to see whether the function is enabled:

SHOW GLOBAL VARIABLES LIKE 'rewriter_enabled';

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

 

Demo Demo

1, insert rewrite rules

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, loading rules

## rewriting rules to load 
the CALL query_rewrite.flush_rewrite_rules (); 


## to view the current rewrite the rules of 
the SELECT  *  the FROM query_rewrite.rewrite_rules \ G 

********************** *****  1 . Row *************************** 
                the above mentioned id: 7 
           pattern: the SELECT  *  the FROM TB001 the WHERE C1 = ? 
  pattern_database: demodb 
       Replacement: the SELECT  * , ' NEW '  AS C11 the FROM TB001 the 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, the test rewrite:

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 statements can be found in the line breaks or spaces no effect on the rewrite rule, but you can not add other content such as LIMIT clause rewriting template basis.

Guess you like

Origin www.cnblogs.com/gaogao67/p/11757595.html