Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check all queries executed by a specific code in Magento

Tags:

php

magento

To check all queries by a specific piece of code I am using:

  1. Modify from protected to public the variable $_debug in Varien_Db_Adapter_Pdo_Mysql

  2. Do the same for $_logAllQueries

  3. Add this before code executes:

    $adapter = Mage::getSingleton('core/resource')->getConnection('core_write');
    $adapter->_debug = true;
    $adapter->_logAllQueries = true;
    
  4. Add this after the code

    $adapter->_debug = false;
    $adapter->_logAllQueries = false;
    

    so your final code will look like this:

    $adapter = Mage::getSingleton('core/resource')->getConnection('core_write');
    $adapter->_debug = true;
    $adapter->_logAllQueries = true;
    
    Mage::getModel('catalog/product')->load(1);
    
    $adapter->_debug = false;
    $adapter->_logAllQueries = false;
    

Is there any other more elegant solution?

like image 251
Razvan AVRAMESCU Avatar asked May 22 '15 21:05

Razvan AVRAMESCU


2 Answers

You can use a variation of my answer at How do I print all the queries in Magento?

Instead of activating the profiler globally in local.xml, add these before and after the code you want to test:

$profiler = Mage::getSingleton('core/resource')->getConnection('core_write')->getProfiler();
$profiler->setEnabled(true);

// ...

Mage::log(print_r($profiler->getQueryProfiles(), true), null, 'queries.log', true);
$profiler->setEnabled(false);

Then you will find the queries in var/log/queries.log

like image 160
Fabian Schmengler Avatar answered Sep 19 '22 13:09

Fabian Schmengler


There is an extension available for Magento which i am personally using from long time (works for Community as well as EE) :

https://github.com/madalinoprea/magneto-debug

This will let you - Check all requests - See all SQL queries executed for a request - See all layout handles executed - and many more

like image 34
SwaPulAtoR Avatar answered Sep 20 '22 13:09

SwaPulAtoR