From c34bebf42859019998c7ff3c03b570295ae83ebe Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Wed, 2 Mar 2011 08:35:39 -0600 Subject: Perform a second query to find total search count This removes the need for SQL_CALC_FOUND_ROWS which can really slow down queries in a lot of cases. The COUNT(*) query we end up performing can reuse a lot of the original clauses from our primary query, but we can really slim it up by omitting some joins and the sorting/limiting clauses. Signed-off-by: Dan McGee Signed-off-by: Lukas Fleischer --- web/lib/pkgfuncs.inc | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/web/lib/pkgfuncs.inc b/web/lib/pkgfuncs.inc index e1505e7..d5e0771 100644 --- a/web/lib/pkgfuncs.inc +++ b/web/lib/pkgfuncs.inc @@ -434,7 +434,7 @@ function pkg_search_page($SID="") { // build the package search query // - $q_select = "SELECT SQL_CALC_FOUND_ROWS "; + $q_select = "SELECT "; if ($SID) { $q_select .= "CommentNotify.UserID AS Notify, PackageVotes.UsersID AS Voted, "; @@ -445,15 +445,18 @@ function pkg_search_page($SID="") { Packages.ID, Packages.OutOfDateTS "; $q_from = "FROM Packages - LEFT JOIN Users ON (Packages.MaintainerUID = Users.ID) "; + LEFT JOIN Users ON (Packages.MaintainerUID = Users.ID) + LEFT JOIN PackageCategories + ON (Packages.CategoryID = PackageCategories.ID) "; if ($SID) { - $q_from .= "LEFT JOIN PackageVotes + # this portion is not needed for the total row count query + $q_from_extra = "LEFT JOIN PackageVotes ON (Packages.ID = PackageVotes.PackageID AND PackageVotes.UsersID = $myuid) LEFT JOIN CommentNotify ON (Packages.ID = CommentNotify.PkgID AND CommentNotify.UserID = $myuid) "; + } else { + $q_from_extra = ""; } - $q_from .= "LEFT JOIN PackageCategories - ON (Packages.CategoryID = PackageCategories.ID) "; $q_where = "WHERE Packages.DummyPkg = 0 "; // TODO: possibly do string matching on category @@ -530,10 +533,11 @@ function pkg_search_page($SID="") { $q_limit = "LIMIT ".$_GET["O"].", ".$_GET["PP"]; - $q = $q_select . $q_from . $q_where . $q_sort . $q_limit; + $q = $q_select . $q_from . $q_from_extra . $q_where . $q_sort . $q_limit; + $q_total = "SELECT COUNT(*) " . $q_from . $q_where; $result = db_query($q, $dbh); - $total = mysql_result(db_query('SELECT FOUND_ROWS() AS Total', $dbh), 0); + $total = mysql_result(db_query($q_total, $dbh), 0); if ($result && $total > 0) { if (isset($_GET["SO"]) && $_GET["SO"] == "d"){ -- cgit v1.2.3-70-g09d2