Przeglądaj źródła

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.
Alexandre Alapetite 12 lat temu
rodzic
commit
3f93866f38
3 zmienionych plików z 22 dodań i 29 usunięć
  1. 1 1
      app/layout/aside_flux.phtml
  2. 20 27
      app/models/Entry.php
  3. 1 1
      public/install.php

+ 1 - 1
app/layout/aside_flux.phtml

@@ -24,7 +24,7 @@
 			<div class="category favorites">
 				<a data-unread="<?php echo $this->nb_favorites['unread']; ?>" class="btn<?php echo $this->get_c == 'favoris' ? ' active' : ''; ?>" href="<?php echo _url ('index', 'index', 'get', 'favoris'); ?>">
 					<i class="icon i_bookmark"></i>
-					<?php echo Translate::t ('favorite_feeds', $this->nb_favorites['read'] + $this->nb_favorites['unread']); ?>
+					<?php echo Translate::t ('favorite_feeds', $this->nb_favorites['all']); ?>
 				</a>
 			</div>
 		</li>

+ 20 - 27
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() {

+ 1 - 1
public/install.php

@@ -24,7 +24,7 @@ define ('SQL_REQ_FEED', 'CREATE TABLE IF NOT EXISTS `%sfeed` (
   `category` char(6) DEFAULT \'000000\',
   `name` varchar(255) NOT NULL,
   `website` varchar(255) NOT NULL,
-  `description` varchar(1023) NOT NULL,
+  `description` text NOT NULL,
   `lastUpdate` int(11) NOT NULL,
   `priority` tinyint(2) NOT NULL DEFAULT \'10\',
   `pathEntries` varchar(511) DEFAULT NULL,