[+/-]
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 namedquery_rewrite
containing a table namedrewrite_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 themessage
column of table rows.The
query_rewrite
database contains a stored procedure namedflush_rewrite_rules()
that loads the contents of the rules table into the plugin.A user-defined function named
load_rewrite_rules()
is used by theflush_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.
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;
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;
1 row in set, 1 warning (0.00 sec)
Note: The additional columns directed_by,actors were added to the rewrite rules table