You might be tempted to rewrite your queries to make arithmetic operations faster, while sacrificing readability. Because MySQL does similar optimizations automatically, you can often avoid this work, and leave the query in a more understandable and maintainable form. Some of the optimizations performed by MySQL follow: 

•Removal of unnecessary parentheses: 

((a AND b) AND c OR (((a AND b) AND (c AND d)))) -> (a AND b AND c) OR (a AND b AND c AND d) 

•Constant folding: 

(a<b AND b=c) AND a=5 -> b>5 AND b=c AND a=5 

•Constant condition removal: 

(b>=5 AND b=5) OR (b=6 AND 5=5) OR (b=7 AND 5=6) -> b=5 OR b=6 

In MySQL 8.0.14 and later, this takes place during preparation rather than during the optimization phase, which helps in simplification of joins. See Section 8.2.1.9, “Outer Join Optimization” , for further information and examples. 

•Constant expressions used by indexes are evaluated only once. 

•Beginning with MySQL 8.0.16, comparisons of columns of numeric types with constant values are checked and folded or removed for invalid or out-of-rage values: 

# CREATE TABLE t (c TINYINT UNSIGNED NOT NULL); SELECT * FROM t WHERE c ≪  256; - ≫  SELECT * FROM t WHERE 1; 

See Section 8.2.1.14, “Constant-Folding Optimization” , for more information. 

• COUNT(*)  on a single table without a WHERE  is retrieved directly from the table information for MyISAM and MEMORY  tables. This is also done for any NOT NULL  expression when used with only one table. 

•Early detection of invalid constant expressions. MySQL quickly detects that some SELECT  statements are impossible and returns no rows. 

• HAVING  is merged with WHERE  if you do not use GROUP BY  or aggregate functions ( COUNT() , MIN() , and so on). 

•For each table in a join, a simpler WHERE  is constructed to get a fast WHERE  evaluation for the table and also to skip rows as soon as possible. 

• All constant tables are read first before any other tables in the query. A constant table is any of the following: 

•An empty table or a table with one row. 

•A table that is used with a WHERE  clause on a PRIMARY KEY  or a UNIQUE  index, where all index parts are compared to constant expressions and are defined as NOT NULL . 

All of the following tables are used as constant tables: 

SELECT * FROM t WHERE primary_key =1; SELECT * FROM t1,t2 WHERE t1. primary_key =1 AND t2. primary_key =t1.id; 

