Exemple #1
0
// Loop through the queries. Run the optimizer, and compare the resulting query
//  with what we expect. Also run both queries and test the time needed to run
//  each; the modified query should be faster. Also the number of results should
//  be the same.
// We need the object, but we really don't care which one.
require ROOT_PATH . 'class/object_users.php';
$o = new LOVD_User();
$i = 0;
foreach ($aSQL as $sSQLInput => $sSQLExpectedOutput) {
    // Query counter, starting at 1.
    $i++;
    // Check if the input query indeed still has SQL_CALC_FOUND_ROWS, otherwise
    //  we can't test.
    assert("strpos('{$sSQLInput}', 'SQL_CALC_FOUND_ROWS') !== false");
    // Check if outcome is as expected.
    $sSQLOutput = $o->getRowCountForViewList(lovd_splitSQL($sSQLInput), array(), true);
    assert("'{$sSQLOutput}' == '{$sSQLExpectedOutput}'");
    // If we're here, the output was as expected. Now run both queries, and time
    //  them. Because timing may vary, we'll run it a maximum of 5 times if it's
    //  not faster than the original. If after 5 tries it's still not, then
    //  we'll bail out.
    $nTries = 0;
    do {
        $nTries++;
        // Starts at 1.
        $t = microtime(true);
        $_DB->query(preg_replace('/^SELECT /', 'SELECT SQL_NO_CACHE ', $sSQLInput));
        $nFoundInput = $_DB->query('SELECT FOUND_ROWS()')->fetchColumn();
        $tSQLInput = microtime(true) - $t;
        $t = microtime(true);
        if (strpos($sSQLOutput, 'SQL_CALC_FOUND_ROWS') !== false) {