Checked database indices

This commit is contained in:
M66B 2016-02-17 15:47:53 +01:00
parent a5b44db63f
commit 6a1a3157b6
1 changed files with 25 additions and 2 deletions

View File

@ -139,8 +139,8 @@ public class DatabaseHelper extends SQLiteOpenHelper {
");"); ");");
db.execSQL("CREATE INDEX idx_log_time ON log(time)"); db.execSQL("CREATE INDEX idx_log_time ON log(time)");
db.execSQL("CREATE INDEX idx_log_dest ON log(daddr)"); db.execSQL("CREATE INDEX idx_log_dest ON log(daddr)");
db.execSQL("CREATE INDEX idx_log_dport ON log(dport)");
db.execSQL("CREATE INDEX idx_log_dname ON log(dname)"); db.execSQL("CREATE INDEX idx_log_dname ON log(dname)");
db.execSQL("CREATE INDEX idx_log_dport ON log(dport)");
db.execSQL("CREATE INDEX idx_log_uid ON log(uid)"); db.execSQL("CREATE INDEX idx_log_uid ON log(uid)");
} }
@ -396,8 +396,9 @@ public class DatabaseHelper extends SQLiteOpenHelper {
public Cursor getLog(boolean udp, boolean tcp, boolean other, boolean allowed, boolean blocked) { public Cursor getLog(boolean udp, boolean tcp, boolean other, boolean allowed, boolean blocked) {
mLock.readLock().lock(); mLock.readLock().lock();
try { try {
// There is no index on protocol/allowed for write performance
SQLiteDatabase db = this.getReadableDatabase(); SQLiteDatabase db = this.getReadableDatabase();
// There is an index on time
// There is no index on protocol/allowed for write performance
String query = "SELECT ID AS _id, *"; String query = "SELECT ID AS _id, *";
query += " FROM log"; query += " FROM log";
query += " WHERE (0 = 1"; query += " WHERE (0 = 1";
@ -424,6 +425,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
mLock.readLock().lock(); mLock.readLock().lock();
try { try {
SQLiteDatabase db = this.getReadableDatabase(); SQLiteDatabase db = this.getReadableDatabase();
// There is a segmented index on daadr, dname, dport and uid
String query = "SELECT ID AS _id, *"; String query = "SELECT ID AS _id, *";
query += " FROM log"; query += " FROM log";
query += " WHERE daddr LIKE ? OR dname LIKE ? OR dport = ? OR uid = ?"; query += " WHERE daddr LIKE ? OR dname LIKE ? OR dport = ? OR uid = ?";
@ -450,6 +452,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
if (block >= 0) if (block >= 0)
cv.put("block", block); cv.put("block", block);
// There is a segmented index on uid, version, protocol, daddr and dport
rows = db.update("access", cv, "uid = ? AND version = ? AND protocol = ? AND daddr = ? AND dport = ?", rows = db.update("access", cv, "uid = ? AND version = ? AND protocol = ? AND daddr = ? AND dport = ?",
new String[]{ new String[]{
Integer.toString(packet.uid), Integer.toString(packet.uid),
@ -491,6 +494,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
SQLiteDatabase db = this.getWritableDatabase(); SQLiteDatabase db = this.getWritableDatabase();
db.beginTransactionNonExclusive(); db.beginTransactionNonExclusive();
try { try {
// There is a segmented index on uid, version, protocol, daddr and dport
String selection = "uid = ? AND version = ? AND protocol = ? AND daddr = ? AND dport = ?"; String selection = "uid = ? AND version = ? AND protocol = ? AND daddr = ? AND dport = ?";
String[] selectionArgs = new String[]{ String[] selectionArgs = new String[]{
Integer.toString(usage.Uid), Integer.toString(usage.Uid),
@ -579,6 +583,8 @@ public class DatabaseHelper extends SQLiteOpenHelper {
SQLiteDatabase db = this.getWritableDatabase(); SQLiteDatabase db = this.getWritableDatabase();
db.beginTransactionNonExclusive(); db.beginTransactionNonExclusive();
try { try {
// There is a segmented index on uid
// There is no index on block for write performance
db.delete("access", "uid = ? AND block < 0", new String[]{Integer.toString(uid)}); db.delete("access", "uid = ? AND block < 0", new String[]{Integer.toString(uid)});
db.setTransactionSuccessful(); db.setTransactionSuccessful();
@ -595,6 +601,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
public void resetUsage(int uid) { public void resetUsage(int uid) {
mLock.writeLock().lock(); mLock.writeLock().lock();
try { try {
// There is a segmented index on uid
SQLiteDatabase db = this.getWritableDatabase(); SQLiteDatabase db = this.getWritableDatabase();
db.beginTransactionNonExclusive(); db.beginTransactionNonExclusive();
try { try {
@ -620,6 +627,8 @@ public class DatabaseHelper extends SQLiteOpenHelper {
mLock.readLock().lock(); mLock.readLock().lock();
try { try {
SQLiteDatabase db = this.getReadableDatabase(); SQLiteDatabase db = this.getReadableDatabase();
// There is a segmented index on uid
// There is no index on time for write performance
String query = "SELECT ID AS _id, *"; String query = "SELECT ID AS _id, *";
query += " FROM access WHERE uid = ?"; query += " FROM access WHERE uid = ?";
query += " ORDER BY time DESC"; query += " ORDER BY time DESC";
@ -634,6 +643,8 @@ public class DatabaseHelper extends SQLiteOpenHelper {
mLock.readLock().lock(); mLock.readLock().lock();
try { try {
SQLiteDatabase db = this.getReadableDatabase(); SQLiteDatabase db = this.getReadableDatabase();
// There is a segmented index on uid
// There is no index on block for write performance
return db.query("access", null, "block >= 0", null, null, null, "uid"); return db.query("access", null, "block >= 0", null, null, null, "uid");
} finally { } finally {
mLock.readLock().unlock(); mLock.readLock().unlock();
@ -644,6 +655,8 @@ public class DatabaseHelper extends SQLiteOpenHelper {
mLock.readLock().lock(); mLock.readLock().lock();
try { try {
SQLiteDatabase db = this.getReadableDatabase(); SQLiteDatabase db = this.getReadableDatabase();
// There is a segmented index on uid and daddr
// There is no index on allowed and time for write performance
String query = "SELECT MAX(time) AS time, daddr, allowed"; String query = "SELECT MAX(time) AS time, daddr, allowed";
query += " FROM access"; query += " FROM access";
query += " WHERE uid = ?"; query += " WHERE uid = ?";
@ -660,6 +673,8 @@ public class DatabaseHelper extends SQLiteOpenHelper {
mLock.readLock().lock(); mLock.readLock().lock();
try { try {
SQLiteDatabase db = this.getReadableDatabase(); SQLiteDatabase db = this.getReadableDatabase();
// There is a segmented index on uid
// There is no index on block for write performance
return db.compileStatement("SELECT COUNT(*) FROM access WHERE block >= 0 AND uid =" + uid).simpleQueryForLong(); return db.compileStatement("SELECT COUNT(*) FROM access WHERE block >= 0 AND uid =" + uid).simpleQueryForLong();
} finally { } finally {
mLock.readLock().unlock(); mLock.readLock().unlock();
@ -670,6 +685,8 @@ public class DatabaseHelper extends SQLiteOpenHelper {
mLock.readLock().lock(); mLock.readLock().lock();
try { try {
SQLiteDatabase db = this.getReadableDatabase(); SQLiteDatabase db = this.getReadableDatabase();
// There is a segmented index on uid
// There is no index on block for write performance
return db.compileStatement("SELECT COUNT(*) FROM access WHERE block > 0 AND uid =" + uid).simpleQueryForLong(); return db.compileStatement("SELECT COUNT(*) FROM access WHERE block > 0 AND uid =" + uid).simpleQueryForLong();
} finally { } finally {
mLock.readLock().unlock(); mLock.readLock().unlock();
@ -717,6 +734,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
SQLiteDatabase db = this.getWritableDatabase(); SQLiteDatabase db = this.getWritableDatabase();
db.beginTransactionNonExclusive(); db.beginTransactionNonExclusive();
try { try {
// There is no index on time for write performance
int rows = db.delete("dns", "time < ?", new String[]{Long.toString(time)}); int rows = db.delete("dns", "time < ?", new String[]{Long.toString(time)});
Log.i(TAG, "Cleanup DNS" + Log.i(TAG, "Cleanup DNS" +
" before=" + SimpleDateFormat.getDateTimeInstance().format(new Date(time)) + " before=" + SimpleDateFormat.getDateTimeInstance().format(new Date(time)) +
@ -735,6 +753,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
mLock.readLock().lock(); mLock.readLock().lock();
try { try {
SQLiteDatabase db = this.getReadableDatabase(); SQLiteDatabase db = this.getReadableDatabase();
// There is a segmented index on resource
return db.compileStatement( return db.compileStatement(
"SELECT qname FROM dns WHERE resource = '" + ip.replace("'", "''") + "'") "SELECT qname FROM dns WHERE resource = '" + ip.replace("'", "''") + "'")
.simpleQueryForString(); .simpleQueryForString();
@ -750,6 +769,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
mLock.readLock().lock(); mLock.readLock().lock();
try { try {
SQLiteDatabase db = this.getReadableDatabase(); SQLiteDatabase db = this.getReadableDatabase();
// 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"; query += " ORDER BY qname";
@ -764,6 +784,9 @@ public class DatabaseHelper extends SQLiteOpenHelper {
try { try {
SQLiteDatabase db = this.getReadableDatabase(); SQLiteDatabase db = this.getReadableDatabase();
// There is a segmented index on qname
// There is a segmented index on daddr
// There is no index on block for write performance
String query = "SELECT a.uid, a.version, a.protocol, a.daddr, d.resource, a.dport, a.block"; String query = "SELECT a.uid, a.version, a.protocol, a.daddr, d.resource, a.dport, a.block";
query += " FROM access AS a"; query += " FROM access AS a";
query += " LEFT JOIN dns AS d"; query += " LEFT JOIN dns AS d";