SQL Keywords

SQL OPTIMIZER_COSTS

What is the OPTIMIZER_COSTS variable in MySQL?

OPTIMIZER_COSTS is a MySQL session or global system variable that lets you fine-tune the internal cost constants the optimizer uses when choosing query execution plans.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL OPTIMIZER_COSTS: Supported: MySQL 8.0.4+, Percona Server 8.0+, MariaDB 10.5+ (partial). Not available in PostgreSQL, SQL Server, Oracle, or SQLite.

SQL OPTIMIZER_COSTS Full Explanation

OPTIMIZER_COSTS overrides the default cost model constants MySQL applies during its cost-based plan selection phase. Each constant represents an estimated relative cost of a low-level operation (reading a block from disk, evaluating a row, creating a temporary table, and so on). By altering these numbers you can bias the optimizer toward or away from certain strategies, such as preferring index scans over full table scans when I/O is expensive, or favouring in-memory operations in RAM-heavy environments.The variable is dynamic and can be set at GLOBAL or SESSION scope. When set at GLOBAL scope it affects all new sessions; an active session can override the global defaults by setting the SESSION value. You supply a comma-separated list of name=value pairs. Omitted names keep their current values. All numeric values are floating-point numbers greater than zero.Because the variable directly influences plan generation, changes can have large performance implications. Always benchmark in a non-production environment first, use SESSION-level settings for experimentation, and persist only well-tested GLOBAL values in my.cnf.

SQL OPTIMIZER_COSTS Syntax

-- Session scope
SET SESSION OPTIMIZER_COSTS = 'io_block_read_cost=0.15, memory_block_read_cost=0.05';

-- Global scope (requires SUPER or SYSTEM_VARIABLES_ADMIN)
SET GLOBAL OPTIMIZER_COSTS = 'memory_temptable_row_cost=0.01';

-- Read current values
SHOW VARIABLES LIKE 'optimizer_costs';

SQL OPTIMIZER_COSTS Parameters

  • io_block_read_cost (FLOAT) - Relative cost of reading one block from disk.
  • memory_block_read_cost (FLOAT) - Cost of reading one block already resident in memory.
  • cpu_tuple_cost (FLOAT) - CPU cost of processing one row.
  • cpu_index_tuple_cost (FLOAT) - CPU cost of processing one index entry.
  • memory_temptable_create_cost (FLOAT) - Cost to create an in-memory temporary table.
  • memory_temptable_row_cost (FLOAT) - Cost per row inserted into an in-memory temporary table.
  • disk_temptable_create_cost (FLOAT) - Cost to create a disk-based temporary table.
  • disk_temptable_row_cost (FLOAT) - Cost per row inserted into a disk temporary table.
  • key_compare_cost (FLOAT) - Cost of comparing two index keys.
  • key_lookup_cost (FLOAT) - Cost of looking up one row via an index.
  • Note - Supply only the constants you wish to override; others retain their last value.

Example Queries Using SQL OPTIMIZER_COSTS

-- Bias the optimizer toward RAM and CPU, discouraging disk I/O
SET SESSION OPTIMIZER_COSTS =
  'io_block_read_cost=1.0, memory_block_read_cost=0.05, cpu_tuple_cost=0.02';

-- Favour disk-based temporary tables when memory is constrained
SET GLOBAL OPTIMIZER_COSTS =
  'memory_temptable_row_cost=0.20, disk_temptable_row_cost=0.05';

-- Reset to defaults (empty string)
SET SESSION OPTIMIZER_COSTS = '';

Expected Output Using SQL OPTIMIZER_COSTS

  • Each SET statement immediately updates the in-memory cost constants
  • No rows are returned
  • Subsequent queries in the session (or all new sessions, if GLOBAL) are optimized using the new numbers

Use Cases with SQL OPTIMIZER_COSTS

  • Performance tuning after EXPLAIN shows sub-optimal plans.
  • Testing how different hardware profiles affect optimizer choices.
  • Forcing the optimizer to avoid large temporary tables when RAM is scarce.
  • Encouraging index usage on slow spinning disks by inflating io_block_read_cost.

Common Mistakes with SQL OPTIMIZER_COSTS

  • Forgetting to quote the name=value list.
  • Providing unknown constant names, which raises ER_WRONG_ARGUMENTS.
  • Setting GLOBAL values in production without prior benchmarking.
  • Expecting existing sessions to adopt new GLOBAL settings automatically (they do not).

Related Topics

OPTIMIZER_SWITCH, OPTIMIZER_TRACE, EXPLAIN, ANALYZE FORMAT=JSON

First Introduced In

MySQL 8.0.4

Frequently Asked Questions

What is the OPTIMIZER_COSTS variable in MySQL?

It is a dynamic system variable that lets you override internal cost constants so you can steer the query optimizer toward certain execution strategies.

How can I view the current OPTIMIZER_COSTS values?

Run `SHOW VARIABLES LIKE 'optimizer_costs';` to see the comma-separated list of active constants for your session.

Do SESSION changes override GLOBAL settings?

Yes. A value set with `SET SESSION` applies only to the current connection and overrides the GLOBAL default for that session.

Can I persist OPTIMIZER_COSTS across restarts?

Add the desired `optimizer_costs = "name=value,..."` line to the `mysqld` section of `my.cnf` or `my.ini`, then restart MySQL to make it permanent.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!