HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlModerate

Is there any way to trace optimizer's work in MySQL?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
anywaymysqlworkoptimizertheretrace

Problem

Just like the MEMO structure in SQL Server which is kind of a "paper trail" of steps the optimizer takes in optimizing the query. Is there anything in MySQL through which I can get the information like, which plans the optimizer considers, the cost of each plan?

Solution

The recent 5.6 version has added this feature.

See: MySQL Internals Manual ::chapter 9. Tracing the Optimizer

Typical Usage:

# Turn tracing on (it's off by default):
SET optimizer_trace="enabled=on";

SELECT ...; # your query here

SELECT * FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;

# possibly more queries...

# When done with tracing, disable it:
SET optimizer_trace="enabled=off";


You can see it in action in SQL-Fiddle. Not sure how the output can be beautified.

Code Snippets

# Turn tracing on (it's off by default):
SET optimizer_trace="enabled=on";

SELECT ...; # your query here

SELECT * FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;

# possibly more queries...

# When done with tracing, disable it:
SET optimizer_trace="enabled=off";

Context

StackExchange Database Administrators Q#42602, answer score: 10

Revisions (0)

No revisions yet.