[aur-dev] [PATCH 2/2] Use sane ORDER BY clauses in package list queries

Dan McGee dan at archlinux.org
Wed Jun 22 11:04:11 EDT 2011

We were doing some silly things here with an "ORDER BY Name, CategoryID"
clause, due to the fact that Name is unique, and thus any additional
ordering after Name will have no effect. Of course, the dumb as a box of
rocks MySQL query optimizer doesn't realize this, leading to full table
scans every time of ~30000 packages instead of using index scans for the
ordering and only retrieving the first 50 rows.

The biggest change is noted in the default sort order as it cuts down
the columns to one, but we can remove the redundant sort from other
orderings as well, even though those will still not be able to use an
index-driven query plan.

Signed-off-by: Dan McGee <dan at archlinux.org>

This will directly address 2 of the top 5 queries in the sigurd MySQL slow
query log.

 web/lib/pkgfuncs.inc.php |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/web/lib/pkgfuncs.inc.php b/web/lib/pkgfuncs.inc.php
index 46768f2..8cd1c61 100644
--- a/web/lib/pkgfuncs.inc.php
+++ b/web/lib/pkgfuncs.inc.php
@@ -472,23 +472,23 @@ function pkg_search_page($SID="") {
 	$order = (isset($_GET["SO"]) && $_GET["SO"] == 'd') ? 'DESC' : 'ASC';
-	$q_sort = "ORDER BY Name ".$order.", CategoryID DESC ";
+	$q_sort = "ORDER BY Name ".$order." ";
 	$sort_by = isset($_GET["SB"]) ? $_GET["SB"] : '';
 	switch ($sort_by) {
 	case 'c':
 		$q_sort = "ORDER BY CategoryID ".$order.", Name ASC ";
 	case 'v':
-		$q_sort = "ORDER BY NumVotes ".$order.", Name ASC, CategoryID DESC ";
+		$q_sort = "ORDER BY NumVotes ".$order.", Name ASC ";
 	case 'w':
 		if ($SID) {
-			$q_sort = "ORDER BY Voted ".$order.", Name ASC, CategoryID DESC ";
+			$q_sort = "ORDER BY Voted ".$order.", Name ASC ";
 	case 'o':
 		if ($SID) {
-			$q_sort = "ORDER BY Notify ".$order.", Name ASC, CategoryID DESC ";
+			$q_sort = "ORDER BY Notify ".$order.", Name ASC ";
 	case 'm':

More information about the aur-dev mailing list