diff options
author | Matthieu Aubry <matt@piwik.org> | 2015-05-06 07:40:57 +0300 |
---|---|---|
committer | Matthieu Aubry <matt@piwik.org> | 2015-05-06 07:40:57 +0300 |
commit | d1d7855dc49f4bb7b5a34bdeb587c99690cadc3d (patch) | |
tree | d4980a6acbbbed116d3d739cffdfef948eada3fa | |
parent | 1c528afcc2fc4aa99ed93029bf27c32295e92d41 (diff) | |
parent | ea1045a0ac5f14bded2acb90539bae656ac460d5 (diff) |
Merge pull request #7820 from piwik/7779
Tracker: Faster visitor recognition
-rw-r--r-- | core/Tracker/Model.php | 88 |
1 files changed, 40 insertions, 48 deletions
diff --git a/core/Tracker/Model.php b/core/Tracker/Model.php index bb4fc1075a..ce725db995 100644 --- a/core/Tracker/Model.php +++ b/core/Tracker/Model.php @@ -333,58 +333,50 @@ class Model $idSite ); - if ($shouldMatchOneFieldOnly) { - if ($isVisitorIdToLookup) { - $whereCommon .= ' AND idvisitor = ?'; - $bindSql[] = $idVisitor; - } else { - $whereCommon .= ' AND config_id = ?'; - $bindSql[] = $configId; + if ($shouldMatchOneFieldOnly && $isVisitorIdToLookup) { + + $visitRow = $this->findVisitorByVisitorId($idVisitor, $select, $from, $whereCommon, $bindSql); + + } elseif ($shouldMatchOneFieldOnly) { + + $visitRow = $this->findVisitorByConfigId($configId, $select, $from, $whereCommon, $bindSql); + + } else { + + $visitRow = $this->findVisitorByVisitorId($idVisitor, $select, $from, $whereCommon, $bindSql); + + if (empty($visitRow)) { + $whereCommon .= ' AND user_id IS NULL '; + $visitRow = $this->findVisitorByConfigId($configId, $select, $from, $whereCommon, $bindSql); } + } - $sql = "$select $from - WHERE " . $whereCommon . " - ORDER BY visit_last_action_time DESC - LIMIT 1"; - } // We have a config_id AND a visitor_id. We match on either of these. - // Why do we also match on config_id? - // we do not trust the visitor ID only. Indeed, some browsers, or browser addons, - // cause the visitor id from the 1st party cookie to be different on each page view! - // It is not acceptable to create a new visit every time such browser does a page view, - // so we also backup by searching for matching config_id. - // We use a UNION here so that each sql query uses its own INDEX - else { - // will use INDEX index_idsite_config_datetime (idsite, config_id, visit_last_action_time) - $where = ' AND config_id = ? AND user_id IS NULL '; - $bindSql[] = $configId; - $sqlConfigId = "$select , - 0 as priority - $from - WHERE $whereCommon $where - ORDER BY visit_last_action_time DESC - LIMIT 1 - "; - // will use INDEX index_idsite_idvisitor (idsite, idvisitor) - $bindSql[] = $timeLookBack; - $bindSql[] = $timeLookAhead; - $bindSql[] = $idSite; - $where = ' AND idvisitor = ?'; - $bindSql[] = $idVisitor; - $sqlVisitorId = "$select , - 1 as priority - $from - WHERE $whereCommon $where + return $visitRow; + } + + private function findVisitorByVisitorId($idVisitor, $select, $from, $where, $bindSql) + { + // will use INDEX index_idsite_idvisitor (idsite, idvisitor) + $where .= ' AND idvisitor = ?'; + $bindSql[] = $idVisitor; + + return $this->fetchVisitor($select, $from, $where, $bindSql); + } + + private function findVisitorByConfigId($configId, $select, $from, $where, $bindSql) + { + // will use INDEX index_idsite_config_datetime (idsite, config_id, visit_last_action_time) + $where .= ' AND config_id = ?'; + $bindSql[] = $configId; + + return $this->fetchVisitor($select, $from, $where, $bindSql); + } + + private function fetchVisitor($select, $from, $where, $bindSql) + { + $sql = "$select $from WHERE " . $where . " ORDER BY visit_last_action_time DESC - LIMIT 1 - "; - - // We join both queries and favor the one matching the visitor_id if it did match - $sql = " ( $sqlConfigId ) - UNION - ( $sqlVisitorId ) - ORDER BY priority DESC LIMIT 1"; - } $visitRow = $this->getDb()->fetch($sql, $bindSql); |