O'Reilly logo

MySQL 8 Cookbook by Karthik Appigatla

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Optimizer hints

Instead of adjusting the optimizer switch or optimizer_search_depth variables at session level, you can hint the optimizer to use, or not to use, certain optimizations. The scope of the optimizer hint is limited to the statement that gives you finer control over the queries, whereas the optimizer switch can be at session or global level.

Again, take the example of the preceding query; if you feel that using sort_union is not optimal, you can turn it off by passing it as a hint in the query itself:

mysql> EXPLAIN SELECT /*+ NO_INDEX_MERGE(employees first_name,last_name_2) */ * FROM employees WHERE first_name='Adam' OR last_name='Adam'\G*************************** 1. row ***************************           id: 1 select_type: SIMPLE ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required