Documentation Home
MySQL 5.7 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 37.5Mb
PDF (A4) - 37.5Mb
PDF (RPM) - 37.1Mb
EPUB - 10.5Mb
HTML Download (TGZ) - 10.3Mb
HTML Download (Zip) - 10.3Mb
HTML Download (RPM) - 9.0Mb
Eclipse Doc Plugin (TGZ) - 11.2Mb
Eclipse Doc Plugin (Zip) - 13.4Mb
Man Pages (TGZ) - 204.0Kb
Man Pages (Zip) - 309.3Kb
Info (Gzip) - 3.4Mb
Info (Zip) - 3.4Mb
Excerpts from this Manual

MySQL 5.7 Reference Manual  /  ...  /  The Rewriter Query Rewrite Plugin

6.5.5 The Rewriter Query Rewrite Plugin

As of MySQL 5.7.6, MySQL Server supports query rewrite plugins that can examine and possibly modify statements received by the server before the server executes them. See Query Rewrite Plugins.

MySQL distributions include a postparse query rewrite plugin named Rewriter and scripts for installing the plugin and its associated components. These components work together to provide SELECT rewriting capability:

  • A server-side plugin named Rewriter examines SELECT statements and may rewrite them, based on its in-memory cache of rewrite rules. Standalone SELECT statements and SELECT statements in prepared statements are subject to rewriting. SELECT statements occurring within view definitions or stored programs are not subject to rewriting.

  • The Rewriter plugin uses a database named query_rewrite containing a table named rewrite_rules. The table provides persistent storage for the rules that the plugin uses to decide whether to rewrite statements. Users communicate with the plugin by modifying the set of rules stored in this table. The plugin communicates with users by setting the message column of table rows.

  • The query_rewrite database contains a stored procedure named flush_rewrite_rules() that loads the contents of the rules table into the plugin.

  • A user-defined function named load_rewrite_rules() is used by the flush_rewrite_rules() stored procedure.

  • The Rewriter plugin exposes system variables that enable plugin configuration and status variables that provide runtime operational information.

The following sections describe how to install and use the Rewriter plugin, and provide reference information for its associated components.


User Comments
  Posted by Revathi Rangachari on September 2, 2015
This is an example of rewriting a select query which initially selects two columns from a table (film) and after query-rewrite, displays additional columns (being added to the query_rewrite.rewrite_rules table), without having to alter the existing select query.
Can be useful when a table is altered with additional columns and when one wants to get the additional columns also in the query output.
-- Create a test table film
mysql> create table film (film_id tinyint unsigned not null, film_name varchar(25), description varchar(50), directed_by varchar(25), actors varchar(100));
Query OK, 0 rows affected (0.52 sec)
-- Insert one row into the table
mysql> insert into film values (1, 'Ricki and the Flash','American Comedy Drama','Jonathan Demme','The film stars Meryl Streep, Mamie Gummer, Kevin Kline, Sebastian Stan, Rick Springfield');
Query OK, 1 row affected (0.01 sec)

SELECT - BEFORE ADDING RULES TO query_rewrite.rewrite_rules table
mysql> select film_name,description from film where film_id=1;
+---------------------+-----------------------+
| film_name | description |
+---------------------+-----------------------+
| Ricki and the Flash | American Comedy Drama |
+---------------------+-----------------------+
1 row in set (0.00 sec)

-- insert the query rewrite rules in the table
mysql> INSERT INTO query_rewrite.rewrite_rules ( pattern, pattern_database, replacement ) VALUES ('SELECT film_name, description FROM film WHERE film_id = ? ', 'test57', 'SELECT film_name, description, directed_by, actors FROM film WHERE film_id = ? ');
Query OK, 1 row affected (0.01 sec)
-- Call this procedure to refresh the rewrite rules table after adding the rules
mysql> CALL query_rewrite.flush_rewrite_rules();
Query OK, 0 rows affected (1.19 sec)

SELECT - AFTER ADDING RULES TO query_rewrite.rewrite_rules table - displays all the column from the film table
mysql> select film_name,description from film where film_id=1;
+---------------------+-----------------------+----------------+------------------------------------------------------------------------------------------+
| film_name | description | directed_by | actors |
+---------------------+-----------------------+----------------+------------------------------------------------------------------------------------------+
| Ricki and the Flash | American Comedy Drama | Jonathan Demme | The film stars Meryl Streep, Mamie Gummer, Kevin Kline, Sebastian Stan, Rick Springfield |
+---------------------+-----------------------+----------------+------------------------------------------------------------------------------------------+
1 row in set, 1 warning (0.00 sec)

Note: The additional columns directed_by,actors were added to the rewrite rules table
Sign Up Login You must be logged in to post a comment.