QOT

MySQL DBA’s intelligent assistant

Rewrites

1. expand-stars
2. orderby

expand-stars

This rewrite transforms wildcards like * or table.* into the field list. For example if we have some table t1 with fields f1, f2, f3 then a query like

SELECT t1.*, t2.value  FROM t1, t2

will be transformed into

SELECT t1.f1, t1.f2, t1.f3, t2.value  FROM t1, t2

This rewrite is recommended almost always. It is important because otherwise when a table is extended the newly added fields will be auto-selected by all the queries that use wildcards, which is often undesired. One notable exception is when you gather some meta-information about the table and want to get list of table fields.

orderby

This rewrite (added in version 0.0.4) removes constant fields from the ORDER BY clause, as they don’t influence the sorting order. Constant fields are the fields that are guaranteed to have a single value according to the WHERE condition. For example in this query

SELECT * FROM t1 WHERE col1 = 1 and col2 > 2 ORDER BY col1, col2

col1 is constant and thus the query can be rewritten as

SELECT * FROM t1 WHERE col1 = 1 and col2 > 2 ORDER BY col2