Rensselaer Center for Open Source Software

Faster random SQL query

8 files changed, 32 lines added, 17 lines removed

Changes

--- analysis/training-set/dbinfo_manager.py 978e42aab5051acc0da5a6dc03c9ec9b7c6b2631
+++ analysis/training-set/dbinfo_manager.py 7b5229086e9b9878746014fe82c37ef45b8413aa
@@ -13,10 +13,17 @@
+
-    info["port"] = raw_input("Enter your port number: ")
+
+    port = raw_input("Enter your port number (leave blank for 3306): ")
+    info["port"] = 3306 if port == "" else int(port)
+
-    info["password"] = raw_input("Enter your password: ")
+
+    info["passwd"] = raw_input("Enter your password: ")
+
+    info["db"] = raw_input("Enter your database: ")
--- analysis/training-set/requestjob.py 978e42aab5051acc0da5a6dc03c9ec9b7c6b2631
+++ analysis/training-set/requestjob.py 7b5229086e9b9878746014fe82c37ef45b8413aa
@@ -1,5 +1,5 @@
-import psycopg2
+import MySQLdb
@@ -7,20 +7,24 @@
-  @chrrypy.tools.json_out()
+  @cherrypy.tools.json_out()
-    conn = psycopg2.connect(**self.dbinfo)
+    conn = MySQLdb.connect(**self.dbinfo)
-    #Get the id and the paragraph for all elements with an empty score
-    sql = "SELECT id,para FROM trainset WHERE score IS NULL"
+    sql = "SELECT RAND()*(SELECT count(*) FROM articles WHERE trainset=1)"
+    selection = int(cur.fetchone()[0])
-    #Pick a result
-    selection = radint(0, cur.rowcount-1)
-    result = cur.fetchall()[selection]
+    sql = "SELECT id,title FROM articles WHERE trainset=1 LIMIT 1 OFFSET %s"
+    args = [selection]
+    cur.execute(sql, args)
+
+    result = cur.fetchone()
+    conn.close()
+
--- analysis/training-set/returnjob.py 978e42aab5051acc0da5a6dc03c9ec9b7c6b2631
+++ analysis/training-set/returnjob.py 7b5229086e9b9878746014fe82c37ef45b8413aa
@@ -1,14 +1,14 @@
-import psycopg2
+import MySQLdb
-  @chrrypy.tools.json_out()
-  def index(self, paraid, score):
-    conn = psycopg2.connect(**self.dbinfo)
+  @cherrypy.tools.json_out()
+  def index(self, articleid, score):
+    conn = MySQLdb.connect(**self.dbinfo)
@@ -16,10 +16,14 @@
-    sql = "UPDATE trainset SET score=%s WHERE id=%s"
-    args = score,paraid
+    #sql = "UPDATE trainset SET score=%s WHERE id=%s"
+    sql = "UPDATE articles SET score=%s WHERE id=%s"
+    args = score,articleid
+    conn.commit()
+    conn.close()
+
--- analysis/training-set/server.py 978e42aab5051acc0da5a6dc03c9ec9b7c6b2631
+++ analysis/training-set/server.py 7b5229086e9b9878746014fe82c37ef45b8413aa
@@ -15,7 +15,7 @@
-
+  print dbinfo
Michael
DaBuzz • 61 weeks ago