MySQL 支持查询重写插件,这些插件可以在服务器执行收到的 SQL 语句之前检查并可能修改这些语句。请参见 查询重写插件。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 发行版包括一个名为 Rewriter 的后解析查询重写插件以及用于安装该插件及其相关元素的脚本。这些元素协同工作以提供语句重写功能:MySQL distributions include a postparse query rewrite plugin named
Rewriter and scripts for installing the plugin
and its associated elements. These elements work together to
provide statement-rewriting capability:
一个名为
Rewriter的服务器端插件会检查语句,并可能基于其内存中的重写规则缓存对它们进行重写。A server-side plugin namedRewriterexamines statements and may rewrite them, based on its in-memory cache of rewrite rules.这些语句可能会被重写:
SELECT、INSERT、REPLACE、UPDATE和DELETE。These statements are subject to rewriting:SELECT,INSERT,REPLACE,UPDATE, andDELETE.独立语句和预处理语句可以被重写。发生在视图定义或存储程序中的语句不受重写的影响。Standalone statements and prepared statements are subject to rewriting. Statements occurring within view definitions or stored programs are not subject to rewriting.
Rewriter插件使用一个名为query_rewrite的数据库,其中包含一个名为rewrite_rules的表。该表为插件用于决定是否重写语句的规则提供持久存储。用户通过修改存储在此表中的规则集合与插件进行通信。插件通过设置表行的message列与用户通信。TheRewriterplugin uses a database namedquery_rewritecontaining 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 themessagecolumn of table rows.query_rewrite数据库包含一个名为flush_rewrite_rules()的存储过程,用于将规则表的内容加载到插件中。Thequery_rewritedatabase contains a stored procedure namedflush_rewrite_rules()that loads the contents of the rules table into the plugin.一个可加载的函数,名为
load_rewrite_rules(),被flush_rewrite_rules()存储过程使用。A loadable function namedload_rewrite_rules()is used by theflush_rewrite_rules()stored procedure.Rewriter插件公开系统变量,用于插件配置,以及提供运行时操作信息的状态变量。该插件还支持一个特权 (SKIP_QUERY_REWRITE),可以保护指定用户的查询不被重写。TheRewriterplugin exposes system variables that enable plugin configuration and status variables that provide runtime operational information. This plugin also supports a privilege (SKIP_QUERY_REWRITE) that protects a given user's queries from being rewritten.
下面的部分介绍如何安装和使用 Rewriter 插件,并提供其相关元素的参考信息。The following sections describe how to install and use the
Rewriter plugin, and provide reference
information for its associated elements.