diff options
| author | 2013-11-12 18:49:18 +0100 | |
|---|---|---|
| committer | 2013-11-12 18:49:18 +0100 | |
| commit | 3f93866f38aff6b9f10d57472bf0ccf16d268499 (patch) | |
| tree | 47fee089a314541e57967413853a8d9f8fc0c8ae /app/models/Entry.php | |
| parent | 14342603a05b64294762c45798debe3a66db26eb (diff) | |
SQL : Optimisation vitesse
Optimisation de la vitesse de quelques requêtes, surtout après les
essais d'INDEX.
Suite de https://github.com/marienfressinaud/FreshRSS/issues/254
Note pour plus tard : il faudra supprimer les INDEX inutilisés.
Diffstat (limited to 'app/models/Entry.php')
| -rwxr-xr-x | app/models/Entry.php | 47 |
1 files changed, 20 insertions, 27 deletions
diff --git a/app/models/Entry.php b/app/models/Entry.php index d03c3a3c1..5c6b379d8 100755 --- a/app/models/Entry.php +++ b/app/models/Entry.php @@ -449,42 +449,35 @@ class EntryDAO extends Model_pdo { } 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'; + $sql = 'SELECT COUNT(e.id) AS count FROM ' . $this->prefix . 'entry e INNER JOIN ' . $this->prefix . 'feed f ON e.id_feed = f.id WHERE priority > 0' + . ' UNION SELECT COUNT(e.id) AS count FROM ' . $this->prefix . 'entry e INNER JOIN ' . $this->prefix . 'feed f ON e.id_feed = f.id WHERE priority > 0 AND is_read = 0'; $stm = $this->bd->prepare ($sql); $stm->execute (); - $res = $stm->fetchAll (PDO::FETCH_ASSOC); - - $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; + $res = $stm->fetchAll (PDO::FETCH_COLUMN, 0); + return array('total' => $res[0], 'unread' => $res[1], 'read' => $res[0] - $res[1]); } - public function count () { //Deprecated: use countUnreadRead() instead - $unreadRead = $this->countUnreadRead (); //This makes better use of caching - return $unreadRead['unread'] + $unreadRead['read']; + public function count () { + $sql = 'SELECT COUNT(e.id) AS count FROM ' . $this->prefix . 'entry e INNER JOIN ' . $this->prefix . 'feed f ON e.id_feed = f.id WHERE priority > 0'; + $stm = $this->bd->prepare ($sql); + $stm->execute (); + $res = $stm->fetchAll (PDO::FETCH_COLUMN, 0); + return $res[0]; } - public function countNotRead () { //Deprecated: use countUnreadRead() instead - $unreadRead = $this->countUnreadRead (); //This makes better use of caching - return $unreadRead['unread']; + public function countNotRead () { + $sql = 'SELECT COUNT(e.id) AS count FROM ' . $this->prefix . 'entry e INNER JOIN ' . $this->prefix . 'feed f ON e.id_feed = f.id WHERE priority > 0 AND is_read = 0'; + $stm = $this->bd->prepare ($sql); + $stm->execute (); + $res = $stm->fetchAll (PDO::FETCH_COLUMN, 0); + return $res[0]; } public function countUnreadReadFavorites () { - $sql = 'SELECT is_read, COUNT(*) AS count FROM ' . $this->prefix . 'entry WHERE is_favorite=1 GROUP BY is_read'; + $sql = 'SELECT COUNT(id) FROM ' . $this->prefix . 'entry WHERE is_favorite=1' + . ' UNION SELECT COUNT(id) FROM ' . $this->prefix . 'entry WHERE is_favorite=1 AND is_read = 0'; $stm = $this->bd->prepare ($sql); $stm->execute (); - $res = $stm->fetchAll (PDO::FETCH_ASSOC); - $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; + $res = $stm->fetchAll (PDO::FETCH_COLUMN, 0); + return array('all' => $res[0], 'unread' => $res[1], 'read' => $res[0] - $res[1]); } public function optimizeTable() { |
