Visual EXPLAIN for MySQL and Laravel
Published on by Paul Redmond
The MySQL Visual Explain tool by Tobias Petry helps users analyze slow queries by providing an easy-to-understand visual representation of MySQL’s EXPLAIN output. This tool enables you to decipher MySQL’s default, often cryptic EXPLAIN output, making it accessible even for those without deep database expertise, providing an excellent visualization of query performance:
The MySQL Visual Explain website has an accompanying API and an integration for laravel. The Laravel package adds methods to the query builder with various options such as automatically running a visual explain and providing a link, dumping the visual explain, or outputting the URL and stopping execution:
// $url will be e.g. https://mysqlexplain.com/explain/01j2gcrbsjet9r8rav114vgfsy$url = Film::where('description', 'like', '%astronaut%') ->visualExplain(); // URL to EXPLAIN will be printed to screen$users = Film::where('description', 'like', '%astronaut%') ->dumpVisualExplain() ->get(); // URL to EXPLAIN will be printed to screen & execution is stoppedFilm::where('description', 'like', '%astronaut%') ->ddVisualExplain(); // Submit raw queriesuse Tpetry\MysqlExplain\Facades\MysqlExplain;$url = MysqlExplain::submitQuery( DB::connection('mysql'), 'SELECT * FROM actor WHERE first_name = ?', ['PENEL\'OPE'],);
For more information, you can visit MySQL Visual Explain.