11

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?

PeeHaa
  • 71,436
  • 58
  • 190
  • 262

3 Answers3

1

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

Ricardo Martins
  • 5,702
  • 3
  • 40
  • 59
SwaPulAtoR
  • 11
  • 1
  • Hi, I am using magneto-debug, a forked patched version, while Madalin Oprea does not accept any pull requests from a while. Is good, but not exactly for a specific piece of code. Thanks, R – Razvan AVRAMESCU Aug 13 '15 at 11:37
1

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

Community
  • 1
  • 1
Fabian Schmengler
  • 24,155
  • 9
  • 79
  • 111
0

You may simply open the MySQL Log file and monitor the logged queries while requesting the page, If you are using Linux for example, use the command tail -F to view all the changes on the log file in a live way.

Try this for example on debian: tail-F /var/log/mysql.log

Yazid Erman
  • 1,166
  • 1
  • 13
  • 24