Documentation Home
MySQL 8.0 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 45.2Mb
PDF (A4) - 45.2Mb
PDF (RPM) - 40.7Mb
HTML Download (TGZ) - 10.4Mb
HTML Download (Zip) - 10.5Mb
HTML Download (RPM) - 9.0Mb
Man Pages (TGZ) - 208.4Kb
Man Pages (Zip) - 310.5Kb
Info (Gzip) - 4.0Mb
Info (Zip) - 4.0Mb
Excerpts from this Manual

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

5.6.4 The Rewriter Query Rewrite Plugin

MySQL supports query rewrite plugins that can examine and possibly modify SQL 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 statement-rewriting capability:

  • A server-side plugin named Rewriter examines statements and may rewrite them, based on its in-memory cache of rewrite rules.

  • These statements are subject to rewriting:

    Standalone statements and prepared statements are subject to rewriting. 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
User comments in this section are, as the name implies, provided by MySQL users. The MySQL documentation team is not responsible for, nor do they endorse, any of the information provided here.
  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