[or-cvs] [metrics-web/master 2/2] Improve relay search speed.

karsten at torproject.org karsten at torproject.org
Thu Nov 25 08:47:25 UTC 2010


Author: Karsten Loesing <karsten.loesing at gmx.net>
Date: Thu, 25 Nov 2010 09:42:00 +0100
Subject: Improve relay search speed.
Commit: f077a67936c98578308b7fdc57230b223ceb0198

As weasel pointed out, ordering results by fingerprint doesn't play well
with our indexes. Instead, find the last 31 validafter times of matching
status entries in a subquery and order/limit the results afterwards.
---
 .../torproject/ernie/web/RelaySearchServlet.java   |   50 +++++++++++++-------
 1 files changed, 32 insertions(+), 18 deletions(-)

diff --git a/src/org/torproject/ernie/web/RelaySearchServlet.java b/src/org/torproject/ernie/web/RelaySearchServlet.java
index 10376d0..c5fb242 100644
--- a/src/org/torproject/ernie/web/RelaySearchServlet.java
+++ b/src/org/torproject/ernie/web/RelaySearchServlet.java
@@ -309,13 +309,40 @@ public class RelaySearchServlet extends HttpServlet {
     request.setAttribute("searchNotice", searchNotice);
 
     /* Prepare the query string. */
+    StringBuilder conditionBuilder = new StringBuilder();
+    boolean addAnd = false;
+    if (searchNickname.length() > 0) {
+      conditionBuilder.append((addAnd ? "AND " : "")
+          + "LOWER(nickname) LIKE '" + searchNickname.toLowerCase()
+          + "%' ");
+      addAnd = true;
+    }
+    if (searchFingerprint.length() > 0) {
+      conditionBuilder.append((addAnd ? "AND " : "")
+          + "fingerprint LIKE '" + searchFingerprint.toLowerCase()
+          + "%' ");
+      addAnd = true;
+    }
+    if (searchIPAddress.length() > 0) {
+      conditionBuilder.append((addAnd ? "AND " : "")
+          + "address LIKE '" + searchIPAddress + "%' ");
+      addAnd = true;
+    }
+    for (String search : searchFingerprintOrNickname) {
+      conditionBuilder.append((addAnd ? "AND " : "")
+          + "(LOWER(nickname) LIKE '" + search.toLowerCase()
+          + "%' OR fingerprint LIKE '" + search.toLowerCase() + "%') ");
+      addAnd = true;
+    }
     StringBuilder queryBuilder = new StringBuilder();
     queryBuilder.append("SELECT validafter, descriptor, rawdesc "
+        + "FROM statusentry WHERE validafter IN (SELECT validafter "
         + "FROM statusentry WHERE ");
+    queryBuilder.append(conditionBuilder.toString());
     if (searchDayTimestamps.size() > 0 ||
         searchMonthTimestamps.size() > 0) {
       boolean addOr = false;
-      queryBuilder.append("(");
+      queryBuilder.append("AND (");
       for (long searchTimestamp : searchDayTimestamps) {
         queryBuilder.append((addOr ? "OR " : "") + "(validafter >= '"
             + dateTimeFormat.format(searchTimestamp) + "' AND "
@@ -336,26 +363,13 @@ public class RelaySearchServlet extends HttpServlet {
       }
       queryBuilder.append(") ");
     } else {
-      queryBuilder.append("validafter >= '" + dateTimeFormat.format(
+      queryBuilder.append("AND validafter >= '" + dateTimeFormat.format(
           System.currentTimeMillis() - 30L * 24L * 60L * 60L * 1000L)
           + "' ");
     }
-    if (searchNickname.length() > 0) {
-      queryBuilder.append("AND LOWER(nickname) LIKE '"
-          + searchNickname.toLowerCase() + "%' ");
-    }
-    if (searchFingerprint.length() > 0) {
-      queryBuilder.append("AND fingerprint LIKE '"
-          + searchFingerprint.toLowerCase() + "%' ");
-    }
-    if (searchIPAddress.length() > 0) {
-      queryBuilder.append("AND address LIKE '" + searchIPAddress + "%' ");
-    }
-    for (String search : searchFingerprintOrNickname) {
-      queryBuilder.append("AND (LOWER(nickname) LIKE '"
-          + search.toLowerCase() + "%' OR fingerprint LIKE '"
-          + search.toLowerCase() + "%') ");
-    }
+    queryBuilder.append("GROUP BY validafter ORDER BY validafter DESC "
+        + "LIMIT 31) AND ");
+    queryBuilder.append(conditionBuilder.toString());
     queryBuilder.append("ORDER BY validafter DESC, fingerprint LIMIT 31");
     String query = queryBuilder.toString();
     request.setAttribute("query", query);
-- 
1.7.1



More information about the tor-commits mailing list