diff --git a/lib/db.php b/lib/db.php
index 74e7ca5b0e0957db219ba60f71a49e528cd1d265..5224d5ee7da46ebfc4d34cc55690cbb7b9a222ff 100644
--- a/lib/db.php
+++ b/lib/db.php
@@ -41,6 +41,8 @@ class OC_DB {
 	const BACKEND_PDO=0;
 	const BACKEND_MDB2=1;
 
+	static private $preparedQueries = array();
+
 	/**
 	 * @var MDB2_Driver_Common
 	 */
@@ -121,6 +123,7 @@ class OC_DB {
 				return true;
 			}
 		}
+		self::$preparedQueries = array();
 		// The global data we need
 		$name = OC_Config::getValue( "dbname", "owncloud" );
 		$host = OC_Config::getValue( "dbhost", "" );
@@ -201,6 +204,7 @@ class OC_DB {
 				return true;
 			}
 		}
+		self::$preparedQueries = array();
 		// The global data we need
 		$name = OC_Config::getValue( "dbname", "owncloud" );
 		$host = OC_Config::getValue( "dbhost", "" );
@@ -321,7 +325,12 @@ class OC_DB {
 					$query.=$limitsql;
 				}
 			}
+		} else {
+			if (isset(self::$preparedQueries[$query])) {
+				return self::$preparedQueries[$query];
+			}
 		}
+		$rawQuery = $query;
 
 		// Optimize the query
 		$query = self::processQuery( $query );
@@ -343,6 +352,9 @@ class OC_DB {
 			}
 			$result=new PDOStatementWrapper($result);
 		}
+		if (is_null($limit) || $limit == -1) {
+			self::$preparedQueries[$rawQuery] = $result;
+		}
 		return $result;
 	}
 
@@ -588,7 +600,7 @@ class OC_DB {
 				error_log('DB error: '.$entry);
 				OC_Template::printErrorPage( $entry );
 			}
-			
+
 			if($result->numRows() == 0) {
 				$query = 'INSERT INTO "' . $table . '" ("'
 					. implode('","', array_keys($input)) . '") VALUES("'
@@ -623,7 +635,7 @@ class OC_DB {
 
 		return $result->execute();
 	}
-	
+
 	/**
 	 * @brief does minor changes to query
 	 * @param string $query Query string