Formatting/coloring log files on the fly with tail and sed

I often monitor MySQL queries for some complex application components that use an ORM (Doctrine).
I’ve just made a special logger that re-runs all the quries with an EXPLAIN and log the “Extra” column of the explain query, in order to spot queries that can be improved. From the log, I pipe the result to “sed” with a regular expression, and colouring some text.
Sure you are not interested in this post. I just write some posts in order to find them easily in the future when I need them, and possibly helpting somebody else googling the same need.
Here the lines I’ve added to my ~/.bashrc
COLOR_RED=`echo -en 'e[31m'`
COLOR_YELLOW=`echo -en 'e[93m'`
COLOR_RESET=`echo -en 'e[00m'`
alias querylog="tail -f /var/www/bnt/Public/application/logs/queries.log | sed -E 
's/(where|intersect|temp|filesort)/$COLOR_RED1$COLOR_RESET/g'"

When NOT to use a mysql subquery

MySQL optimizer rewrites uncorrelated subqueries (when the inner query does NOT use fields of the outer query) as correlated query, with perrformances of quadratic order rather than linear.

If the inner query does not use a WHERE with fields on the outer query, rewrite it as a JOIN

Source: MySQL :: MySQL 5.5 Reference Manual :: E.4 Restrictions on Subqueries

The problem is that, for a statement that uses an IN subquery, the optimizer rewrites it as a correlated subquery. Consider the following statement that uses an uncorrelated subquery:
SELECT … FROM t1 WHERE t1.a IN (SELECT b FROM t2);
The optimizer rewrites the statement to a correlated subquery:
SELECT … FROM t1 WHERE EXISTS (SELECT 1 FROM t2 WHERE t2.b = t1.a);
If the inner and outer queries return M and N rows, respectively, the execution time becomes on the order of O(M×N), rather than O(M+N) as it would be for an uncorrelated subquery.

PHPUnit: Assert/mock a method for consecutive calls

I’m testing a class that is calling a method twice and with different parameters. I want to assert that this method (addSourceLinkAndUpdateCounts) is called twice and with different argument for each call.

The matcher to use is the “InvokedAtIndex”. See PHPUnit 3.6 documentation

Following, an example (the method addSourceLinkAndUpdateCounts is mocked and I assert that is called twice. The first time with arguments (http://www.link1.com, 12), and the second time with arguments (http://www.link2.com, 12)

// assert save function is called twice and with the two args
        $this->object
             ->expects($this->exactly(2))
             ->method('addSourceLinkAndUpdateCounts')
        ;

        // assert calls return the two links
        $this->object
             ->expects($this->at(1))
             ->method('addSourceLinkAndUpdateCounts')
             ->with('http://www.link1.com', 12)
        ;

        $this->object
             ->expects($this->at(2))
             ->method('addSourceLinkAndUpdateCounts')
             ->with('http://www.link2.com', 12)
        ;

Raw query on Zend framework

Zend_Db_Table_Abstract::getDefaultAdapter()->query($sql);
Zend_Db_Table_Abstract::getDefaultAdapter()->query($sql)->fetch(); //if you need to fetch results

if it does not work, use the db handler Pdo), and call – for example – exec()

Zend_Db_Table_Abstract::getDefaultAdapter()->getConnection()->exec($sql);