On my site, I have encoded a function that shows how many unique visitors and how many page views I get per day.
The problem is that sometimes the insert request is executed forever, and there is no DELAYED INSERT function in InnoDB.
Edit: It uses InnoDB, what I mean with a long load time is about 6 seconds instead of 0.1-0.5 seconds. As soon as I delete the registration, the site is much faster.
The $b array contains information about the browser, but according to XHProf, its a PDO request that takes so long.
Insert Code:
$values = array( 'time' => time(), 'ip' => $_SERVER['REMOTE_ADDR'], 'page' => rtrim((isset($_GET['q']) ? $_GET['q'] : 'index'), '/'), 'browser' => $b[11][0] . ' ' . $b[11][1], 'os' => $uos, 'referred' => (isset($_SERVER['HTTP_REFERER']) && !preg_match('|^' . Config::getValue('site', 'url') . '|', $_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''), ); $this->table->insert($values);
and insert function code:
public function insert($table, $data) { ksort($data); $fieldNames = implode('`, `', array_keys($data)); $fieldValues = ':' . implode(', :', array_keys($data)); $sth = $this->prepare('INSERT INTO ' . $table . '(`' . $fieldNames . '`) VALUES (' . $fieldValues . ');'); foreach ($data as $key => $value) { $sth->bindValue(':' . $key, $value); } $sth->execute(); }
source share