ソースを参照

Merge branch 'sqlOptimisation' of https://github.com/Alkarex/FreshRSS into Alkarex-sqlOptimisation

Marien Fressinaud 12 年 前
コミット
94a887f321
3 ファイル変更31 行追加13 行削除
  1. 6 3
      app/models/Category.php
  2. 9 10
      app/models/Feed.php
  3. 16 0
      lib/minz/dao/Model_pdo.php

+ 6 - 3
app/models/Category.php

@@ -11,7 +11,7 @@ class Category extends Model {
 	public function __construct ($name = '', $color = '#0062BE', $feeds = null) {
 		$this->_name ($name);
 		$this->_color ($color);
-		if (!empty($feeds)) {
+		if (isset ($feeds)) {
 			$this->_feeds ($feeds);
 			$this->nbFeed = 0;
 			$this->nbNotRead = 0;
@@ -177,10 +177,13 @@ class CategoryDAO extends Model_pdo {
 
 	public function listCategories ($prePopulateFeeds = true) {
 		if ($prePopulateFeeds) {
-			$sql = 'SELECT c.id as c_id, c.name as c_name, c.color as c_color, count(e.id) as nbNotRead, f.* '
+			$sql = 'SELECT c.id AS c_id, c.name AS c_name, c.color AS c_color, '
+			     . 'COUNT(CASE WHEN e.is_read = 0 THEN 1 END) AS nbNotRead, '
+			     . 'COUNT(e.id) AS nbEntries, '
+			     . 'f.* '
 			     . 'FROM  ' . $this->prefix . 'category c '
 			     . 'LEFT OUTER JOIN ' . $this->prefix . 'feed f ON f.category = c.id '
-			     . 'LEFT OUTER JOIN  ' . $this->prefix . 'entry e ON e.id_feed = f.id AND e.is_read = 0 '
+			     . 'LEFT OUTER JOIN ' . $this->prefix . 'entry e ON e.id_feed = f.id '
 			     . 'GROUP BY f.id '
 			     . 'ORDER BY c.name, f.name';
 			$stm = $this->bd->prepare ($sql);

+ 9 - 10
app/models/Feed.php

@@ -145,10 +145,7 @@ class Feed extends Model {
 		$this->lastUpdate = $value;
 	}
 	public function _priority ($value) {
-		if (!is_int (intval ($value))) {
-			$value = 10;
-		}
-		$this->priority = $value;
+		$this->priority = is_numeric ($value) ? intval ($value) : 10;
 	}
 	public function _pathEntries ($value) {
 		$this->pathEntries = $value;
@@ -173,11 +170,10 @@ class Feed extends Model {
 		$this->keep_history = $value;
 	}
 	public function _nbNotRead ($value) {
-		if (!is_int ($value)) {
-			$value = -1;
+		$this->nbNotRead = is_numeric ($value) ? intval ($value) : -1;
 		}
-
-		$this->nbNotRead = intval ($value);
+	public function _nbEntries ($value) {
+		$this->nbEntries = is_numeric ($value) ? intval ($value) : -1;
 	}
 
 	public function load () {
@@ -472,7 +468,7 @@ class FeedDAO extends Model_pdo {
 		return HelperFeed::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
 	}
 
-	public function count () {
+	public function count () {	//Is this used?
 		$sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'feed';
 		$stm = $this->bd->prepare ($sql);
 		$stm->execute ();
@@ -490,7 +486,7 @@ class FeedDAO extends Model_pdo {
 
 		return $res[0]['count'];
 	}
-	public function countNotRead ($id) {
+	public function countNotRead ($id) {	//Is this used?
 		$sql = 'SELECT COUNT(*) AS count FROM ' . $this->prefix . 'entry WHERE is_read=0 AND id_feed=?';
 		$stm = $this->bd->prepare ($sql);
 		$values = array ($id);
@@ -531,6 +527,9 @@ class HelperFeed {
 			if (isset ($dao['nbNotRead'])) {
 				$list[$key]->_nbNotRead ($dao['nbNotRead']);
 			}
+			if (isset ($dao['nbEntries'])) {
+				$list[$key]->_nbEntries ($dao['nbEntries']);
+			}
 			if (isset ($dao['id'])) {
 				$list[$key]->_id ($dao['id']);
 			}

+ 16 - 0
lib/minz/dao/Model_pdo.php

@@ -9,6 +9,14 @@
  * Seul la connexion MySQL est prise en charge pour le moment
  */
 class Model_pdo {
+
+	/**
+	 * Partage la connexion à la base de données entre toutes les instances.
+	 */
+	public static $useSharedBd = true;
+	private static $sharedBd = null;
+	private static $sharedPrefix;
+
 	/**
 	 * $bd variable représentant la base de données
 	 */
@@ -21,6 +29,12 @@ class Model_pdo {
 	 * HOST, BASE, USER et PASS définies dans le fichier de configuration
 	 */
 	public function __construct () {
+		if (self::$useSharedBd && self::$sharedBd != null) {
+			$this->bd = self::$sharedBd;
+			$this->prefix = self::$sharedPrefix;
+			return;
+		}
+
 		$db = Configuration::dataBase ();
 		$driver_options = null;
 
@@ -46,8 +60,10 @@ class Model_pdo {
 				$db['password'],
 				$driver_options
 			);
+			self::$sharedBd = $this->bd;
 
 			$this->prefix = $db['prefix'];
+			self::$sharedPrefix = $this->prefix;
 		} catch (Exception $e) {
 			throw new PDOConnectionException (
 				$string,