Cost-based Optimization in MySQL 5.7

Optimiser is the brain of the RDBMS. Optimiser decides the right access method , algorithms , join order and right index to be used for better execution of the query. This blog is made to shed some lights on Cost based optimiser in MySQL 5.7. The cost or statistics are stored in the data dictionary .

What is cost-based optimization ?

  • The cost model is based on estimates of cost various operations occur during query execution.
  • The optimizer has a set of default “cost constants” it will make decision on execution plans.
  • In MySQL 5.7, the optimizer has addition a database of cost estimates to use during the execution plan.
  • These cost estimates are stored in server_cost & engine_cost tables in MySQL schema. For more details Cost Model


MySQL Optimizer

Cost Model Databases:

The MySQL system database contain cost estimate information tables inside MySQL schema.

==> Server_cost

==> Engine_cost


  • The server_cost estimates general server operations.The server_cost contain these columns.


  • The name of a cost estimate used in cost-model,it’s not case sensitive .


  • If the value is not null the server use’s the cost .Otherwise it use’s default estimate value.


  • It show’s time of the last row updated.


  • It’s a descriptive comment associated with cost model.

Cost_name values for server_cost tables:


  • It estimates the operations specific for particular storage engines.


  • The name of the storage engine this cost estimate applied .If the value is default it applies all storage engines.


  • It’s intended for specifying storage devices like hard drives (vs) solid state drives.


  • The name of a cost estimate used in cost-model,it’s not case sensitive .


  • If the value is not null the server use’s the cost .Otherwise it use’s default estimate value.


  • It show’s time of the last row updated.


  • Same as the server cost table.

Cost_name’s for engine_cost table:


  • The default value of io_block_read_cost is (1.0).It will reading an index or data block from disk.


  • The default value is (1.0),it’s similar to io_block_read_cost. It represents the cost of reading an index or data block from an memory database buffer.

Cost-based Model Architecture:


How to estimate a Cost?

The cost for executing a query below this method used.

Cost Unit:

The cost unit is calculated form executing  query read a random data pages.

Cost Factors:

There are 2 main factors are used.

==> IO cost

==> CPU cost

IO cost:

  • The IO cost is estimates from storage engine based on number of pages to read.
  • IO is estimated in pages read from table and pages read from index.

CPU cost:

The CPU cost is based on evaluating query conditions , comparing keys/records and sorting keys.

Cost Constants:

Cost Cost-value
Reading a random page 1.0
Evaluating Query conditions 0.2
Comparing key/record 0.1


This is cost model for a range scan 


select * from City where Population between 731200 and 801200;

In this table City contain a 4013 records.


explain format=json  select * from City where Population between 731200 and 801200;


Calculating IO cost:

IO cost = pages in table * io_block_read_cost


Let us assume io_block_read_cost is 1.0

IO cost = 445 * 1.0

Calculating CPU cost:

CPU cost = records * row_evaluate_cost


Let’s assume row_evaluate_cost=0.2

CPU cost = 4013 * 0.2

Total cost = 525.26

Cost-model for joins:

  • The query have a two or more table joins a MySQL optimizer mission is to find best combination of join order and access method.
  • So the response time becomes come to low as possible.


Select City.Name,City.Population,Country.Region,Country.LocalName from City left outer join Country on City.ID=Country.Code where City.Population between 5000 and 100000 order by City.Population;
  • In this join query MySQL may choose (City,Country) or (Country,City) join order.  The join order does not matter when deciding a access method.
  • In either case query execution plan will be table scan on first table and access for second table.
  • This is the graphical representation of this query.




  • This query scanning a full table so it will increase a total cost of the query. How to reduce the total cost of this query?.
  • The best solution is adding a indexes for specific columns & most important thing is enable the “condition_fanout_filter” in optimizer switch.


  • It will make the better decision to use all condition’s on table, and it’s estimate a qualifying row’s that will be join to the next table.



Enable a Condition filter:

Enable the condition filter in globally,

 mysql> set optimizer_switch ='condition_fanout_filter=ON';

After that create a index for above that query in population field, it will give better performance.


It will reduce the number of rows scanning and decrease the query cost.

Configurable cost:

  • In MySQL 5.7 we can able to configure the server_cost and engine_cost to manually.
  • In default cost value have a NULL values in both server_cost & engine_cost tables.



mysql> update mysql.server_cost set cost_value=3;

mysql> update mysql.engine_cost set cost_value=0.5;


  • In MySQL 5.6 we cannot assign the value for server cost and engine cost.But MySQL 5.7 we can assign the values.
  • The MySQL 5.7 they introduce a condition will make a better decision to use all condition’s in the table.  
  • The cost model  will chose the scenario for least cost and most efficient way to run the query.


  • In MySQL 5.7 they improved the optimizer performance,the cost-based optimization is best method for analyzing the performance & cost of the query.
  • In this blog I explained basics  of cost-based optimization and how it’s works. Next series I will explain very detailed about  this cost model.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s