

Thus, the selectivity of a predicate indicates how many rows pass the predicate test. Selectivity is an internal calculation that is not visible in execution plans.Ī predicate filters a specific number of rows from a row set. For example, if the advisor is not frequently notified of traffic jams, road closures, and poor road conditions, then the recommended route may turn out to be inefficient (high cost). The more accurate the statistics, the better the advice. The advisor picks the most efficient (lowest cost) overall route based on user-specified goals and the available statistics about roads and traffic conditions. For example, the trip advisor may estimate one subroute at 15 minutes with medium difficulty, an alternative subroute at 22 minutes with minimal difficulty, and so on. Internally, the advisor may divide the overall route into several subroutes (subplans), and calculate the efficiency for each subroute separately. In this analogy, an execution plan is a possible route generated by the trip advisor. The cyclist can influence the trip advisor's decision by using directives such as "I want to arrive as fast as possible" or "I want the easiest ride possible."

A query is like the directive "I need the most efficient route from point A to point B" or "I need the most efficient route from point A to point B by way of point C." The trip advisor uses an internal algorithm, which relies on factors such as speed and difficulty, to determine the most efficient route. One analogy for the optimizer is an online trip advisor.Ī cyclist wants to know the most efficient bicycle route from point A to point B. For this reason, the optimizer is sometimes called the cost-based optimizer (CBO) to contrast it with the legacy rule-based optimizer (RBO).

After calculating the costs of alternative plans, the optimizer chooses the plan with the lowest cost estimate.
#MYSQL OPTIMIZER SCRIPT FULL#
The optimizer determines the optimal plan for a SQL statement by examining multiple access methods, such as full table scan or index scans, different join methods such as nested loops and hash joins, different join orders, and possible transformations.įor a given query and environment, the optimizer assigns a relative numerical cost to each step of a possible plan, and then factors these values together to generate an overall cost estimate for the plan. The database optimizes each SQL statement based on statistics collected about the accessed data.
#MYSQL OPTIMIZER SCRIPT FREE#
SQL is a nonprocedural language, so the optimizer is free to merge, reorganize, and process in any order. Query optimization is the overall process of choosing the most efficient means of executing a SQL statement. For this reason, all SQL statements use the optimizer. However, if statistics indicate that very few employees are managers, then reading an index followed by a table access by rowid may be more efficient than a full table scan.īecause the database has many internal statistics and tools at its disposal, the optimizer is usually in a better position than the user to determine the optimal method of statement execution. If the optimizer statistics indicate that 80% of employees are managers, then the optimizer may decide that a full table scan is most efficient. For a specific query in a given environment, the cost computation accounts for factors of query execution such as I/O, CPU, and communication.įor example, a query might request information about employees who are managers. The optimizer uses available statistics to calculate cost.

The optimizer choose the plan with the lowest cost among all considered candidate plans. The optimizer attempts to generate the most optimal execution plan for a SQL statement.
