diff options
| author | 2013-09-14 22:12:44 +0200 | |
|---|---|---|
| committer | 2013-09-14 22:12:44 +0200 | |
| commit | 6f8b260ebf1768fdf567a03989556b50c9cbda25 (patch) | |
| tree | 28b6e1545518adfa216396aa8a09a4de8d736629 /app/models/Entry.php | |
| parent | 6525779349ba915e17ef1846948c861bfa36425e (diff) | |
| parent | e19695e14bdf3ea1baf04141f346060751eb1789 (diff) | |
Merge branch 'Alkarex-SqlOptimisation' into sql-optimisation
Diffstat (limited to 'app/models/Entry.php')
| -rwxr-xr-x | app/models/Entry.php | 100 |
1 files changed, 50 insertions, 50 deletions
diff --git a/app/models/Entry.php b/app/models/Entry.php index 647555ae9..d73ce7245 100755 --- a/app/models/Entry.php +++ b/app/models/Entry.php @@ -392,12 +392,15 @@ class EntryDAO extends Model_pdo { } } - public function listWhere ($where, $state, $order, $values = array ()) { + private function listWhere ($where, $state, $order, $limitFromId = '', $limitCount = '', $values = array ()) { if ($state == 'not_read') { $where .= ' AND is_read = 0'; } elseif ($state == 'read') { $where .= ' AND is_read = 1'; } + if (!empty($limitFromId)) { //TODO: Consider using LPAD(e.date, 11) //CONCAT is for cases when many entries have the same date + $where .= ' AND CONCAT(e.date, e.id) ' . ($order === 'low_to_high' ? '<=' : '>=') . ' (SELECT CONCAT(s.date, s.id) from freshrss_entry s WHERE s.id = "' . $limitFromId . '")'; + } if ($order == 'low_to_high') { $order = ' DESC'; @@ -407,78 +410,75 @@ class EntryDAO extends Model_pdo { $sql = 'SELECT e.* FROM ' . $this->prefix . 'entry e' . ' INNER JOIN ' . $this->prefix . 'feed f ON e.id_feed = f.id' . $where - . ' ORDER BY date' . $order; + . ' ORDER BY e.date' . $order . ', e.id' . $order; + + if (!empty($limitCount)) { + $sql .= ' LIMIT ' . ($limitCount + 2); //TODO: See http://explainextended.com/2009/10/23/mysql-order-by-limit-performance-late-row-lookups/ + } + $stm = $this->bd->prepare ($sql); $stm->execute ($values); return HelperEntry::daoToEntry ($stm->fetchAll (PDO::FETCH_ASSOC)); } - public function listEntries ($state, $order = 'high_to_low') { - return $this->listWhere (' WHERE priority > 0', $state, $order); + public function listEntries ($state, $order = 'high_to_low', $limitFromId = '', $limitCount = '') { + return $this->listWhere (' WHERE priority > 0', $state, $order, $limitFromId, $limitCount); } - public function listFavorites ($state, $order = 'high_to_low') { - return $this->listWhere (' WHERE is_favorite = 1', $state, $order); + public function listFavorites ($state, $order = 'high_to_low', $limitFromId = '', $limitCount = '') { + return $this->listWhere (' WHERE is_favorite = 1', $state, $order, $limitFromId, $limitCount); } - public function listPublic ($state, $order = 'high_to_low') { - return $this->listWhere (' WHERE is_public = 1', $state, $order); + public function listPublic ($state, $order = 'high_to_low', $limitFromId = '', $limitCount = '') { + return $this->listWhere (' WHERE is_public = 1', $state, $order, $limitFromId, $limitCount); } - public function listByCategory ($cat, $state, $order = 'high_to_low') { - return $this->listWhere (' WHERE category = ?', $state, $order, array ($cat)); + public function listByCategory ($cat, $state, $order = 'high_to_low', $limitFromId = '', $limitCount = '') { + return $this->listWhere (' WHERE category = ?', $state, $order, $limitFromId, $limitCount, array ($cat)); } - public function listByFeed ($feed, $state, $order = 'high_to_low') { - return $this->listWhere (' WHERE id_feed = ?', $state, $order, array ($feed)); + public function listByFeed ($feed, $state, $order = 'high_to_low', $limitFromId = '', $limitCount = '') { + return $this->listWhere (' WHERE id_feed = ?', $state, $order, $limitFromId, $limitCount, array ($feed)); } - public function count () { - $sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'entry e INNER JOIN ' . $this->prefix . 'feed f ON e.id_feed = f.id WHERE priority > 0'; + public function countUnreadRead () { + $sql = 'SELECT is_read, COUNT(*) AS count FROM ' . $this->prefix . 'entry e INNER JOIN ' . $this->prefix . 'feed f ON e.id_feed = f.id WHERE priority > 0 GROUP BY is_read'; $stm = $this->bd->prepare ($sql); $stm->execute (); $res = $stm->fetchAll (PDO::FETCH_ASSOC); - return $res[0]['count']; + $readUnread = array('unread' => 0, 'read' => 0); + foreach ($res as $line) { + switch (intval($line['is_read'])) { + case 0: $readUnread['unread'] = intval($line['count']); break; + case 1: $readUnread['read'] = intval($line['count']); break; + } + } + return $readUnread; } - public function countNotRead () { - $sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'entry e INNER JOIN ' . $this->prefix . 'feed f ON e.id_feed = f.id WHERE is_read=0 AND priority > 0'; - $stm = $this->bd->prepare ($sql); - $stm->execute (); - $res = $stm->fetchAll (PDO::FETCH_ASSOC); - - return $res[0]['count']; + public function count () { //Deprecated: use countUnreadRead() instead + $unreadRead = $this->countUnreadRead (); //This makes better use of caching + return $unreadRead['unread'] + $unreadRead['read']; } - - public function countNotReadByFeed ($id) { - $sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'entry WHERE is_read = 0 AND id_feed = ?'; - $stm = $this->bd->prepare ($sql); - $stm->execute (array ($id)); - $res = $stm->fetchAll (PDO::FETCH_ASSOC); - - return $res[0]['count']; - } - - public function countNotReadByCat ($id) { - $sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'entry e INNER JOIN ' . $this->prefix . 'feed f ON e.id_feed = f.id WHERE is_read=0 AND category = ?'; - $stm = $this->bd->prepare ($sql); - $stm->execute (array ($id)); - $res = $stm->fetchAll (PDO::FETCH_ASSOC); - - return $res[0]['count']; + public function countNotRead () { //Deprecated: use countUnreadRead() instead + $unreadRead = $this->countUnreadRead (); //This makes better use of caching + return $unreadRead['unread']; } - public function countNotReadFavorites () { - $sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'entry WHERE is_read=0 AND is_favorite=1'; + public function countUnreadReadFavorites () { + $sql = 'SELECT is_read, COUNT(*) AS count FROM ' . $this->prefix . 'entry WHERE is_favorite=1 GROUP BY is_read'; $stm = $this->bd->prepare ($sql); $stm->execute (); $res = $stm->fetchAll (PDO::FETCH_ASSOC); - - return $res[0]['count']; + $readUnread = array('unread' => 0, 'read' => 0); + foreach ($res as $line) { + switch (intval($line['is_read'])) { + case 0: $readUnread['unread'] = intval($line['count']); break; + case 1: $readUnread['read'] = intval($line['count']); break; + } + } + return $readUnread; } - public function countFavorites () { - $sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'entry WHERE is_favorite=1'; - $stm = $this->bd->prepare ($sql); - $stm->execute (); - $res = $stm->fetchAll (PDO::FETCH_ASSOC); - return $res[0]['count']; + public function countFavorites () { //Deprecated: use countUnreadReadFavorites() instead + $unreadRead = $this->countUnreadReadFavorites (); //This makes better use of caching + return $unreadRead['unread'] + $unreadRead['read']; } public function optimizeTable() { @@ -522,7 +522,7 @@ class HelperEntry { $list[$key] = self::createEntry ($dao); $count++; - $first_is_found = true; + $first_is_found = true; //TODO: Update: Now done in SQL } if ($count >= self::$nb) { $break_after = true; |
