Skip to content

The Explain Plan Plugin

Gavin Towey edited this page Aug 15, 2014 · 5 revisions

Anemometer has the abitlity to generate the EXPLAIN plan, and CREATE TABLE statements when viewing the query details.

However, it needs to have the correct database to which to connect so it can run the EXPLAIN and find the tables. By default Anemometer is configured with a generic function to retrieve this information, but this may not work in every environment. If you are getting errors about generating explain plans then this section should help you understand how to configure it properly for you.

Inside the config.inc.php

Part of the configuration file contains a block which looks like this:

$conf['plugins'] = array(
 
	'show_create'	=> true,
	'show_status'	=> true,
	'explain'		=> function ($sample) {
		$conn = array();
 
		if (!array_key_exists('hostname_max',$sample) 
			or strlen($sample['hostname_max']) < 5)
		{
			return;
		}

 		$parts = preg_split("/:/", $sample['hostname_max']);
		$conn['host'] = $parts[0];
		$conn['port'] = $parts[1];
	
		if ($sample['db_max'] != '')
		{
			$conn['db'] = $sample['db_max'];
		}
 
		$conn['user'] = 'root';
		$conn['password'] = '';
 
		return $conn;
	},
);

When you view a query in Anemometer the query details are passed to the plugin as the $sample argument. The function needs to return an array which has the following fields:

$conn = array();
$conn['host']     = 'db.example.com';
$conn['port']     = 3306
$conn['user']     = 'anemometer_explain';
$conn['password'] = 'superSecurePass';

So at a minimum you will have to define the user and password fields in the existing function:

		$conn['user'] = 'anemometer_explain';
		$conn['password'] = 'superSecurePass';

In the above example we've told Anemometer to connect with the user anemometer_explain. You can create this user on your databases which only needs SELECT privilege:

GRANT SELECT ON *.* TO 'anemometer_explain'@'%' IDENTIFIED BY 'superSecurePass';"

Updating The Plugin

To do this you'll have to have some knowledge of php. The basics are that the $sample variable will contain a bunch of information you can use (all the data for that event from the global_query_review_history table):

Array
(
    [hostname_max] => gtowey.local
    [db_max] => test
    [checksum] => 17174117975938943445
    [sample] => select * from a
    [ts_min] => 2014-04-10 15:53:46
    [ts_max] => 2014-04-10 15:53:50
    [ts_cnt] => 8
    ...snip...
)

That's what you have to work with; If the host, port & database settings need to be different from the hostname_max and db_max settings, then you will have to provide a method to retrieve the correct info.