/** * @param string $statement * @return mixed */ public function query($statement) { # Execute $start = microtime(true); $result = call_user_func_array('parent::query', func_get_args()); $time = microtime(true) - $start; # Explain $explain = new Explain($this, $statement); # Log if (!preg_match('/^\\s*(EXPLAIN)\\s+/i', $statement)) { $this->logger->info('query', array('queryString' => $statement, 'parameters' => array(), 'time' => $time, 'explain' => $explain->getResult(), 'backtrace' => debug_backtrace())); } # Return return $result; }
/** * @param array $parameters */ public function execute($parameters = array()) { # Execute $start = microtime(true); $result = $this->sth->execute($parameters); $time = microtime(true) - $start; # Explain $explain = new Explain($this->pdo, $this->sth->queryString, $parameters); # Log if (!preg_match('/^\\s*(EXPLAIN)\\s+/i', $this->sth->queryString)) { $this->logger->info('prepare', array('queryString' => $this->sth->queryString, 'parameters' => $parameters, 'time' => $time, 'explain' => $explain->getResult(), 'backtrace' => debug_backtrace())); } # Return return $result; }
public function __construct(MonitorDbConnection $connection, $query = null) { parent::__construct($connection, $query); $this->_connection = $connection; $this->reft = new ReflectionClass("CDbCommand"); $this->explainFilter = Explain::createNoticeFunc($connection->monitorFilter); }
/////////////////////////////////////////////////////////////////////////////////////////////// $result = ["select_type" => "PRIMARY", "type" => "range", "possible_keys" => 1, "key" => 1, "rows" => 1000, "Extra" => "Using filesort"]; // type级别大于 range,扫描表行数大于999 $conf = ["and" => ["type" => "index_subquery", "rows" => 999]]; assert(Explain::ifNotice($result, $conf) === true); $conf = ["and" => ["type" => "index_subquery", "rows" => 1001]]; assert(Explain::ifNotice($result, $conf) === false); $conf = ["and" => ["type" => "index", "rows" => 999]]; assert(Explain::ifNotice($result, $conf) === false); $conf = ["or" => ["type" => "index_subquery", "rows" => 1001]]; assert(Explain::ifNotice($result, $conf) === true); $conf = ["or" => ["type" => "index", "rows" => 999]]; assert(Explain::ifNotice($result, $conf) === true); $conf = ["or" => ["type" => "index", "rows" => 1001]]; assert(Explain::ifNotice($result, $conf) === false); $conf = ["key" => 0, "or" => ["type" => "index", "rows" => 999]]; assert(Explain::ifNotice($result, $conf) === false); assert(Explain::ifNotice($result, $conf, "or") === true); $conf = ["key" => 2, "or" => ["type" => "index", "rows" => 1001, "and" => ["select_type" => "PRIMARY, UNCACHEABLE SUBQUERY", "Extra" => "Using filesort,Using temporary"]]]; assert(Explain::ifNotice($result, $conf, "or") === true); $conf = ["type" => "index_subquery", "rows" => 1001]; assert(Explain::ifNotice($result, $conf, "and") === false); assert(Explain::ifNotice($result, $conf, "or") === true); $conf = ["key" => 2, "or" => ["type" => "index", "rows" => 1001, "and" => ["select_type" => "PRIMARY, UNCACHEABLE SUBQUERY", "Extra" => "Using temporary"]]]; assert(Explain::ifNotice($result, $conf) === false); $conf = ["key" => 2, "or" => ["type" => "index_subquery", "rows" => 1001, "and" => ["select_type" => "PRIMARY, UNCACHEABLE SUBQUERY", "Extra" => "Using temporary"]]]; assert(Explain::ifNotice($result, $conf) === true); $conf = ["key" => 2, "or" => ["type" => "index", "rows" => 1001, "and" => ["Extra" => "Using temporary", "select_type" => "UN_EXIST"]]]; assert(Explain::ifNotice($result, $conf) === false); assert(Explain::ifNotice($result, $conf, "or") === true);