The Rewriter Query Rewrite Plugin Suppose also that the  id  column is renamed to  user_id  (perhaps the table must be modified to add another type of ID and it is necessary to indicate more specifically what type of ID the  id  column represents). The change means that applications must refer to  user_id  rather than  id  in the  WHERE  clause. But if there are old applications that cannot be written to change the  SELECT  queries they generate, they no longer work properly. The  Rewriter  plugin can solve this problem. To match and rewrite statements whether or not they qualify the table name, add the following two rules and reload the rules table: Rewriter  uses the first rule to match statements that use the qualified table name. It uses the second to match statements that used the unqualified name, but only if the default database is  appdb  (the value in pattern_database ). The  Rewriter  plugin uses statement digests and digest hash values to match incoming statements against rewrite rules in stages. The  max_digest_length  system variable determines the size of the buffer used for computing statement digests. Larger values enable computation of digests that distinguish longer statements. Smaller values use less memory but increase the likelihood of longer statements colliding with the same digest value. The plugin matches each statement to the rewrite rules as follows: If multiple rules match a statement, it is nondeterministic which one the plugin uses to rewrite the statement. If a pattern contains more markers than the replacement, the plugin discards excess data values. If a pattern contains fewer markers than the replacement, it is an error. The plugin notices this when the rules table is loaded, writes an error message to the  message  column of the rule row to communicate the problem, and sets the  Rewriter_reload_error  status variable to  ON . Prepared statements are rewritten at parse time (that is, when they are prepared), not when they are executed later. INSERT INTO query_rewrite.rewrite_rules (pattern, replacement) VALUES( ’SELECT * FROM appdb.users WHERE id = ?’, ’SELECT * FROM appdb.users WHERE user_id = ?’ ); INSERT INTO query_rewrite.rewrite_rules (pattern, replacement, pattern_database) VALUES( ’SELECT * FROM users WHERE id = ?’, ’SELECT * FROM users WHERE user_id = ?’, ’appdb’ ); CALL query_rewrite.flush_rewrite_rules(); How Statement Matching Works 1. Compute the statement digest hash value and compare it to the rule digest hash values. This is subject to false positives, but serves as a quick rejection test. 2. If the statement digest hash value matches any pattern digest hash values, match the normalized (statement digest) form of the statement to the normalized form of the matching rule patterns. 3. If the normalized statement matches a rule, compare the literal values in the statement and the pattern. A  ?  character in the pattern matches any literal value in the statement. If the statement prepares a statement,  ?  in the pattern also matches  ?  in the statement. Otherwise, corresponding literals must be the same. Rewriting Prepared Statements 1091