If you have installed the plugin, the following SQL statements can be used to define your SQL replacement rules and error message handling.
INSERT INTO query_rewrite.rewrite_rules (message, pattern, replacement)
VALUES(Unique_ID, Original_SQL, Rewrite_SQL);
Pattern – This column represents the pattern or condition that triggers the rewriting of a SQL query. It defines the specific query or query pattern to match.
Replacement – This column specifies the replacement or transformation that should be applied to the matched query or query pattern.
When a SQL query is executed, the Query Rewriter Plugin checks the query_rewrite.rewrite_rules table for matching patterns. If a pattern matches the executed query, the plugin rewrites the query using the corresponding replacement. This allows you to modify the query structure, optimize it, or add custom logic based on specific patterns or conditions.
Here is an example of SQL statement with a hardcoded literal, the SQL takes 2.1 seconds to execute with the following query plan.
SELECT *
FROM employee
WHERE emp_id in (SELECT emp_id id
FROM emp_subsidiary
where emp_dept < ‘D’)
ORDER BY emp_id LIMIT 1;


INSERT INTO query_rewrite.rewrite_rules (message, pattern, replacement)
VALUES(Unique_ID, Original_SQL, Rewrite_SQL);
SELECT *
FROM employee
WHERE emp_id in (SELECT emp_id id
FROM emp_subsidiary
where emp_dept < ?)
ORDER BY emp_id LIMIT 1
Rewrite_SQL
select /*+ QB_NAME(QB1) JOIN_ORDER(`employee`@QB1, `emp_subsidiary`@QB2) */ *
from employee
where emp_id in (select /*+ QB_NAME(QB2) */ emp_id id
from emp_subsidiary
where emp_dept < ?)
order by emp_id limit 1

Tosska DB Ace Enterprise for MySQL – Tosska Technologies Limited
DBAM Tune Rewriter demo – YouTube

