Query improvements

This commit is contained in:
M66B 2017-03-09 07:15:23 +01:00
parent 90875c5805
commit 026d7b95d9
1 changed files with 9 additions and 5 deletions

View File

@ -680,7 +680,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
// There is a segmented index on uid // There is a segmented index on uid
// There is no index on time for write performance // There is no index on time for write performance
String query = "SELECT a.ID AS _id, a.*"; String query = "SELECT a.ID AS _id, a.*";
query += ", (SELECT COUNT(*) FROM dns d WHERE d.resource = (SELECT d1.resource FROM dns d1 WHERE d1.qname = a.daddr)) count"; query += ", (SELECT COUNT(DISTINCT d.qname) FROM dns d WHERE d.resource = (SELECT d1.resource FROM dns d1 WHERE d1.qname = a.daddr)) count";
query += " FROM access a"; query += " FROM access a";
query += " WHERE a.uid = ?"; query += " WHERE a.uid = ?";
query += " ORDER BY a.time DESC"; query += " ORDER BY a.time DESC";
@ -825,7 +825,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
query += " LEFT JOIN access AS a"; query += " LEFT JOIN access AS a";
query += " ON a.daddr = d.qname AND a.uid = " + uid; query += " ON a.daddr = d.qname AND a.uid = " + uid;
query += " WHERE d.resource = '" + ip.replace("'", "''") + "'"; query += " WHERE d.resource = '" + ip.replace("'", "''") + "'";
query += " ORDER BY CASE a.daddr WHEN NULL THEN 1 ELSE 0 END, d.qname DESC"; query += " ORDER BY CASE a.daddr WHEN NULL THEN 1 ELSE 0 END, d.qname";
query += " LIMIT 1"; query += " LIMIT 1";
return db.compileStatement(query).simpleQueryForString(); return db.compileStatement(query).simpleQueryForString();
} catch (SQLiteDoneException ignored) { } catch (SQLiteDoneException ignored) {
@ -840,10 +840,11 @@ public class DatabaseHelper extends SQLiteOpenHelper {
lock.readLock().lock(); lock.readLock().lock();
try { try {
SQLiteDatabase db = this.getReadableDatabase(); SQLiteDatabase db = this.getReadableDatabase();
// There is an index on resource
// There is a segmented index on qname // There is a segmented index on qname
String query = "SELECT ID AS _id, *"; String query = "SELECT ID AS _id, *";
query += " FROM dns"; query += " FROM dns";
query += " ORDER BY qname, resource"; query += " ORDER BY resource, qname";
return db.rawQuery(query, new String[]{}); return db.rawQuery(query, new String[]{});
} finally { } finally {
lock.readLock().unlock(); lock.readLock().unlock();
@ -863,11 +864,14 @@ public class DatabaseHelper extends SQLiteOpenHelper {
query += " LEFT JOIN dns AS d"; query += " LEFT JOIN dns AS d";
query += " ON d.qname = a.daddr"; query += " ON d.qname = a.daddr";
query += " WHERE a.block >= 0"; query += " WHERE a.block >= 0";
query += " AND d.time + d.ttl >= " + now; query += " AND d.time + d.ttl >= ?";
if (dname != null) if (dname != null)
query += " AND a.daddr = ?"; query += " AND a.daddr = ?";
return db.rawQuery(query, dname == null ? new String[]{} : new String[]{dname}); if (dname == null)
return db.rawQuery(query, new String[]{Long.toString(now)});
else
return db.rawQuery(query, new String[]{Long.toString(now), dname});
} finally { } finally {
lock.readLock().unlock(); lock.readLock().unlock();
} }