Rechercher dans le manuel MySQL

5.6.4.2 Using the Rewriter Query Rewrite Plugin

To enable or disable the plugin, enable or disable the rewriter_enabled system variable. By default, the Rewriter plugin is enabled when you install it (see Section 5.6.4.1, “Installing or Uninstalling the Rewriter Query Rewrite Plugin”). To set the initial plugin state explicitly, you can set the variable at server startup. For example, to enable the plugin in an option file, use these lines:

[mysqld]
rewriter_enabled=ON

It is also possible to enable or disable the plugin at runtime:

  1. SET GLOBAL rewriter_enabled = ON;
  2. SET GLOBAL rewriter_enabled = OFF;

Assuming that the Rewriter plugin is enabled, it examines and possibly modifies each rewritable statement received by the server. The plugin determines whether to rewrite statements based on its in-memory cache of rewriting rules, which are loaded from the rewrite_rules table in the query_rewrite database.

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.

Adding Rewrite Rules

To add rules for the Rewriter plugin, add rows to the rewrite_rules table, then invoke the flush_rewrite_rules() stored procedure to load the rules from the table into the plugin. The following example creates a simple rule to match statements that select a single literal value:

  1. INSERT INTO query_rewrite.rewrite_rules (pattern, replacement)
  2. VALUES('SELECT ?', 'SELECT ? + 1');

The resulting table contents look like this:

  1. mysql> SELECT * FROM query_rewrite.rewrite_rules\G
  2. *************************** 1. row ***************************
  3.                 id: 1
  4.            pattern: SELECT ?
  5.   pattern_database: NULL
  6.        replacement: SELECT ? + 1
  7.            enabled: YES
  8.            message: NULL
  9.     pattern_digest: NULL
  10. normalized_pattern: NULL

The rule specifies a pattern template indicating which SELECT statements to match, and a replacement template indicating how to rewrite matching statements. However, adding the rule to the rewrite_rules table is not sufficient to cause the Rewriter plugin to use the rule. You must invoke flush_rewrite_rules() to load the table contents into the plugin in-memory cache:

  1. mysql> CALL query_rewrite.flush_rewrite_rules();
Tip

If your rewrite rules seem not to be working properly, make sure that you have reloaded the rules table by calling flush_rewrite_rules().

When the plugin reads each rule from the rules table, it computes a normalized (statement digest) form from the pattern and a digest hash value, and uses them to update the normalized_pattern and pattern_digest columns:

  1. mysql> SELECT * FROM query_rewrite.rewrite_rules\G
  2. *************************** 1. row ***************************
  3.                 id: 1
  4.            pattern: SELECT ?
  5.   pattern_database: NULL
  6.        replacement: SELECT ? + 1
  7.            enabled: YES
  8.            message: NULL
  9.     pattern_digest: d1b44b0c19af710b5a679907e284acd2ddc285201794bc69a2389d77baedddae
  10. normalized_pattern: select ?

For information about statement digesting, normalized statements, and digest hash values, see Section 26.10, “Performance Schema Statement Digests and Sampling”.

If a rule cannot be loaded due to some error, calling flush_rewrite_rules() produces an error:

  1. mysql> CALL query_rewrite.flush_rewrite_rules();
  2. ERROR 1644 (45000): Loading of some rule(s) failed.

When this occurs, the plugin writes an error message to the message column of the rule row to communicate the problem. Check the rewrite_rules table for rows with non-NULL message column values to see what problems exist.

Patterns use the same syntax as prepared statements (see Section 13.5.1, “PREPARE Syntax”). Within a pattern template, ? characters act as parameter markers that match data values. Parameter markers can be used only where data values should appear, not for SQL keywords, identifiers, and so forth. The ? characters should not be enclosed within quotation marks.

Like the pattern, the replacement can contain ? characters. For a statement that matches a pattern template, the plugin rewrites it, replacing ? parameter markers in the replacement using data values matched by the corresponding markers in the pattern. The result is a complete statement string. The plugin asks the server to parse it, and returns the result to the server as the representation of the rewritten statement.

After adding and loading the rule, check whether rewriting occurs according to whether statements match the rule pattern:

  1. mysql> SELECT PI();
  2. +----------+
  3. | PI()     |
  4. +----------+
  5. | 3.141593 |
  6. +----------+
  7. 1 row in set (0.01 sec)
  8.  
  9. mysql> SELECT 10;
  10. +--------+
  11. | 10 + 1 |
  12. +--------+
  13. |     11 |
  14. +--------+
  15. 1 row in set, 1 warning (0.00 sec)

No rewriting occurs for the first SELECT statement, but does for the second. The second statement illustrates that when the Rewriter plugin rewrites a statement, it produces a warning message. To view the message, use SHOW WARNINGS:

  1. mysql> SHOW WARNINGS\G
  2. *************************** 1. row ***************************
  3.   Level: Note
  4.    Code: 1105
  5. Message: Query 'SELECT 10' rewritten to 'SELECT 10 + 1' by a query rewrite plugin

A statement need not be rewritten to a statement of the same type. The following example loads a rule that rewrites DELETE statements to UPDATE statements:

  1. INSERT INTO query_rewrite.rewrite_rules (pattern, replacement)
  2. VALUES('DELETE FROM db1.t1 WHERE col = ?',
  3.        'UPDATE db1.t1 SET col = NULL WHERE col = ?');
  4. CALL query_rewrite.flush_rewrite_rules();

To enable or disable an existing rule, modify its enabled column and reload the table into the plugin. To disable rule 1:

  1. UPDATE query_rewrite.rewrite_rules SET enabled = 'NO' WHERE id = 1;
  2. CALL query_rewrite.flush_rewrite_rules();

This enables you to deactivate a rule without removing it from the table.

To re-enable rule 1:

  1. UPDATE query_rewrite.rewrite_rules SET enabled = 'YES' WHERE id = 1;
  2. CALL query_rewrite.flush_rewrite_rules();

The rewrite_rules table contains a pattern_database column that Rewriter uses for matching table names that are not qualified with a database name:

  • Qualified table names in statements match qualified names in the pattern if corresponding database and table names are identical.

  • Unqualified table names in statements match unqualified names in the pattern only if the default database is the same as pattern_database and the table names are identical.

Suppose that a table named appdb.users has a column named id and that applications are expected to select rows from the table using a query of one of these forms, where the second can be used only if appdb is the default database:

  1. SELECT * FROM users WHERE appdb.id = id_value;
  2. SELECT * FROM users WHERE id = id_value;

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 will 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:

  1. INSERT INTO query_rewrite.rewrite_rules
  2.     (pattern, replacement) VALUES(
  3.     'SELECT * FROM appdb.users WHERE id = ?',
  4.     'SELECT * FROM appdb.users WHERE user_id = ?'
  5.     );
  6. INSERT INTO query_rewrite.rewrite_rules
  7.     (pattern, replacement, pattern_database) VALUES(
  8.     'SELECT * FROM users WHERE id = ?',
  9.     'SELECT * FROM users WHERE user_id = ?',
  10.     'appdb'
  11.     );
  12. CALL query_rewrite.flush_rewrite_rules();

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).

Inhoudsopgave Haut

How Statement Matching Works

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:

  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.

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.

Inhoudsopgave Haut

Rewriting Prepared Statements

Prepared statements are rewritten at parse time (that is, when they are prepared), not when they are executed later.

Prepared statements differ from nonprepared statements in that they may contain ? characters as parameter markers. To match a ? in a prepared statement, a Rewriter pattern must contain ? in the same location. Suppose that a rewrite rule has this pattern:

  1. SELECT ?, 3

The following table shows several prepared SELECT statements and whether the rule pattern matches them.

Prepared Statement Whether Pattern Matches Statement
PREPARE s AS 'SELECT 3, 3' Yes
PREPARE s AS 'SELECT ?, 3' Yes
PREPARE s AS 'SELECT 3, ?' No
PREPARE s AS 'SELECT ?, ?' No

Inhoudsopgave Haut

Rewriter Plugin Operational Information

The Rewriter plugin makes information available about its operation by means of several status variables:

  1. mysql> SHOW GLOBAL STATUS LIKE 'Rewriter%';
  2. +-----------------------------------+-------+
  3. | Variable_name                     | Value |
  4. +-----------------------------------+-------+
  5. | Rewriter_number_loaded_rules      | 1     |
  6. | Rewriter_number_reloads           | 5     |
  7. | Rewriter_number_rewritten_queries | 1     |
  8. | Rewriter_reload_error             | ON    |
  9. +-----------------------------------+-------+

For descriptions of these variables, see Section 5.6.4.3.4, “Rewriter Query Rewrite Plugin Status Variables”.

When you load the rules table by calling the flush_rewrite_rules() stored procedure, if an error occurs for some rule, the CALL statement produces an error, and the plugin sets the Rewriter_reload_error status variable to ON:

  1. mysql> CALL query_rewrite.flush_rewrite_rules();
  2. ERROR 1644 (45000): Loading of some rule(s) failed.
  3.  
  4. mysql> SHOW GLOBAL STATUS LIKE 'Rewriter_reload_error';
  5. +-----------------------+-------+
  6. | Variable_name         | Value |
  7. +-----------------------+-------+
  8. | Rewriter_reload_error | ON    |
  9. +-----------------------+-------+

In this case, check the rewrite_rules table for rows with non-NULL message column values to see what problems exist.

Inhoudsopgave Haut

Rewriter Plugin Use of Character Sets

When the rewrite_rules table is loaded into the Rewriter plugin, the plugin interprets statements using the current global value of the character_set_client system variable. If the global character_set_client value is changed subsequently, the rules table must be reloaded.

A client must have a session character_set_client value identical to what the global value was when the rules table was loaded or rule matching will not work for that client.


Zoek in de MySQL-handleiding

Nederlandse vertaling

U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.

Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.

Bij voorbaat dank.

Document heeft de 26/06/2006 gemaakt, de laatste keer de 26/10/2018 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/mysql-rf-rewriter-query-rewrite-plugin-usage.html

De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.

Referenties

  1. Bekijk - html-document Taal van het document:en Manuel MySQL : https://dev.mysql.com/

Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.

Inhoudsopgave Haut